Skip to main content

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    /// Enable strict cipher field decryption (propagates errors instead of nulling fields).
17    #[serde(alias = "pm-34500-strict-cipher-decryption")]
18    pub strict_cipher_decryption: bool,
19}
20
21impl Flags {
22    /// Create a new `Flags` instance from a map of flag names and values.
23    pub fn load_from_map(map: std::collections::HashMap<String, bool>) -> Self {
24        let map = map
25            .into_iter()
26            .map(|(k, v)| (k, serde_json::Value::Bool(v)))
27            .collect();
28        serde_json::from_value(serde_json::Value::Object(map)).expect("Valid map")
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn test_load_empty_map() {
38        let map = std::collections::HashMap::new();
39        let flags = Flags::load_from_map(map);
40        assert!(!flags.enable_cipher_key_encryption);
41    }
42
43    #[test]
44    fn test_load_valid_map() {
45        let mut map = std::collections::HashMap::new();
46        map.insert("enableCipherKeyEncryption".into(), true);
47        let flags = Flags::load_from_map(map);
48        assert!(flags.enable_cipher_key_encryption);
49    }
50
51    #[test]
52    fn test_load_valid_map_alias() {
53        let mut map = std::collections::HashMap::new();
54        map.insert("cipher-key-encryption".into(), true);
55        let flags = Flags::load_from_map(map);
56        assert!(flags.enable_cipher_key_encryption);
57    }
58
59    #[test]
60    fn test_load_invalid_map() {
61        let mut map = std::collections::HashMap::new();
62        map.insert("thisIsNotAFlag".into(), true);
63        let flags = Flags::load_from_map(map);
64        assert!(!flags.enable_cipher_key_encryption);
65    }
66}