Skip to main content

kanidmd_lib/valueset/
spn.rs

1use crate::{
2    prelude::*,
3    schema::SchemaAttribute,
4    valueset::{DbValueSetV2, ScimResolveStatus, ValueSet},
5};
6
7use smolset::SmolSet;
8
9#[derive(Debug, Clone)]
10pub struct ValueSetSpn {
11    set: SmolSet<[(String, String); 1]>,
12}
13
14impl ValueSetSpn {
15    pub fn new(u: (String, String)) -> Box<Self> {
16        let mut set = SmolSet::new();
17        set.insert(u);
18        Box::new(ValueSetSpn { set })
19    }
20
21    pub fn push(&mut self, u: (String, String)) -> bool {
22        self.set.insert(u)
23    }
24
25    pub fn from_dbvs2(data: Vec<(String, String)>) -> Result<ValueSet, OperationError> {
26        let set = data.into_iter().collect();
27        Ok(Box::new(ValueSetSpn { set }))
28    }
29
30    // We need to allow this, because rust doesn't allow us to impl FromIterator on foreign
31    // types, and tuples are always foreign.
32    #[allow(clippy::should_implement_trait)]
33    pub fn from_iter<T>(iter: T) -> Option<Box<Self>>
34    where
35        T: IntoIterator<Item = (String, String)>,
36    {
37        let set = iter.into_iter().collect();
38        Some(Box::new(ValueSetSpn { set }))
39    }
40}
41
42impl ValueSetT for ValueSetSpn {
43    fn insert_checked(&mut self, value: Value) -> Result<bool, OperationError> {
44        match value {
45            Value::Spn(n, d) => Ok(self.set.insert((n, d))),
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::Spn(n, d) => {
60                self.set.remove(&(n.clone(), d.clone()));
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::Spn(n, d) => self.set.contains(&(n.clone(), d.clone())),
72            _ => false,
73        }
74    }
75
76    fn substring(&self, _pv: &PartialValue) -> bool {
77        false
78    }
79
80    fn startswith(&self, _pv: &PartialValue) -> bool {
81        false
82    }
83
84    fn endswith(&self, _pv: &PartialValue) -> bool {
85        false
86    }
87
88    fn lessthan(&self, _pv: &PartialValue) -> bool {
89        false
90    }
91
92    fn len(&self) -> usize {
93        self.set.len()
94    }
95
96    fn generate_idx_eq_keys(&self) -> Vec<String> {
97        self.set.iter().map(|(n, d)| format!("{n}@{d}")).collect()
98    }
99
100    fn syntax(&self) -> SyntaxType {
101        SyntaxType::SecurityPrincipalName
102    }
103
104    fn validate(&self, _schema_attr: &SchemaAttribute) -> bool {
105        self.set.iter().all(|(a, b)| {
106            Value::validate_str_escapes(a)
107                && Value::validate_str_escapes(b)
108                && Value::validate_singleline(a)
109                && Value::validate_singleline(b)
110        })
111    }
112
113    fn to_proto_string_clone_iter(&self) -> Box<dyn Iterator<Item = String> + '_> {
114        Box::new(self.set.iter().map(|(n, d)| format!("{n}@{d}")))
115    }
116
117    fn to_scim_value(&self) -> Option<ScimResolveStatus> {
118        let mut iter = self.set.iter().map(|(n, d)| format!("{n}@{d}"));
119        if self.len() == 1 {
120            let v = iter.next().unwrap_or_default();
121            Some(v.into())
122        } else {
123            let arr = iter.collect::<Vec<_>>();
124            Some(arr.into())
125        }
126    }
127
128    fn to_db_valueset_v2(&self) -> DbValueSetV2 {
129        DbValueSetV2::Spn(self.set.iter().cloned().collect())
130    }
131
132    fn to_partialvalue_iter(&self) -> Box<dyn Iterator<Item = PartialValue> + '_> {
133        Box::new(
134            self.set
135                .iter()
136                .map(|(n, d)| PartialValue::Spn(n.clone(), d.clone())),
137        )
138    }
139
140    fn to_value_iter(&self) -> Box<dyn Iterator<Item = Value> + '_> {
141        Box::new(
142            self.set
143                .iter()
144                .map(|(n, d)| Value::Spn(n.clone(), d.clone())),
145        )
146    }
147
148    fn equal(&self, other: &ValueSet) -> bool {
149        if let Some(other) = other.as_spn_set() {
150            &self.set == other
151        } else {
152            debug_assert!(false);
153            false
154        }
155    }
156
157    fn merge(&mut self, other: &ValueSet) -> Result<(), OperationError> {
158        if let Some(b) = other.as_spn_set() {
159            mergesets!(self.set, b)
160        } else {
161            debug_assert!(false);
162            Err(OperationError::InvalidValueState)
163        }
164    }
165
166    /*
167    fn to_spn_single(&self) -> Option<> {
168        if self.set.len() == 1 {
169            self.set.iter().copied().take(1).next()
170        } else {
171            None
172        }
173    }
174    */
175
176    fn as_spn_set(&self) -> Option<&SmolSet<[(String, String); 1]>> {
177        Some(&self.set)
178    }
179
180    /*
181    fn as_spn_iter(&self) -> Option<Box<dyn Iterator<Item = Spn> + '_>> {
182        Some(Box::new(self.set.iter().copied()))
183    }
184    */
185}
186
187#[cfg(test)]
188mod tests {
189    use super::ValueSetSpn;
190    use crate::prelude::ValueSet;
191
192    #[test]
193    fn test_scim_spn() {
194        let vs: ValueSet = ValueSetSpn::new(("claire".to_string(), "example.com".to_string()));
195        crate::valueset::scim_json_reflexive(&vs, r#""claire@example.com""#);
196    }
197}