use core::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum SurfaceError {
InvalidDimension {
parameter: &'static str,
value: f64,
},
InvalidFlattening {
value: f64,
},
InvalidLatitude {
degrees: f64,
},
UnsupportedSurface {
body: &'static str,
reason: &'static str,
},
DegenerateGeometry {
reason: &'static str,
},
MismatchedObserver,
NonFiniteInput {
field: &'static str,
},
}
impl fmt::Display for SurfaceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidDimension { parameter, value } => {
write!(f, "invalid surface dimension {parameter}={value}")
}
Self::InvalidFlattening { value } => {
write!(f, "invalid ellipsoid flattening f={value}")
}
Self::InvalidLatitude { degrees } => {
write!(f, "invalid latitude {degrees}°")
}
Self::UnsupportedSurface { body, reason } => {
write!(f, "unsupported surface model for {body}: {reason}")
}
Self::DegenerateGeometry { reason } => {
write!(f, "degenerate surface geometry: {reason}")
}
Self::MismatchedObserver => {
write!(
f,
"horizontal observation belongs to a different surface site"
)
}
Self::NonFiniteInput { field } => {
write!(f, "non-finite input in {field}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for SurfaceError {}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn diagnostics_identify_the_invalid_parameter_or_site() {
for (error, expected) in [
(
SurfaceError::InvalidDimension {
parameter: "radius",
value: -1.0,
},
"invalid surface dimension radius=-1",
),
(
SurfaceError::InvalidFlattening { value: 1.0 },
"invalid ellipsoid flattening f=1",
),
(
SurfaceError::InvalidLatitude { degrees: 91.0 },
"invalid latitude 91°",
),
(
SurfaceError::UnsupportedSurface {
body: "test body",
reason: "no reference level",
},
"unsupported surface model for test body: no reference level",
),
(
SurfaceError::DegenerateGeometry {
reason: "zero range",
},
"degenerate surface geometry: zero range",
),
(
SurfaceError::MismatchedObserver,
"horizontal observation belongs to a different surface site",
),
(
SurfaceError::NonFiniteInput { field: "longitude" },
"non-finite input in longitude",
),
] {
assert_eq!(error.to_string(), expected);
}
}
}