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(
117 "Bit reservoir overflow: attempted to use {requested} bits, only {available} available"
118 )]
119 BitReservoirOverflow { requested: usize, available: usize },
120
121 #[error("Validation error: {0}")]
123 ValidationError(String),
124}
125
126pub type ConfigResult<T> = std::result::Result<T, ConfigError>;
128pub type InputResult<T> = std::result::Result<T, InputDataError>;
129pub type EncodingResult<T> = std::result::Result<T, EncodingError>;
130
131impl From<EncoderError> for EncodingError {
133 fn from(err: EncoderError) -> Self {
134 match err {
135 EncoderError::Config(config_err) => {
136 EncodingError::ValidationError(format!("Config error: {}", config_err))
137 }
138 EncoderError::InputData(input_err) => {
139 EncodingError::ValidationError(format!("Input error: {}", input_err))
140 }
141 EncoderError::Encoding(encoding_err) => encoding_err,
142 EncoderError::Memory => EncodingError::ValidationError("Memory error".to_string()),
143 EncoderError::InternalState(msg) => {
144 EncodingError::ValidationError(format!("Internal state error: {}", msg))
145 }
146 }
147 }
148}