1use thiserror::Error;
7
8#[derive(Debug, Error)]
10pub enum EncoderError {
11 #[error("Configuration error: {0}")]
13 Config(#[from] ConfigError),
14
15 #[error("Input data error: {0}")]
17 InputData(#[from] InputDataError),
18
19 #[error("Encoding error: {0}")]
21 Encoding(#[from] EncodingError),
22
23 #[error("Memory allocation error")]
25 Memory,
26
27 #[error("Internal state error: {0}")]
29 InternalState(String),
30}
31
32#[derive(Debug, Error)]
34pub enum ConfigError {
35 #[error("Unsupported sample rate: {0} Hz")]
37 UnsupportedSampleRate(u32),
38
39 #[error("Unsupported bitrate: {0} kbps")]
41 UnsupportedBitrate(u32),
42
43 #[error("Invalid channel configuration")]
45 InvalidChannels,
46
47 #[error("Incompatible sample rate ({sample_rate} Hz) and bitrate ({bitrate} kbps) combination: {reason}")]
49 IncompatibleRateCombination {
50 sample_rate: u32,
51 bitrate: u32,
52 reason: String,
53 },
54
55 #[error("Invalid stereo mode {mode:?} for {channels} channels")]
57 InvalidStereoMode { mode: String, channels: u8 },
58}
59
60#[derive(Debug, Error)]
62pub enum InputDataError {
63 #[error("Invalid PCM data length: expected {expected} samples, got {actual}")]
65 InvalidLength { expected: usize, actual: usize },
66
67 #[error("Invalid channel count in PCM data: expected {expected}, got {actual}")]
69 InvalidChannelCount { expected: usize, actual: usize },
70
71 #[error("PCM data contains invalid samples")]
73 InvalidSamples,
74
75 #[error("Empty input data provided")]
77 EmptyInput,
78}
79
80#[derive(Debug, Error)]
82pub enum EncodingError {
83 #[error("Quantization loop failed to converge within maximum iterations")]
85 QuantizationFailed,
86
87 #[error("Huffman encoding error: {0}")]
89 HuffmanError(String),
90
91 #[error("Bitstream writing error: {0}")]
93 BitstreamError(String),
94
95 #[error("MDCT transform error: {0}")]
97 MdctError(String),
98
99 #[error("Subband filter error: {0}")]
101 SubbandError(String),
102
103 #[error("Invalid input length: expected {expected} samples, got {actual}")]
105 InvalidInputLength { expected: usize, actual: usize },
106
107 #[error("Invalid data length: expected {expected}, got {actual}")]
109 InvalidDataLength { expected: usize, actual: usize },
110
111 #[error("Invalid channel index {channel}: maximum supported channels is {max_channels}")]
113 InvalidChannelIndex { channel: usize, max_channels: usize },
114
115 #[error("Bit reservoir overflow: attempted to use {requested} bits, only {available} available")]
117 BitReservoirOverflow { requested: usize, available: usize },
118
119 #[error("Validation error: {0}")]
121 ValidationError(String),
122
123 #[error("Debug stop after frames")]
125 StopAfterFrames,
126}
127
128pub type ConfigResult<T> = std::result::Result<T, ConfigError>;
130pub type InputResult<T> = std::result::Result<T, InputDataError>;
131pub type EncodingResult<T> = std::result::Result<T, EncodingError>;
132
133impl From<EncoderError> for EncodingError {
135 fn from(err: EncoderError) -> Self {
136 match err {
137 EncoderError::Config(config_err) => EncodingError::ValidationError(format!("Config error: {}", config_err)),
138 EncoderError::InputData(input_err) => EncodingError::ValidationError(format!("Input error: {}", input_err)),
139 EncoderError::Encoding(encoding_err) => encoding_err,
140 EncoderError::Memory => EncodingError::ValidationError("Memory error".to_string()),
141 EncoderError::InternalState(msg) => EncodingError::ValidationError(format!("Internal state error: {}", msg)),
142 }
143 }
144}