Skip to main content

prospicio_core/
error.rs

1//! The error type shared across the workspace.
2
3use std::fmt;
4
5/// Result alias using [`Error`].
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Why an operation could not be carried out.
9#[derive(Debug, Clone, PartialEq)]
10pub enum Error {
11    /// A model or distribution parameter is outside its valid domain.
12    InvalidParameter {
13        name: &'static str,
14        value: f64,
15        reason: &'static str,
16    },
17    /// A probability argument is NaN or outside `[0, 1]`.
18    InvalidProbability(f64),
19    /// A month number is outside 1 to 12.
20    InvalidMonth(u8),
21    /// Input data cannot be used as given: a missing or mistyped column, a
22    /// factor level not seen in training, mismatched lengths. The message
23    /// names what is wrong.
24    Data(String),
25}
26
27impl fmt::Display for Error {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::InvalidParameter {
31                name,
32                value,
33                reason,
34            } => write!(f, "invalid parameter {name} = {value}: {reason}"),
35            Self::InvalidProbability(p) => write!(f, "probability {p} is not in [0, 1]"),
36            Self::InvalidMonth(m) => write!(f, "month {m} is not in 1 to 12"),
37            Self::Data(message) => write!(f, "invalid data: {message}"),
38        }
39    }
40}
41
42impl std::error::Error for Error {}