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