pub trait Validation { fn validate(&self) -> Result<(), ValidationError>; } #[derive(Debug)] pub struct ValidationError { messages: Vec, } 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 } ) } }