blob: 715b472b3b60469302a965233b62aa2f06872ed9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
pub trait Validation {
fn validate(&self) -> Result<(), ValidationError>;
}
#[derive(Debug)]
pub struct ValidationError {
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 } )
}
}
|