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