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 {}