Skip to main content

shine_rs/
error.rs

1//! Error types for the MP3 encoder
2//!
3//! This module defines all error types used throughout the encoder,
4//! providing detailed error information for different failure scenarios.
5
6use thiserror::Error;
7
8/// Main error type for the MP3 encoder
9#[derive(Debug, Error)]
10pub enum EncoderError {
11    /// Configuration-related errors
12    #[error("Configuration error: {0}")]
13    Config(#[from] ConfigError),
14    
15    /// Input data validation errors
16    #[error("Input data error: {0}")]
17    InputData(#[from] InputDataError),
18    
19    /// Encoding process errors
20    #[error("Encoding error: {0}")]
21    Encoding(#[from] EncodingError),
22    
23    /// Memory allocation failures
24    #[error("Memory allocation error")]
25    Memory,
26    
27    /// Internal state consistency errors
28    #[error("Internal state error: {0}")]
29    InternalState(String),
30}
31
32/// Configuration validation errors
33#[derive(Debug, Error)]
34pub enum ConfigError {
35    /// Unsupported sample rate
36    #[error("Unsupported sample rate: {0} Hz")]
37    UnsupportedSampleRate(u32),
38    
39    /// Unsupported bitrate
40    #[error("Unsupported bitrate: {0} kbps")]
41    UnsupportedBitrate(u32),
42    
43    /// Invalid channel configuration
44    #[error("Invalid channel configuration")]
45    InvalidChannels,
46    
47    /// Incompatible sample rate and bitrate combination
48    #[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    /// Invalid stereo mode for channel count
56    #[error("Invalid stereo mode {mode:?} for {channels} channels")]
57    InvalidStereoMode { mode: String, channels: u8 },
58}
59
60/// Input data validation errors
61#[derive(Debug, Error)]
62pub enum InputDataError {
63    /// Invalid PCM data length
64    #[error("Invalid PCM data length: expected {expected} samples, got {actual}")]
65    InvalidLength { expected: usize, actual: usize },
66    
67    /// Invalid channel count in PCM data
68    #[error("Invalid channel count in PCM data: expected {expected}, got {actual}")]
69    InvalidChannelCount { expected: usize, actual: usize },
70    
71    /// PCM data contains invalid samples
72    #[error("PCM data contains invalid samples")]
73    InvalidSamples,
74    
75    /// Empty input data
76    #[error("Empty input data provided")]
77    EmptyInput,
78}
79
80/// Encoding process errors
81#[derive(Debug, Error)]
82pub enum EncodingError {
83    /// Quantization loop failed to converge
84    #[error("Quantization loop failed to converge within maximum iterations")]
85    QuantizationFailed,
86    
87    /// Huffman encoding error
88    #[error("Huffman encoding error: {0}")]
89    HuffmanError(String),
90    
91    /// Bitstream writing error
92    #[error("Bitstream writing error: {0}")]
93    BitstreamError(String),
94    
95    /// MDCT transform error
96    #[error("MDCT transform error: {0}")]
97    MdctError(String),
98    
99    /// Subband filter error
100    #[error("Subband filter error: {0}")]
101    SubbandError(String),
102    
103    /// Invalid input length for processing
104    #[error("Invalid input length: expected {expected} samples, got {actual}")]
105    InvalidInputLength { expected: usize, actual: usize },
106    
107    /// Invalid data length for processing
108    #[error("Invalid data length: expected {expected}, got {actual}")]
109    InvalidDataLength { expected: usize, actual: usize },
110    
111    /// Invalid channel index
112    #[error("Invalid channel index {channel}: maximum supported channels is {max_channels}")]
113    InvalidChannelIndex { channel: usize, max_channels: usize },
114    
115    /// Bit reservoir overflow
116    #[error("Bit reservoir overflow: attempted to use {requested} bits, only {available} available")]
117    BitReservoirOverflow { requested: usize, available: usize },
118    
119    /// Validation error for testing and verification
120    #[error("Validation error: {0}")]
121    ValidationError(String),
122    
123    /// Debug stop after specified number of frames
124    #[error("Debug stop after frames")]
125    StopAfterFrames,
126}
127
128/// Specialized result types for different modules
129pub 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
133/// Convert EncoderError to EncodingError for verification purposes
134impl 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}