kanidm/src/lib/modify.rs

112 lines
2.6 KiB
Rust
Raw Normal View History

2019-02-22 07:15:48 +01:00
use proto_v1::Modify as ProtoModify;
2019-02-24 05:15:37 +01:00
use proto_v1::ModifyList as ProtoModifyList;
2019-02-22 07:15:48 +01:00
use error::SchemaError;
use schema::{SchemaAttribute, SchemaReadTransaction};
// Should this be std?
use std::slice;
#[derive(Serialize, Deserialize, Debug)]
pub struct ModifyValid;
#[derive(Serialize, Deserialize, Debug)]
pub struct ModifyInvalid;
#[derive(Serialize, Deserialize, Debug)]
pub enum Modify {
// This value *should* exist.
Present(String, String),
// This value *should not* exist.
Removed(String, String),
// This attr *should not* exist.
Purged(String),
}
2019-02-22 07:15:48 +01:00
impl Modify {
pub fn from(m: &ProtoModify) -> Self {
match m {
2019-02-24 05:15:37 +01:00
ProtoModify::Present(a, v) => Modify::Present(a.clone(), v.clone()),
ProtoModify::Removed(a, v) => Modify::Removed(a.clone(), v.clone()),
ProtoModify::Purged(a) => Modify::Purged(a.clone()),
2019-02-22 07:15:48 +01:00
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ModifyList<VALID> {
valid: VALID,
// The order of this list matters. Each change must be done in order.
mods: Vec<Modify>,
}
2019-03-05 06:49:48 +01:00
// TODO: ModifyList should be like filter and have valid/invalid to schema.
// Or do we not care because the entry will be invalid at the end?
impl<'a> IntoIterator for &'a ModifyList<ModifyValid> {
type Item = &'a Modify;
type IntoIter = slice::Iter<'a, Modify>;
fn into_iter(self) -> Self::IntoIter {
self.mods.iter()
}
}
impl ModifyList<ModifyInvalid> {
pub fn new() -> Self {
ModifyList {
valid: ModifyInvalid,
mods: Vec::new()
}
}
pub fn new_list(mods: Vec<Modify>) -> Self {
ModifyList {
valid: ModifyInvalid,
mods: mods
}
}
pub fn push_mod(&mut self, modify: Modify) {
self.mods.push(modify)
}
2019-02-22 07:15:48 +01:00
pub fn from(ml: &ProtoModifyList) -> Self {
// For each ProtoModify, do a from.
ModifyList {
valid: ModifyInvalid,
2019-02-24 05:15:37 +01:00
mods: ml.mods.iter().map(|pm| Modify::from(pm)).collect(),
2019-02-22 07:15:48 +01:00
}
}
pub fn validate(&self,
schema: &SchemaReadTransaction,
) -> Result<ModifyList<ModifyValid>, SchemaError> {
// Check that all attributes exist in the schema
// Normalise them
// Validate them
// Return new ModifyList!
unimplemented!()
}
}
impl ModifyList<ModifyValid> {
#[cfg(test)]
pub unsafe fn new_valid_list(mods: Vec<Modify>) -> Self {
ModifyList {
valid: ModifyValid,
mods: mods
}
}
}
impl<VALID> ModifyList<VALID> {
pub fn len(&self) -> usize {
self.mods.len()
}
}