kanidmd_lib/valueset/
s256.rs1use crate::prelude::*;
2use crate::schema::SchemaAttribute;
3use crate::valueset::ScimResolveStatus;
4use crate::valueset::ValueSetResolveStatus;
5use crate::valueset::ValueSetScimPut;
6use crate::valueset::{DbValueSetV2, ValueSet};
7use crypto_glue::s256::Sha256Output;
8use serde::Deserialize;
9use serde_with::serde_as;
10use std::collections::BTreeSet;
11
12#[derive(Debug, Clone)]
13pub struct ValueSetSha256 {
14 set: BTreeSet<Sha256Output>,
15}
16
17impl ValueSetSha256 {
18 pub fn new(v: Sha256Output) -> Box<Self> {
19 let mut set = BTreeSet::new();
20 set.insert(v);
21 Box::new(ValueSetSha256 { set })
22 }
23
24 pub fn from_dbvs2(set: BTreeSet<Sha256Output>) -> Result<ValueSet, OperationError> {
25 Ok(Box::new(Self { set }))
26 }
27}
28
29#[serde_as]
30#[derive(Deserialize)]
31struct Sha256OutputVec {
32 #[serde(flatten)]
33 #[serde_as(as = "Vec<serde_with::hex::Hex>")]
34 set: Vec<Vec<u8>>,
35}
36
37impl ValueSetScimPut for ValueSetSha256 {
38 fn from_scim_json_put(value: JsonValue) -> Result<ValueSetResolveStatus, OperationError> {
39 let value = serde_json::from_value::<Sha256OutputVec>(value).map_err(|err| {
40 error!(?err, "SCIM SHA256 Syntax Invalid");
41 OperationError::SC0030Sha256SyntaxInvalid
42 })?;
43
44 let set: BTreeSet<Sha256Output> = value
45 .set
46 .into_iter()
47 .map(|bytes| {
48 Sha256Output::from_exact_iter(bytes).ok_or_else(|| {
49 error!("SCIM SHA256 Syntax Invalid");
50 OperationError::SC0030Sha256SyntaxInvalid
51 })
52 })
53 .collect::<Result<_, OperationError>>()?;
54
55 Ok(ValueSetResolveStatus::Resolved(Box::new(Self { set })))
56 }
57}
58
59impl ValueSetT for ValueSetSha256 {
60 fn insert_checked(&mut self, value: Value) -> Result<bool, OperationError> {
61 match value {
62 Value::Sha256(s) => Ok(self.set.insert(s)),
63 _ => {
64 debug_assert!(false);
65 Err(OperationError::InvalidValueState)
66 }
67 }
68 }
69
70 fn clear(&mut self) {
71 self.set.clear();
72 }
73
74 fn remove(&mut self, pv: &PartialValue, _cid: &Cid) -> bool {
75 match pv {
76 PartialValue::Sha256(s) => self.set.remove(s),
77 _ => {
78 debug_assert!(false);
79 true
80 }
81 }
82 }
83
84 fn contains(&self, pv: &PartialValue) -> bool {
85 match pv {
86 PartialValue::Sha256(s) => self.set.contains(s),
87 _ => false,
88 }
89 }
90
91 fn len(&self) -> usize {
92 self.set.len()
93 }
94
95 fn generate_idx_eq_keys(&self) -> Vec<String> {
96 self.set.iter().map(hex::encode).collect()
97 }
98
99 fn syntax(&self) -> SyntaxType {
100 SyntaxType::Sha256
101 }
102
103 fn validate(&self, _schema_attr: &SchemaAttribute) -> bool {
104 true
105 }
106
107 fn to_proto_string_clone_iter(&self) -> Box<dyn Iterator<Item = String> + '_> {
108 Box::new(self.set.iter().map(hex::encode))
109 }
110
111 fn to_scim_value(&self) -> Option<ScimResolveStatus> {
112 let iter = self.set.iter().cloned();
113 let arr = iter.collect::<Vec<_>>();
114 Some(arr.into())
115 }
116
117 fn to_db_valueset_v2(&self) -> DbValueSetV2 {
118 DbValueSetV2::Sha256(self.set.iter().cloned().collect())
119 }
120
121 fn to_partialvalue_iter(&self) -> Box<dyn Iterator<Item = PartialValue> + '_> {
122 Box::new(self.set.iter().cloned().map(PartialValue::Sha256))
123 }
124
125 fn to_value_iter(&self) -> Box<dyn Iterator<Item = Value> + '_> {
126 Box::new(self.set.iter().cloned().map(Value::Sha256))
127 }
128
129 fn equal(&self, other: &ValueSet) -> bool {
130 if let Some(other) = other.as_s256_set() {
131 &self.set == other
132 } else {
133 debug_assert!(false);
134 false
135 }
136 }
137
138 fn merge(&mut self, other: &ValueSet) -> Result<(), OperationError> {
139 if let Some(b) = other.as_s256_set() {
140 mergesets!(self.set, b)
141 } else {
142 debug_assert!(false);
143 Err(OperationError::InvalidValueState)
144 }
145 }
146
147 fn as_s256_set(&self) -> Option<&BTreeSet<Sha256Output>> {
148 Some(&self.set)
149 }
150
151 fn as_s256_set_mut(&mut self) -> Option<&mut BTreeSet<Sha256Output>> {
152 Some(&mut self.set)
153 }
154}