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,
},
InvalidPower,
InvalidMass,
InvalidWork,
InsufficientEfforts,
DurationOutOfModelRange,
UnsolvablePowerDuration,
InvalidPhysicsParam,
UnsolvableSpeed,
}
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::InvalidPower => f.write_str("power must be a positive finite number of watts"),
Self::InvalidMass => f.write_str("mass must be a positive finite number of kilograms"),
Self::InvalidWork => f.write_str("work must be a positive finite number of joules"),
Self::InsufficientEfforts => f.write_str("not enough maximal efforts for this model"),
Self::DurationOutOfModelRange => {
f.write_str("duration is outside the model's valid window")
}
Self::UnsolvablePowerDuration => {
f.write_str("power–duration model could not be solved")
}
Self::InvalidPhysicsParam => {
f.write_str("physics parameter is zero, negative, non-finite, or out of range")
}
Self::UnsolvableSpeed => {
f.write_str("no speed in the solver bracket matches the given power")
}
}
}
}
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::InvalidHms.to_string(),
"minutes and seconds must be less than 60"
);
assert_eq!(
Error::AgeOutOfRange { min: 15, max: 90 }.to_string(),
"age is outside the supported range (15–90)"
);
assert_eq!(
Error::InvalidPower.to_string(),
"power must be a positive finite number of watts"
);
assert_eq!(
Error::InvalidMass.to_string(),
"mass must be a positive finite number of kilograms"
);
assert_eq!(
Error::InvalidWork.to_string(),
"work must be a positive finite number of joules"
);
assert_eq!(
Error::InsufficientEfforts.to_string(),
"not enough maximal efforts for this model"
);
assert_eq!(
Error::DurationOutOfModelRange.to_string(),
"duration is outside the model's valid window"
);
assert_eq!(
Error::UnsolvablePowerDuration.to_string(),
"power–duration model could not be solved"
);
assert_eq!(
Error::InvalidPhysicsParam.to_string(),
"physics parameter is zero, negative, non-finite, or out of range"
);
assert_eq!(
Error::UnsolvableSpeed.to_string(),
"no speed in the solver bracket matches the given power"
);
}
#[test]
fn implements_std_error() {
let err: Box<dyn StdError> = Box::new(Error::InvalidPower);
assert!(err.source().is_none());
}
#[cfg(feature = "serde")]
#[test]
fn serde_error_roundtrip() {
let json = serde_json::to_string(&Error::InvalidPower).unwrap();
assert_eq!(
serde_json::from_str::<Error>(&json).unwrap(),
Error::InvalidPower
);
let age = Error::AgeOutOfRange { min: 15, max: 90 };
let age_json = serde_json::to_string(&age).unwrap();
assert_eq!(serde_json::from_str::<Error>(&age_json).unwrap(), age);
}
}