From 84c2dab4200c37c818d83c95f85445ee00d83bf6 Mon Sep 17 00:00:00 2001 From: jelemux Date: Sun, 18 Oct 2020 22:40:57 +0200 Subject: initial commit --- src/validation.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/validation.rs (limited to 'src/validation.rs') diff --git a/src/validation.rs b/src/validation.rs new file mode 100644 index 0000000..715b472 --- /dev/null +++ b/src/validation.rs @@ -0,0 +1,37 @@ + + +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 } ) + } +} \ No newline at end of file -- cgit v1.2.3