Skip to main content

indicators/
error.rs

1use std::error::Error;
2use std::fmt;
3
4// ── Error ────────────────────────────────────────────────────────────────────
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum IndicatorError {
8    InsufficientData {
9        required: usize,
10        available: usize,
11    },
12    InvalidParameter {
13        name: String,
14        value: f64,
15    },
16    /// Returned by the registry when `name` is not registered.
17    /// Mirrors Python `IndicatorFactory`: `raise ValueError(f"Indicator not found: {name}")`.
18    UnknownIndicator {
19        name: String,
20    },
21    /// General construction-time validation failure (bad param combination, etc.).
22    InvalidParam(String),
23}
24
25impl fmt::Display for IndicatorError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            IndicatorError::InsufficientData {
29                required,
30                available,
31            } => write!(
32                f,
33                "Insufficient data: required {required} candles, but only {available} available"
34            ),
35            IndicatorError::InvalidParameter { name, value } => {
36                write!(f, "Invalid parameter {name}: {value}")
37            }
38            IndicatorError::UnknownIndicator { name } => {
39                write!(f, "Unknown indicator: '{name}'")
40            }
41            IndicatorError::InvalidParam(msg) => {
42                write!(f, "Invalid parameter: {msg}")
43            }
44        }
45    }
46}
47
48impl Error for IndicatorError {}