lasprs 0.14.1

Library for Acoustic Signal Processing (Rust edition, with optional Python bindings via pyo3)
//! Error types and enums for the power spectra module.
use crate::config::*;
use snafu::prelude::*;
use std::fmt::{Display, Formatter};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter, EnumMessage};

/// Errors that can occur during frequency-domain smoothing of power spectra.
#[cfg_attr(
    feature = "python-bindings",
    gen_stub_pyclass_complex_enum,
    pyclass(from_py_object)
)]
#[derive(Debug, Snafu, Clone, PartialEq)]
#[snafu(visibility(pub))]
#[allow(missing_docs)]
pub enum FreqSmoothError {
    /// The frequency vector is too short (must have at least 2 elements).
    #[snafu(display("Invalid frequency vector length {length}. Must be >= 2"))]
    FreqTooShort {
        /// Actual length of the frequency vector.
        length: usize,
    },

    /// The frequency and spectrum vectors have different lengths.
    #[snafu(display(
        "Frequency vector length ({freq_len}) does not match spectrum vector length ({x_len})"
    ))]
    SizeMismatch {
        /// Length of the frequency vector.
        freq_len: usize,
        /// Length of the spectrum vector.
        x_len: usize,
    },

    /// The minimum non-DC frequency is not strictly positive, which prevents
    /// construction of a logarithmic frequency grid.
    #[snafu(display(
        "Minimum non-DC frequency ({freq_min}) must be strictly positive for log-scale interpolation"
    ))]
    InvalidFreqMin {
        /// The offending frequency value.
        freq_min: Flt,
    },

    /// An error occurred during interpolation (e.g. from `ndarray-interp`).
    #[snafu(display("Interpolation failed: {msg}"))]
    InterpolationFailed {
        /// Description of the interpolation failure.
        msg: String,
    },

    /// Power spectrum values must be non-negative.
    #[snafu(display("Power spectrum contains negative values (minimum: {min_value})"))]
    NegativePower {
        /// The minimum (negative) value found in the spectrum.
        min_value: Flt,
    },

    /// Decibel (levels) input must be real-valued, but complex data was given.
    #[snafu(display("Decibel (levels) input must be real-valued, not complex"))]
    ComplexLevels {},
}

#[cfg(feature = "python-bindings")]
#[gen_stub_pymethods]
#[pymethods]
impl FreqSmoothError {
    fn __str__(&self) -> String {
        self.to_string()
    }
}

cfg_select! {
    any(feature = "python-bindings") => {
        use pyo3::exceptions::PyException;

        #[gen_stub_pyclass]
        #[pyclass(extends = PyException, str, from_py_object)]
        /// Wrapper for FreqSmoothError, to make it a struct, not enum, as enums
        /// cannot be extended from PyBaseException.
        #[derive(Debug, Clone)]
        pub struct PyFreqSmoothError {
            /// The inner error
            #[pyo3(get)]
            pub inner: FreqSmoothError,
        }

        impl Display for PyFreqSmoothError {
            fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.inner)
            }
        }

        #[gen_stub_pymethods]
        #[pymethods]
        impl PyFreqSmoothError {
            /// Create a new PyFreqSmoothError from a FreqSmoothError
            #[new]
            fn new(inner: FreqSmoothError) -> Self {
                PyFreqSmoothError { inner }
            }
        }

        impl From<FreqSmoothError> for PyErr {
            fn from(value: FreqSmoothError) -> Self {
                PyErr::new::<PyFreqSmoothError, _>(value)
            }
        }
    }
    _ => {}
}