use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum StatError {
EmptyInput,
TooFewObservations {
needed: usize,
got: usize,
},
MismatchedLengths {
a: usize,
b: usize,
},
AllNaN,
DomainError(&'static str),
ProbabilityOutOfRange(f64),
}
impl fmt::Display for StatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StatError::EmptyInput => write!(f, "input slice is empty"),
StatError::TooFewObservations { needed, got } =>
write!(f, "need at least {needed} observations, got {got}"),
StatError::MismatchedLengths { a, b } =>
write!(f, "mismatched lengths: {a} vs {b}"),
StatError::AllNaN => write!(f, "no usable (non-NaN) observations"),
StatError::DomainError(m) => write!(f, "domain error: {m}"),
StatError::ProbabilityOutOfRange(p) =>
write!(f, "probability {p} outside [0, 1]"),
}
}
}
impl std::error::Error for StatError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_is_human_readable() {
assert_eq!(StatError::EmptyInput.to_string(), "input slice is empty");
assert_eq!(
StatError::TooFewObservations { needed: 2, got: 1 }.to_string(),
"need at least 2 observations, got 1",
);
}
#[test]
fn errors_compare_by_value() {
assert_eq!(StatError::AllNaN, StatError::AllNaN);
assert_ne!(StatError::EmptyInput, StatError::AllNaN);
}
}