detsys_ids_client/
checkin.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::{collections::HashMap, sync::Arc};

use serde::Deserialize;

use crate::{collator::FeatureFacts, Map};

pub(crate) type CoherentFeatureFlags = HashMap<String, Arc<Feature<serde_json::Value>>>;

#[derive(Clone, Debug, Deserialize, Default)]
pub struct Checkin {
    pub(crate) options: CoherentFeatureFlags,
}

impl Checkin {
    pub(crate) fn as_feature_facts(&self) -> FeatureFacts {
        let mut feature_facts = Map::new();
        feature_facts.insert(
            "$active_feature_flags".into(),
            self.options
                .keys()
                .map(|v| serde_json::Value::from(v.to_owned()))
                .collect::<Vec<serde_json::Value>>()
                .into(),
        );

        for (name, feat) in self.options.iter() {
            feature_facts.insert(format!("$feature/{name}"), feat.variant.clone());
        }

        FeatureFacts(feature_facts)
    }
}

#[derive(Clone, Debug, Deserialize)]
pub struct Feature<T: serde::de::DeserializeOwned> {
    pub variant: serde_json::Value,
    #[serde(
        with = "crate::json_string",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub payload: Option<T>,
}

#[cfg(test)]
mod test {

    #[test]
    fn test_parse() {
        let json = r#"
        {

            "options": {
                "dni-det-msg-ptr": {
                    "variant": "a",
                    "payload": "\"dni-det-msg-a\""
                },
                "dni-det-msg-a": {
                    "variant": "a",
                    "payload": "\"hello\""
                },
                "fine-grained-tokens": {
                    "variant": false
                }
            }
        }"#;

        let _: super::Checkin = serde_json::from_str(json).unwrap();
    }
}