Skip to main content

kanidmd_lib/valueset/
bool.rs

1use crate::{
2    prelude::*,
3    schema::SchemaAttribute,
4    valueset::{DbValueSetV2, ScimResolveStatus, ValueSet, ValueSetResolveStatus, ValueSetScimPut},
5};
6use kanidm_proto::scim_v1::JsonValue;
7use smolset::SmolSet;
8
9#[derive(Debug, Clone)]
10pub struct ValueSetBool {
11    set: SmolSet<[bool; 1]>,
12}
13
14impl ValueSetBool {
15    pub fn new(b: bool) -> Box<Self> {
16        let mut set = SmolSet::new();
17        set.insert(b);
18        Box::new(ValueSetBool { set })
19    }
20
21    pub fn push(&mut self, b: bool) -> bool {
22        self.set.insert(b)
23    }
24
25    pub fn from_dbvs2(data: Vec<bool>) -> Result<ValueSet, OperationError> {
26        let set = data.into_iter().collect();
27        Ok(Box::new(ValueSetBool { set }))
28    }
29
30    // We need to allow this, because rust doesn't allow us to impl FromIterator on foreign
31    // types, and bool is foreign.
32    #[allow(clippy::should_implement_trait)]
33    pub fn from_iter<T>(iter: T) -> Option<Box<Self>>
34    where
35        T: IntoIterator<Item = bool>,
36    {
37        let set = iter.into_iter().collect();
38        Some(Box::new(ValueSetBool { set }))
39    }
40}
41
42impl ValueSetScimPut for ValueSetBool {
43    fn from_scim_json_put(value: JsonValue) -> Result<ValueSetResolveStatus, OperationError> {
44        let value: bool = serde_json::from_value(value).map_err(|err| {
45            error!(?err, "SCIM boolean syntax invalid");
46            OperationError::SC0005BoolSyntaxInvalid
47        })?;
48
49        let mut set = SmolSet::new();
50        set.insert(value);
51
52        Ok(ValueSetResolveStatus::Resolved(Box::new(ValueSetBool {
53            set,
54        })))
55    }
56}
57
58impl ValueSetT for ValueSetBool {
59    fn insert_checked(&mut self, value: Value) -> Result<bool, OperationError> {
60        match value {
61            Value::Bool(u) => Ok(self.set.insert(u)),
62            _ => {
63                debug_assert!(false);
64                Err(OperationError::InvalidValueState)
65            }
66        }
67    }
68
69    fn clear(&mut self) {
70        self.set.clear();
71    }
72
73    fn remove(&mut self, _pv: &PartialValue, _cid: &Cid) -> bool {
74        true
75    }
76
77    fn contains(&self, pv: &PartialValue) -> bool {
78        match pv {
79            PartialValue::Bool(u) => self.set.contains(u),
80            _ => false,
81        }
82    }
83
84    fn substring(&self, _pv: &PartialValue) -> bool {
85        false
86    }
87
88    fn startswith(&self, _pv: &PartialValue) -> bool {
89        false
90    }
91
92    fn endswith(&self, _pv: &PartialValue) -> bool {
93        false
94    }
95
96    fn lessthan(&self, _pv: &PartialValue) -> bool {
97        false
98    }
99
100    fn len(&self) -> usize {
101        self.set.len()
102    }
103
104    fn generate_idx_eq_keys(&self) -> Vec<String> {
105        self.set.iter().map(|b| b.to_string()).collect()
106    }
107
108    fn syntax(&self) -> SyntaxType {
109        SyntaxType::Boolean
110    }
111
112    fn validate(&self, _schema_attr: &SchemaAttribute) -> bool {
113        true
114    }
115
116    fn to_proto_string_clone_iter(&self) -> Box<dyn Iterator<Item = String> + '_> {
117        Box::new(self.set.iter().map(|b| b.to_string()))
118    }
119
120    fn to_scim_value(&self) -> Option<ScimResolveStatus> {
121        if self.len() == 1 {
122            // Because self.len == 1 we know this has to yield a value.
123            let b = self.set.iter().copied().next().unwrap_or_default();
124
125            Some(b.into())
126        } else {
127            // Makes no sense for more than 1 value.
128            None
129        }
130    }
131
132    fn to_db_valueset_v2(&self) -> DbValueSetV2 {
133        DbValueSetV2::Bool(self.set.iter().cloned().collect())
134    }
135
136    fn to_partialvalue_iter(&self) -> Box<dyn Iterator<Item = PartialValue> + '_> {
137        Box::new(self.set.iter().copied().map(PartialValue::new_bool))
138    }
139
140    fn to_value_iter(&self) -> Box<dyn Iterator<Item = Value> + '_> {
141        Box::new(self.set.iter().copied().map(Value::new_bool))
142    }
143
144    fn equal(&self, other: &ValueSet) -> bool {
145        if let Some(other) = other.as_bool_set() {
146            &self.set == other
147        } else {
148            debug_assert!(false);
149            false
150        }
151    }
152
153    fn merge(&mut self, other: &ValueSet) -> Result<(), OperationError> {
154        if let Some(b) = other.as_bool_set() {
155            mergesets!(self.set, b)
156        } else {
157            debug_assert!(false);
158            Err(OperationError::InvalidValueState)
159        }
160    }
161
162    fn to_bool_single(&self) -> Option<bool> {
163        if self.set.len() == 1 {
164            self.set.iter().copied().take(1).next()
165        } else {
166            None
167        }
168    }
169
170    fn as_bool_set(&self) -> Option<&SmolSet<[bool; 1]>> {
171        Some(&self.set)
172    }
173}
174
175#[cfg(test)]
176mod tests {
177    use super::ValueSetBool;
178    use crate::prelude::ValueSet;
179
180    #[test]
181    fn test_scim_boolean() {
182        let vs: ValueSet = ValueSetBool::new(true);
183        crate::valueset::scim_json_reflexive(&vs, "true");
184
185        // Test that we can parse json values into a valueset.
186        crate::valueset::scim_json_put_reflexive::<ValueSetBool>(&vs, &[])
187    }
188}