diff options
Diffstat (limited to 'excluded')
-rw-r--r-- | excluded/model.rs | 88 | ||||
-rw-r--r-- | excluded/validation.rs | 37 |
2 files changed, 125 insertions, 0 deletions
diff --git a/excluded/model.rs b/excluded/model.rs new file mode 100644 index 0000000..39e4c96 --- /dev/null +++ b/excluded/model.rs @@ -0,0 +1,88 @@ +use chrono::NaiveDateTime; +use crate::validation::{self, *}; + +pub struct BCard { + pub name: Option<Name>, + pub nickname: Option<String>, + pub label: Option<TypedProperty<String>>, + pub address: Option<TypedProperty<Address>>, + pub emails: Option<Vec<TypedProperty<String>>>, + pub title: Option<String>, + pub role: Option<String>, + pub organization: Option<String>, + pub urls: Option<Vec<TypedProperty<String>>>, + pub telephones: Option<Vec<TypedProperty<String>>>, + pub revision: Option<NaiveDateTime>, +} + +impl BCard { + pub fn new() -> Self { + Self { + name: None, + nickname: None, + label: None, + address: None, + emails: None, + title: None, + role: None, + organization: None, + urls: None, + telephones: None, + revision: None, + } + } +} + +impl Validation for BCard { + fn validate(&self) -> Result<(), ValidationError> { + let mut result = Ok(()); + result = match &self.name { + Some(n) => validation::add_results(result, n.validate()), + None => Err( ValidationError{ messages: vec![String::from("Name cannot be empty")] } ), + }; + // TODO add some more validation + result + } +} + +pub struct Name { + pub prefix: Option<String>, + pub first_name: Option<String>, + pub middle_name: Option<String>, + pub family_name: Option<String>, + pub suffix: Option<String>, +} + +impl Name { + pub fn new() -> Self { + Self { + prefix: None, + first_name: None, + middle_name: None, + family_name: None, + suffix: None, + } + } +} + +impl Validation for Name { + fn validate(&self) -> std::result::Result<(), ValidationError> { todo!() } +} + +pub enum WorkHomeType { + Home, + Work, +} + +pub struct TypedProperty<T> { + pub p_type: Option<WorkHomeType>, + pub value: T, +} + +pub struct Address { + pub street: Option<String>, + pub city: Option<String>, + pub locality: Option<String>, + pub postal_code: Option<String>, + pub country: Option<String>, +}
\ No newline at end of file diff --git a/excluded/validation.rs b/excluded/validation.rs new file mode 100644 index 0000000..6258dcf --- /dev/null +++ b/excluded/validation.rs @@ -0,0 +1,37 @@ + + +pub trait Validation { + fn validate(&self) -> Result<(), ValidationError>; +} + +#[derive(Debug)] +pub struct ValidationError { + pub messages: Vec<String>, +} + +impl std::fmt::Display for ValidationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for msg in &self.messages { + write!(f, "{}\n", msg)?; + } + Ok(()) + } +} + +impl std::error::Error for ValidationError { } + +pub fn add_results(first: Result<(), ValidationError>, second: Result<(), ValidationError>) -> Result<(), ValidationError> { + if first.is_ok() && second.is_ok() { + Ok(()) + } else if first.is_ok() && second.is_err() { + second + } else if first.is_err() && second.is_ok() { + first + } else { + let mut first = first.err().unwrap(); + let mut second = second.err().unwrap(); + first.messages.append(&mut second.messages); + + Err( ValidationError{ messages: first.messages } ) + } +}
\ No newline at end of file |