From 3a0a55362fd98c74bfb14df320bcacafd0c57835 Mon Sep 17 00:00:00 2001 From: jelemux Date: Mon, 19 Oct 2020 22:46:45 +0200 Subject: break things down into their most basic parts --- excluded/validation.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 excluded/validation.rs (limited to 'excluded/validation.rs') 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, +} + +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 -- cgit v1.2.3