use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum TechnicalIndicatorError {
EmptyData { name: String },
MismatchedLength { names: Vec<(String, usize)> },
InvalidPeriod {
period: usize,
data_len: usize,
reason: String,
},
InvalidValue {
name: String,
value: f64,
reason: String,
},
UnsupportedType { type_name: String },
Custom { message: String },
}
impl fmt::Display for TechnicalIndicatorError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TechnicalIndicatorError::EmptyData { name } => {
write!(f, "{} cannot be empty", name)
}
TechnicalIndicatorError::MismatchedLength { names } => {
write!(f, "Mismatched lengths: ")?;
for (i, (name, len)) in names.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}={}", name, len)?;
}
Ok(())
}
TechnicalIndicatorError::InvalidPeriod {
period,
data_len,
reason,
} => {
write!(
f,
"Invalid period {}: {} (data length: {})",
period, reason, data_len
)
}
TechnicalIndicatorError::InvalidValue {
name,
value,
reason,
} => {
write!(f, "Invalid value for {}: {} ({})", name, value, reason)
}
TechnicalIndicatorError::UnsupportedType { type_name } => {
write!(f, "Unsupported type: {}", type_name)
}
TechnicalIndicatorError::Custom { message } => {
write!(f, "{}", message)
}
}
}
}
impl std::error::Error for TechnicalIndicatorError {}
pub type Result<T> = std::result::Result<T, TechnicalIndicatorError>;