kanidmd_lib/valueset/
cid.rs

1use crate::be::dbvalue::DbCidV1;
2use crate::prelude::*;
3use crate::repl::cid::Cid;
4use crate::schema::SchemaAttribute;
5use crate::valueset::{DbValueSetV2, ScimResolveStatus, ValueSet};
6
7use smolset::SmolSet;
8
9#[derive(Debug, Clone)]
10pub struct ValueSetCid {
11    set: SmolSet<[Cid; 1]>,
12}
13
14impl ValueSetCid {
15    pub fn new(u: Cid) -> Box<Self> {
16        let mut set = SmolSet::new();
17        set.insert(u);
18        Box::new(ValueSetCid { set })
19    }
20
21    pub fn push(&mut self, u: Cid) -> bool {
22        self.set.insert(u)
23    }
24
25    pub fn from_dbvs2(data: Vec<DbCidV1>) -> Result<ValueSet, OperationError> {
26        let set = data
27            .into_iter()
28            .map(|dc| Cid {
29                s_uuid: dc.server_id,
30                ts: dc.timestamp,
31            })
32            .collect();
33        Ok(Box::new(ValueSetCid { set }))
34    }
35}
36
37impl FromIterator<Cid> for Option<Box<ValueSetCid>> {
38    fn from_iter<T>(iter: T) -> Option<Box<ValueSetCid>>
39    where
40        T: IntoIterator<Item = Cid>,
41    {
42        let set = iter.into_iter().collect();
43        Some(Box::new(ValueSetCid { set }))
44    }
45}
46
47impl ValueSetT for ValueSetCid {
48    fn insert_checked(&mut self, value: Value) -> Result<bool, OperationError> {
49        match value {
50            Value::Cid(u) => Ok(self.set.insert(u)),
51            _ => {
52                debug_assert!(false);
53                Err(OperationError::InvalidValueState)
54            }
55        }
56    }
57
58    fn clear(&mut self) {
59        self.set.clear();
60    }
61
62    fn remove(&mut self, pv: &PartialValue, _cid: &Cid) -> bool {
63        match pv {
64            PartialValue::Cid(u) => self.set.remove(u),
65            _ => {
66                debug_assert!(false);
67                true
68            }
69        }
70    }
71
72    fn contains(&self, pv: &PartialValue) -> bool {
73        match pv {
74            PartialValue::Cid(u) => self.set.contains(u),
75            _ => false,
76        }
77    }
78
79    fn substring(&self, _pv: &PartialValue) -> bool {
80        false
81    }
82
83    fn startswith(&self, _pv: &PartialValue) -> bool {
84        false
85    }
86
87    fn endswith(&self, _pv: &PartialValue) -> bool {
88        false
89    }
90
91    fn lessthan(&self, pv: &PartialValue) -> bool {
92        match pv {
93            PartialValue::Cid(c2) => self.set.iter().any(|c1| c1 < c2),
94            _ => false,
95        }
96    }
97
98    fn len(&self) -> usize {
99        self.set.len()
100    }
101
102    fn generate_idx_eq_keys(&self) -> Vec<String> {
103        Vec::with_capacity(0)
104    }
105
106    fn syntax(&self) -> SyntaxType {
107        SyntaxType::Cid
108    }
109
110    fn validate(&self, _schema_attr: &SchemaAttribute) -> bool {
111        true
112    }
113
114    fn to_proto_string_clone_iter(&self) -> Box<dyn Iterator<Item = String> + '_> {
115        Box::new(self.set.iter().map(|c| c.to_string()))
116    }
117
118    fn to_scim_value(&self) -> Option<ScimResolveStatus> {
119        let mut iter = self.set.iter().map(|cid| cid.to_string());
120        if self.len() == 1 {
121            let v = iter.next().unwrap_or_default();
122            Some(v.into())
123        } else {
124            let arr = iter.collect::<Vec<_>>();
125            Some(arr.into())
126        }
127    }
128
129    fn to_db_valueset_v2(&self) -> DbValueSetV2 {
130        DbValueSetV2::Cid(
131            self.set
132                .iter()
133                .map(|c| DbCidV1 {
134                    server_id: c.s_uuid,
135                    timestamp: c.ts,
136                })
137                .collect(),
138        )
139    }
140
141    fn to_partialvalue_iter(&self) -> Box<dyn Iterator<Item = PartialValue> + '_> {
142        Box::new(self.set.iter().cloned().map(PartialValue::new_cid))
143    }
144
145    fn to_value_iter(&self) -> Box<dyn Iterator<Item = Value> + '_> {
146        Box::new(self.set.iter().cloned().map(Value::new_cid))
147    }
148
149    fn equal(&self, other: &ValueSet) -> bool {
150        if let Some(other) = other.as_cid_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_cid_set() {
160            mergesets!(self.set, b)
161        } else {
162            debug_assert!(false);
163            Err(OperationError::InvalidValueState)
164        }
165    }
166
167    fn to_cid_single(&self) -> Option<Cid> {
168        if self.set.len() == 1 {
169            self.set.iter().take(1).next().cloned()
170        } else {
171            None
172        }
173    }
174
175    fn as_cid_set(&self) -> Option<&SmolSet<[Cid; 1]>> {
176        Some(&self.set)
177    }
178
179    /*
180    fn as_cid_iter(&self) -> Option<Box<dyn Iterator<Item = Cid> + '_>> {
181        Some(Box::new(self.set.iter().copied()))
182    }
183    */
184}
185
186#[cfg(test)]
187mod tests {
188    use super::ValueSetCid;
189    use crate::prelude::{Cid, ValueSet};
190
191    #[test]
192    fn test_scim_cid() {
193        let vs: ValueSet = ValueSetCid::new(Cid::new_zero());
194
195        let data = r#""00000000000000000000000000000000-00000000-0000-0000-0000-000000000000""#;
196        crate::valueset::scim_json_reflexive(&vs, data);
197    }
198}