micromegas_analytics/properties/
property_set.rs

1use anyhow::Result;
2use micromegas_telemetry::property::Property;
3use micromegas_transit::value::{Object, Value};
4use std::sync::Arc;
5
6/// A set of properties, backed by a `transit` object.
7#[derive(Debug, Clone)]
8pub struct PropertySet {
9    obj: Arc<Object>,
10}
11
12impl PropertySet {
13    pub fn new(obj: Arc<Object>) -> Self {
14        Self { obj }
15    }
16
17    pub fn empty() -> Self {
18        lazy_static::lazy_static! {
19            static ref TYPE_NAME: Arc<String> = Arc::new("EmptyPropertySet".into());
20            static ref EMPTY_SET: PropertySet = PropertySet::new( Arc::new( Object{ type_name: TYPE_NAME.clone(), members: vec![] }) );
21        }
22        EMPTY_SET.clone()
23    }
24
25    /// Iterates over the properties in the set.
26    pub fn for_each_property<Fun: FnMut(Property) -> Result<()>>(
27        &self,
28        mut fun: Fun,
29    ) -> Result<()> {
30        for (key, value) in &self.obj.members {
31            if let Value::String(value_str) = value {
32                fun(Property::new(key.clone(), value_str.clone()))?;
33            }
34        }
35        Ok(())
36    }
37
38    /// Get a reference to the underlying `Arc<Object>` for pointer-based operations.
39    ///
40    /// This is used by custom dictionary builders for efficient deduplication
41    /// based on Arc pointer addresses rather than content hashing.
42    pub fn as_arc_object(&self) -> &Arc<Object> {
43        &self.obj
44    }
45}
46
47impl From<Arc<Object>> for PropertySet {
48    fn from(value: Arc<Object>) -> Self {
49        Self::new(value)
50    }
51}