kanidmd_lib/valueset/
bool.rs

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