Skip to main content

ebur128_stream/
error.rs

1//! Error type for the crate.
2//!
3//! A single global enum carries every failure mode (per `SPEC.md` ยง15). New
4//! variants may be added without a major version bump; the enum is
5//! `#[non_exhaustive]`.
6
7use core::fmt;
8
9/// Errors returned from analyzer construction or sample ingestion.
10#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub enum Error {
14    /// The requested sample rate is not one of the supported rates
15    /// (22 050, 32 000, 44 100, 48 000, 88 200, 96 000, 192 000 Hz).
16    InvalidSampleRate {
17        /// The unsupported rate that was supplied.
18        hz: u32,
19    },
20    /// The number of channels in a `push` call did not match the layout
21    /// the analyzer was built with.
22    ChannelMismatch {
23        /// Channels expected (from the configured layout).
24        expected: usize,
25        /// Channels actually supplied.
26        got: usize,
27    },
28    /// The configured channel layout was empty or exceeded the supported
29    /// maximum.
30    InvalidChannelLayout {
31        /// The number of channels supplied.
32        got: usize,
33        /// The maximum supported (currently 8).
34        max: usize,
35    },
36    /// `AnalyzerBuilder::build` was called without selecting any [`Mode`].
37    ///
38    /// At least one mode must be requested.
39    ///
40    /// [`Mode`]: crate::Mode
41    NoModesSelected,
42    /// `push_interleaved` was called with a buffer whose length is not a
43    /// whole multiple of the channel count.
44    InterleavedLengthNotMultiple {
45        /// Number of samples actually supplied.
46        samples: usize,
47        /// Configured channel count.
48        channels: usize,
49    },
50    /// `push_planar` was called with channel slices of differing length.
51    PlanarLengthMismatch {
52        /// Length of the first slice supplied.
53        first: usize,
54        /// Length of the offending later slice.
55        got: usize,
56    },
57    /// Loudness Range (`Mode::Lra`) requires the `alloc` feature, which is
58    /// not enabled in this build.
59    LraRequiresAlloc,
60    /// Integrated loudness (`Mode::Integrated`) requires the `alloc`
61    /// feature, which is not enabled in this build.
62    IntegratedRequiresAlloc,
63    /// A sample value was non-finite (NaN or infinity). The analyzer
64    /// rejects these rather than silently propagating; sanitize at the
65    /// caller.
66    NonFiniteSample,
67}
68
69impl fmt::Display for Error {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Error::InvalidSampleRate { hz } => write!(
73                f,
74                "invalid sample rate {hz} Hz; supported: 22050, 32000, 44100, 48000, 88200, 96000, 192000"
75            ),
76            Error::ChannelMismatch { expected, got } => {
77                write!(f, "channel count mismatch: expected {expected}, got {got}")
78            }
79            Error::InvalidChannelLayout { got, max } => {
80                write!(f, "invalid channel layout: {got} channels (max {max})")
81            }
82            Error::NoModesSelected => write!(f, "no analysis modes selected"),
83            Error::InterleavedLengthNotMultiple { samples, channels } => write!(
84                f,
85                "interleaved buffer length {samples} is not a multiple of channel count {channels}"
86            ),
87            Error::PlanarLengthMismatch { first, got } => write!(
88                f,
89                "planar channel slices have differing lengths: first {first}, got {got}"
90            ),
91            Error::LraRequiresAlloc => {
92                write!(f, "Mode::Lra requires the `alloc` feature")
93            }
94            Error::IntegratedRequiresAlloc => {
95                write!(f, "Mode::Integrated requires the `alloc` feature")
96            }
97            Error::NonFiniteSample => write!(f, "sample buffer contained NaN or infinity"),
98        }
99    }
100}
101
102#[cfg(feature = "std")]
103impl std::error::Error for Error {}