use core::fmt;
use std::error::Error;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RealError {
NonFiniteValue {
name: &'static str,
value: f64,
},
NonFiniteTolerance(f64),
NegativeTolerance(f64),
InvalidInterval {
min: f64,
max: f64,
},
}
impl RealError {
pub(crate) const fn validate_value(name: &'static str, value: f64) -> Result<f64, Self> {
if !value.is_finite() {
return Err(Self::NonFiniteValue { name, value });
}
Ok(value)
}
pub(crate) const fn validate_tolerance(tolerance: f64) -> Result<f64, Self> {
if !tolerance.is_finite() {
return Err(Self::NonFiniteTolerance(tolerance));
}
if tolerance < 0.0 {
return Err(Self::NegativeTolerance(tolerance));
}
Ok(tolerance)
}
}
impl fmt::Display for RealError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NonFiniteValue { name, value } => {
write!(formatter, "{name} must be finite, got {value}")
},
Self::NonFiniteTolerance(value) => {
write!(formatter, "tolerance must be finite, got {value}")
},
Self::NegativeTolerance(value) => {
write!(formatter, "tolerance must be non-negative, got {value}")
},
Self::InvalidInterval { min, max } => {
write!(
formatter,
"interval min must not exceed max, got min={min}, max={max}"
)
},
}
}
}
impl Error for RealError {}