Skip to main content

micromegas_analytics/properties/
property_set.rs

1use anyhow::Result;
2use micromegas_transit::value::{Object, Value};
3
4/// A set of properties, backed by an arena-allocated `transit` object.
5///
6/// Borrows the parse arena, so it is valid only within a single `parse_block`
7/// call; consumers must serialize/copy it out before the arena is dropped.
8#[derive(Debug, Clone, Copy)]
9pub struct PropertySet<'a> {
10    obj: &'a Object<'a>,
11}
12
13impl<'a> PropertySet<'a> {
14    pub fn new(obj: &'a Object<'a>) -> Self {
15        Self { obj }
16    }
17
18    pub fn empty() -> PropertySet<'static> {
19        static EMPTY: Object<'static> = Object {
20            type_name: "EmptyPropertySet",
21            members: &[],
22        };
23        PropertySet { obj: &EMPTY }
24    }
25
26    /// Iterates over the string-valued properties in the set as `(key, value)` pairs.
27    pub fn for_each_property<Fun: FnMut(&'a str, &'a str) -> Result<()>>(
28        &self,
29        mut fun: Fun,
30    ) -> Result<()> {
31        for &(key, value) in self.obj.members {
32            if let Value::String(value_str) = value {
33                fun(key, value_str)?;
34            }
35        }
36        Ok(())
37    }
38
39    /// Stable identity pointer to the underlying arena object.
40    ///
41    /// Used by the dictionary builder for pointer-based deduplication: within a
42    /// single block, every event referencing the same property-set dependency
43    /// shares one arena `Object`, so identical sets compare equal by address.
44    /// The pointer is only ever compared, never dereferenced.
45    pub fn object_ptr(&self) -> *const () {
46        self.obj as *const Object as *const ()
47    }
48}
49
50impl<'a> From<&'a Object<'a>> for PropertySet<'a> {
51    fn from(value: &'a Object<'a>) -> Self {
52        Self::new(value)
53    }
54}