1use core::fmt;
2
3#[derive(Debug, Clone, PartialEq)]
5pub enum StreamError {
6 EmptyInput,
8
9 SampleRateMismatch {
11 expected: u32,
13 got: u32,
15 },
16
17 ChannelMismatch {
19 expected: u16,
21 got: u16,
23 },
24
25 ProcessingError(alloc::string::String),
27}
28
29extern crate alloc;
30
31impl fmt::Display for StreamError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 Self::EmptyInput => write!(f, "input chunk is empty"),
35 Self::SampleRateMismatch { expected, got } => {
36 write!(
37 f,
38 "sample rate mismatch: expected {expected} Hz, got {got} Hz"
39 )
40 }
41 Self::ChannelMismatch { expected, got } => {
42 write!(f, "channel count mismatch: expected {expected}, got {got}")
43 }
44 Self::ProcessingError(msg) => write!(f, "processing error: {msg}"),
45 }
46 }
47}
48
49impl std::error::Error for StreamError {}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn display_empty_input() {
57 let err = StreamError::EmptyInput;
58 assert_eq!(err.to_string(), "input chunk is empty");
59 }
60
61 #[test]
62 fn display_sample_rate_mismatch() {
63 let err = StreamError::SampleRateMismatch {
64 expected: 44100,
65 got: 48000,
66 };
67 assert_eq!(
68 err.to_string(),
69 "sample rate mismatch: expected 44100 Hz, got 48000 Hz"
70 );
71 }
72
73 #[test]
74 fn display_channel_mismatch() {
75 let err = StreamError::ChannelMismatch {
76 expected: 2,
77 got: 1,
78 };
79 assert_eq!(err.to_string(), "channel count mismatch: expected 2, got 1");
80 }
81
82 #[test]
83 fn display_processing_error() {
84 let err = StreamError::ProcessingError("FFT failed".into());
85 assert_eq!(err.to_string(), "processing error: FFT failed");
86 }
87
88 #[test]
89 fn clone_and_eq() {
90 let a = StreamError::EmptyInput;
91 let b = a.clone();
92 assert_eq!(a, b);
93 }
94}