Skip to main content

lmn_core/threshold/
error.rs

1/// Errors that can occur during threshold parsing and evaluation.
2#[derive(Debug)]
3pub enum ThresholdError {
4    /// Input could not be parsed as valid JSON or YAML.
5    ParseError(String),
6    /// Input was parseable but failed validation (e.g. out-of-range values).
7    ValidationError(String),
8}
9
10impl std::fmt::Display for ThresholdError {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        match self {
13            Self::ParseError(msg) => write!(f, "Threshold parse error: {}", msg),
14            Self::ValidationError(msg) => write!(f, "Threshold validation error: {}", msg),
15        }
16    }
17}
18
19impl std::error::Error for ThresholdError {}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn display_parse_error() {
27        let e = ThresholdError::ParseError("bad json".to_string());
28        assert!(e.to_string().contains("bad json"));
29    }
30
31    #[test]
32    fn display_validation_error() {
33        let e = ThresholdError::ValidationError("out of range".to_string());
34        assert!(e.to_string().contains("out of range"));
35    }
36}