mirror of
https://github.com/kanidm/kanidm.git
synced 2025-02-24 04:57:00 +01:00
168 lines
4.5 KiB
Rust
168 lines
4.5 KiB
Rust
use std::collections::BTreeSet;
|
|
|
|
use crate::prelude::*;
|
|
use crate::repl::proto::ReplAttrV1;
|
|
use crate::schema::SchemaAttribute;
|
|
use crate::valueset::{DbValueSetV2, ValueSet};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct ValueSetRestricted {
|
|
set: BTreeSet<String>,
|
|
}
|
|
|
|
impl ValueSetRestricted {
|
|
pub fn new(s: String) -> Box<Self> {
|
|
let mut set = BTreeSet::new();
|
|
set.insert(s);
|
|
Box::new(ValueSetRestricted { set })
|
|
}
|
|
|
|
pub fn push(&mut self, s: String) -> bool {
|
|
self.set.insert(s)
|
|
}
|
|
|
|
pub fn from_dbvs2(data: Vec<String>) -> Result<ValueSet, OperationError> {
|
|
let set = data.into_iter().collect();
|
|
Ok(Box::new(ValueSetRestricted { set }))
|
|
}
|
|
|
|
pub fn from_repl_v1(data: &[String]) -> Result<ValueSet, OperationError> {
|
|
let set = data.iter().cloned().collect();
|
|
Ok(Box::new(ValueSetRestricted { set }))
|
|
}
|
|
|
|
// We need to allow this, because rust doesn't allow us to impl FromIterator on foreign
|
|
// types, and String is foreign.
|
|
#[allow(clippy::should_implement_trait)]
|
|
pub fn from_iter<T>(iter: T) -> Option<Box<Self>>
|
|
where
|
|
T: IntoIterator<Item = String>,
|
|
{
|
|
let set = iter.into_iter().collect();
|
|
Some(Box::new(ValueSetRestricted { set }))
|
|
}
|
|
}
|
|
|
|
impl ValueSetT for ValueSetRestricted {
|
|
fn insert_checked(&mut self, value: Value) -> Result<bool, OperationError> {
|
|
match value {
|
|
Value::RestrictedString(s) => Ok(self.set.insert(s)),
|
|
_ => {
|
|
debug_assert!(false);
|
|
Err(OperationError::InvalidValueState)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn clear(&mut self) {
|
|
self.set.clear();
|
|
}
|
|
|
|
fn remove(&mut self, pv: &PartialValue) -> bool {
|
|
match pv {
|
|
PartialValue::RestrictedString(s) => self.set.remove(s),
|
|
_ => {
|
|
debug_assert!(false);
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
fn contains(&self, pv: &PartialValue) -> bool {
|
|
match pv {
|
|
PartialValue::RestrictedString(s) => self.set.contains(s.as_str()),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
fn substring(&self, pv: &PartialValue) -> bool {
|
|
match pv {
|
|
PartialValue::RestrictedString(s2) => self.set.iter().any(|s1| s1.contains(s2)),
|
|
_ => {
|
|
debug_assert!(false);
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
fn lessthan(&self, _pv: &PartialValue) -> bool {
|
|
false
|
|
}
|
|
|
|
fn len(&self) -> usize {
|
|
self.set.len()
|
|
}
|
|
|
|
fn generate_idx_eq_keys(&self) -> Vec<String> {
|
|
self.set.iter().cloned().collect()
|
|
}
|
|
|
|
fn syntax(&self) -> SyntaxType {
|
|
unreachable!();
|
|
// SyntaxType::RestrictedString
|
|
}
|
|
|
|
fn validate(&self, _schema_attr: &SchemaAttribute) -> bool {
|
|
self.set.iter().all(|_s| {
|
|
// schema_attr.re_pattern.is_match(s)
|
|
false
|
|
})
|
|
}
|
|
|
|
fn to_proto_string_clone_iter(&self) -> Box<dyn Iterator<Item = String> + '_> {
|
|
Box::new(self.set.iter().cloned())
|
|
}
|
|
|
|
fn to_db_valueset_v2(&self) -> DbValueSetV2 {
|
|
DbValueSetV2::RestrictedString(self.set.iter().cloned().collect())
|
|
}
|
|
|
|
fn to_repl_v1(&self) -> ReplAttrV1 {
|
|
ReplAttrV1::RestrictedString {
|
|
set: self.set.iter().cloned().collect(),
|
|
}
|
|
}
|
|
|
|
fn to_partialvalue_iter(&self) -> Box<dyn Iterator<Item = PartialValue> + '_> {
|
|
Box::new(self.set.iter().cloned().map(PartialValue::RestrictedString))
|
|
}
|
|
|
|
fn to_value_iter(&self) -> Box<dyn Iterator<Item = Value> + '_> {
|
|
Box::new(self.set.iter().cloned().map(Value::RestrictedString))
|
|
}
|
|
|
|
fn equal(&self, other: &ValueSet) -> bool {
|
|
if let Some(other) = other.as_restricted_string_set() {
|
|
&self.set == other
|
|
} else {
|
|
debug_assert!(false);
|
|
false
|
|
}
|
|
}
|
|
|
|
fn merge(&mut self, other: &ValueSet) -> Result<(), OperationError> {
|
|
if let Some(b) = other.as_restricted_string_set() {
|
|
mergesets!(self.set, b)
|
|
} else {
|
|
debug_assert!(false);
|
|
Err(OperationError::InvalidValueState)
|
|
}
|
|
}
|
|
|
|
fn to_restricted_string_single(&self) -> Option<&str> {
|
|
if self.set.len() == 1 {
|
|
self.set.iter().take(1).next().map(|s| s.as_str())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn as_restricted_string_set(&self) -> Option<&BTreeSet<String>> {
|
|
Some(&self.set)
|
|
}
|
|
|
|
fn as_restricted_string_iter(&self) -> Option<Box<dyn Iterator<Item = &str> + '_>> {
|
|
Some(Box::new(self.set.iter().map(|s| s.as_str())))
|
|
}
|
|
}
|