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