Skip to main content

numra_signal/
error.rs

1//! Error types for signal processing.
2//!
3//! Author: Moussa Leblouba
4//! Date: 9 February 2026
5//! Modified: 2 May 2026
6
7use core::fmt;
8use numra_core::NumraError;
9
10/// Errors that can occur in signal processing operations.
11#[derive(Clone, Debug, PartialEq)]
12pub enum SignalError {
13    /// Filter order must be positive.
14    InvalidOrder(usize),
15    /// Cutoff frequency must be in (0, fs/2).
16    InvalidCutoff { cutoff: f64, nyquist: f64 },
17    /// Ripple must be positive.
18    InvalidRipple(f64),
19    /// Input signal is empty or too short.
20    InsufficientData { need: usize, got: usize },
21    /// Invalid parameter.
22    InvalidParameter(String),
23}
24
25impl fmt::Display for SignalError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::InvalidOrder(n) => write!(f, "invalid filter order: {n}"),
29            Self::InvalidCutoff { cutoff, nyquist } => {
30                write!(f, "cutoff {cutoff} must be in (0, {nyquist})")
31            }
32            Self::InvalidRipple(r) => write!(f, "ripple must be positive, got {r}"),
33            Self::InsufficientData { need, got } => {
34                write!(f, "need at least {need} samples, got {got}")
35            }
36            Self::InvalidParameter(msg) => write!(f, "invalid parameter: {msg}"),
37        }
38    }
39}
40
41impl std::error::Error for SignalError {}
42
43impl From<SignalError> for NumraError {
44    fn from(e: SignalError) -> Self {
45        NumraError::Signal(e.to_string())
46    }
47}