datadog_agent_remote_config/
product.rs

1use std::fmt;
2use std::ops::Deref;
3
4/// Identifies one configuration assigned to a product.
5///
6/// A configuration's ID is the `semantic.v1` segment of `employee/APM_SEMANTIC_CORE_DD/semantic.v1/config`.
7/// The protocol reports apply status by product and configuration ID alone, so the rest of the path is not part of
8/// the configuration's identity.
9///
10/// Ordering is the order in which the client presents a product's configurations to its decoder.
11#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12pub struct ConfigId(pub(crate) String);
13
14impl ConfigId {
15    /// Creates a configuration ID, such as `semantic.v1`, for calling a decoder directly in tests.
16    ///
17    /// The client creates every ID a decoder receives in production; nothing in this crate accepts a `ConfigId` from a
18    /// subscriber, so an ID created here cannot reach the client. Any string is accepted.
19    pub fn new(id: impl Into<String>) -> Self {
20        Self(id.into())
21    }
22}
23
24impl Deref for ConfigId {
25    type Target = str;
26
27    fn deref(&self) -> &Self::Target {
28        &self.0
29    }
30}
31
32impl fmt::Display for ConfigId {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        self.0.fmt(f)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use crate::product::ConfigId;
41
42    #[test]
43    fn config_id_derefs_to_str_for_matching() {
44        let config_id = ConfigId::new("semantic.v1");
45
46        assert!(matches!(&*config_id, "semantic.v1"));
47        assert_eq!("semantic.v1", config_id.to_string());
48    }
49
50    #[test]
51    fn config_ids_order_ascending_by_id() {
52        let mut ids = [
53            ConfigId("registry.v3".to_string()),
54            ConfigId("registry.v1".to_string()),
55            ConfigId("registry.v2".to_string()),
56        ];
57        ids.sort();
58
59        let sorted: Vec<&str> = ids.iter().map(|id| &**id).collect();
60        assert_eq!(vec!["registry.v1", "registry.v2", "registry.v3"], sorted);
61    }
62}