use std::error::Error as StdError;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Error {
NonPositiveTime,
InvalidHms,
AgeOutOfRange {
min: u16,
max: u16,
},
EmptyRaces,
InvalidVdot,
InvalidDistance,
UnrecognizedDistance,
InvalidPace,
UnsolvableTime,
UnsupportedAgeGradeDistance,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NonPositiveTime => f.write_str("duration must be positive"),
Self::InvalidHms => f.write_str("minutes and seconds must be less than 60"),
Self::AgeOutOfRange { min, max } => {
write!(f, "age is outside the supported range ({min}–{max})")
}
Self::EmptyRaces => f.write_str("at least one race time is required"),
Self::InvalidVdot => f.write_str("VDOT must be a positive finite value"),
Self::InvalidDistance => {
f.write_str("distance must be a positive finite number of metres")
}
Self::UnrecognizedDistance => f.write_str("unrecognized distance"),
Self::InvalidPace => f.write_str("pace must be a positive finite value"),
Self::UnsolvableTime => {
f.write_str("no finish time in the 2–12 min/km VDOT solver bracket")
}
Self::UnsupportedAgeGradeDistance => {
f.write_str("distance is not supported for age grading")
}
}
}
}
impl StdError for Error {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_messages_match_constructors() {
assert_eq!(
Error::NonPositiveTime.to_string(),
"duration must be positive"
);
assert_eq!(
Error::EmptyRaces.to_string(),
"at least one race time is required"
);
assert_eq!(
Error::InvalidVdot.to_string(),
"VDOT must be a positive finite value"
);
assert_eq!(
Error::InvalidDistance.to_string(),
"distance must be a positive finite number of metres"
);
assert_eq!(
Error::UnrecognizedDistance.to_string(),
"unrecognized distance"
);
assert_eq!(
Error::InvalidHms.to_string(),
"minutes and seconds must be less than 60"
);
assert_eq!(
Error::InvalidPace.to_string(),
"pace must be a positive finite value"
);
assert_eq!(
Error::UnsolvableTime.to_string(),
"no finish time in the 2–12 min/km VDOT solver bracket"
);
assert_eq!(
Error::AgeOutOfRange { min: 5, max: 99 }.to_string(),
"age is outside the supported range (5–99)"
);
assert_eq!(
Error::UnsupportedAgeGradeDistance.to_string(),
"distance is not supported for age grading"
);
}
#[test]
fn implements_std_error() {
let err: Box<dyn StdError> = Box::new(Error::EmptyRaces);
assert!(err.source().is_none());
}
#[cfg(feature = "serde")]
#[test]
fn serde_error_roundtrip() {
let json = serde_json::to_string(&Error::EmptyRaces).unwrap();
assert_eq!(
serde_json::from_str::<Error>(&json).unwrap(),
Error::EmptyRaces
);
let age = Error::AgeOutOfRange { min: 5, max: 99 };
let age_json = serde_json::to_string(&age).unwrap();
assert_eq!(serde_json::from_str::<Error>(&age_json).unwrap(), age);
}
}