Skip to main content

beatwise/config/
errors.rs

1/// A task configuration error.
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum ConfigError {
4    EmptyName,
5    ZeroInterval,
6    ZeroStartupGrace,
7    TooFewLearningSamples,
8    InvalidSensitivity,
9    InvalidRobustWindowCapacity,
10    InvalidPatternMaximumPeriod,
11    InvalidPatternMinimumCycles,
12    InvalidPatternTolerance,
13    InvalidPatternContrast,
14}
15
16impl fmt::Display for ConfigError {
17    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Self::EmptyName => formatter.write_str("task name cannot be empty"),
20            Self::ZeroInterval => formatter.write_str("fixed interval cannot be zero"),
21            Self::ZeroStartupGrace => formatter.write_str("startup grace cannot be zero"),
22            Self::TooFewLearningSamples => {
23                formatter.write_str("learned timing requires at least two interval samples")
24            }
25            Self::InvalidSensitivity => {
26                formatter.write_str("learned timing sensitivity must be finite and positive")
27            }
28            Self::InvalidRobustWindowCapacity => formatter.write_str(
29                "robust learning window capacity must be an odd number from 5 through 31",
30            ),
31            Self::InvalidPatternMaximumPeriod => {
32                formatter.write_str("pattern maximum period must be from 2 through 8")
33            }
34            Self::InvalidPatternMinimumCycles => {
35                formatter.write_str("pattern minimum cycles must be from 3 through 8")
36            }
37            Self::InvalidPatternTolerance => {
38                formatter.write_str("pattern tolerance must be from 1 through 50 percent")
39            }
40            Self::InvalidPatternContrast => formatter
41                .write_str("pattern minimum contrast must be from 1 through 100 percent"),
42        }
43    }
44}
45
46impl Error for ConfigError {}
47
48/// A task registration error.
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum RegisterError {
51    InvalidConfig(ConfigError),
52    IdSpaceExhausted,
53}
54
55impl fmt::Display for RegisterError {
56    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
57        match self {
58            Self::InvalidConfig(error) => write!(formatter, "invalid task configuration: {error}"),
59            Self::IdSpaceExhausted => formatter.write_str("task ID space exhausted"),
60        }
61    }
62}
63
64impl Error for RegisterError {
65    fn source(&self) -> Option<&(dyn Error + 'static)> {
66        match self {
67            Self::InvalidConfig(error) => Some(error),
68            Self::IdSpaceExhausted => None,
69        }
70    }
71}
72
73impl From<ConfigError> for RegisterError {
74    fn from(error: ConfigError) -> Self {
75        Self::InvalidConfig(error)
76    }
77}
78
79/// Returned when a learned task cannot be retrained.
80#[derive(Clone, Copy, Debug, PartialEq, Eq)]
81pub enum RetrainError {
82    /// No retained task exists for the supplied identifier.
83    UnknownTask,
84
85    /// Fixed timing has no learned baseline to reset.
86    FixedTiming,
87
88    /// Stopped tasks cannot re-enter learning.
89    Stopped,
90}
91
92impl fmt::Display for RetrainError {
93    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
94        match self {
95            Self::UnknownTask => formatter.write_str("task is not registered"),
96            Self::FixedTiming => formatter.write_str("fixed timing cannot be retrained"),
97            Self::Stopped => formatter.write_str("stopped task cannot be retrained"),
98        }
99    }
100}
101
102impl Error for RetrainError {}