Skip to main content

kanidmd_lib/valueset/
nsuniqueid.rs

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