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
99
100
101
102
use std::fmt;
use failure::Fail;
#[derive(Clone, Copy, Debug, Fail, PartialEq)]
pub enum ValidationError {
#[fail(display = "No profile for pressure.")]
NoPressureProfile,
#[fail(
display = "Vector length for profile of {} ({}) does not match length of pressure profile: {}.",
_0,
_1,
_2
)]
InvalidVectorLength(&'static str, usize, usize),
#[fail(display = "Pressure not decreasing with height.")]
PressureNotDecreasingWithHeight,
#[fail(display = "Dry bulb temperature less than wet bulb temperature. ({} < {})", _0, _1)]
TemperatureLessThanWetBulb(f64, f64),
#[fail(display = "Dry bulb temperature less than dew point temperature. ({} < {})", _0, _1)]
TemperatureLessThanDewPoint(f64, f64),
#[fail(display = "Wet bulb temperature less than dew point temperature. ({} < {})", _0, _1)]
WetBulbLessThanDewPoint(f64, f64),
#[fail(display = "{} less than 0.0 ({})", _0, _1)]
InvalidNegativeValue(&'static str, f64),
#[fail(display = "Wind direction {} not in 0 to 360\u{00B0} range", _0)]
InvalidWindDirection(f64),
}
#[derive(Debug, Default)]
pub struct ValidationErrors {
errors: Vec<ValidationError>,
}
impl ValidationErrors {
pub fn new() -> Self {
ValidationErrors { errors: vec![] }
}
pub fn to_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 Fail for ValidationErrors {
fn cause(&self) -> Option<&Fail> {
if self.errors.is_empty() {
None
} else {
Some(&self.errors[0])
}
}
}