bitwarden_core/client/
flags.rs

1/// Internal Feature flag representation for the Bitwarden SDK client.
2///
3/// **Note:** The struct should be deserialized directly from the `api/config` endpoint. Take care
4/// to ensure any appropriate aliases are used. By default we use `kebab-schema` but there may be
5/// value in having shorter names.
6///
7/// **Note:** This struct while public, is intended for internal use and may change in future
8/// releases.
9#[derive(Debug, Default, Clone, serde::Deserialize)]
10#[serde(default, rename_all = "kebab-case")]
11pub struct Flags {
12    /// Enable cipher key encryption.
13    #[serde(alias = "enableCipherKeyEncryption", alias = "cipher-key-encryption")]
14    pub enable_cipher_key_encryption: bool,
15}
16
17impl Flags {
18    /// Create a new `Flags` instance from a map of flag names and values.
19    pub fn load_from_map(map: std::collections::HashMap<String, bool>) -> Self {
20        let map = map
21            .into_iter()
22            .map(|(k, v)| (k, serde_json::Value::Bool(v)))
23            .collect();
24        serde_json::from_value(serde_json::Value::Object(map)).expect("Valid map")
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn test_load_empty_map() {
34        let map = std::collections::HashMap::new();
35        let flags = Flags::load_from_map(map);
36        assert!(!flags.enable_cipher_key_encryption);
37    }
38
39    #[test]
40    fn test_load_valid_map() {
41        let mut map = std::collections::HashMap::new();
42        map.insert("enableCipherKeyEncryption".into(), true);
43        let flags = Flags::load_from_map(map);
44        assert!(flags.enable_cipher_key_encryption);
45    }
46
47    #[test]
48    fn test_load_valid_map_alias() {
49        let mut map = std::collections::HashMap::new();
50        map.insert("cipher-key-encryption".into(), true);
51        let flags = Flags::load_from_map(map);
52        assert!(flags.enable_cipher_key_encryption);
53    }
54
55    #[test]
56    fn test_load_invalid_map() {
57        let mut map = std::collections::HashMap::new();
58        map.insert("thisIsNotAFlag".into(), true);
59        let flags = Flags::load_from_map(map);
60        assert!(!flags.enable_cipher_key_encryption);
61    }
62}