Skip to main content

kanidmd_lib/valueset/
hexstring.rs

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