Skip to main content

indicators/
error.rs

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