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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::error::Error;
use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ValidationError {
NoPressureProfile,
InvalidVectorLength(&'static str, usize, usize),
PressureNotDecreasingWithHeight,
TemperatureLessThanWetBulb(f64, f64),
TemperatureLessThanDewPoint(f64, f64),
WetBulbLessThanDewPoint(f64, f64),
InvalidNegativeValue(&'static str, f64),
InvalidWindDirection(f64),
}
impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use crate::ValidationError::*;
match self {
NoPressureProfile => write!(f, "missing pressure profile"),
InvalidVectorLength(_, _, _) => write!(f, "vectors do not match length"),
PressureNotDecreasingWithHeight => write!(f, "pressure not decreasing with height"),
TemperatureLessThanWetBulb(_, _) => write!(f, "temperature less than wet bulb"),
TemperatureLessThanDewPoint(_, _) => write!(f, "temperature less than dew point"),
WetBulbLessThanDewPoint(_, _) => write!(f, "wet bulb less than dew point"),
InvalidNegativeValue(msg, val) => {
write!(f, "invalid negative value: {} : {}", msg, val)
}
InvalidWindDirection(dir) => write!(f, "invalid wind direction: {}", dir),
}
}
}
impl Error for ValidationError {}
#[derive(Debug, Default)]
pub struct ValidationErrors {
errors: Vec<ValidationError>,
}
impl ValidationErrors {
pub fn new() -> Self {
ValidationErrors { errors: vec![] }
}
pub fn into_inner(self) -> Vec<ValidationError> {
self.errors
}
pub fn push_error(&mut self, result: Result<(), ValidationError>) {
match result {
Ok(()) => {}
Err(err) => self.errors.push(err),
}
}
pub fn check_any(self) -> Result<(), ValidationErrors> {
if self.errors.is_empty() {
Ok(())
} else {
Err(self)
}
}
}
impl fmt::Display for ValidationErrors {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "\nValidation Errors")?;
for error in &self.errors {
writeln!(f, " {}", error)?;
}
writeln!(f)
}
}
impl Error for ValidationErrors {}