mozjpeg_rs/
error.rs

1//! Error types for the mozjpeg encoder.
2
3use std::fmt;
4
5/// Result type for mozjpeg operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error type for mozjpeg operations.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Invalid image dimensions (zero width or height)
13    InvalidDimensions {
14        /// Image width
15        width: u32,
16        /// Image height
17        height: u32,
18    },
19    /// Image buffer size doesn't match dimensions
20    BufferSizeMismatch {
21        /// Expected buffer size in bytes
22        expected: usize,
23        /// Actual buffer size in bytes
24        actual: usize,
25    },
26    /// Invalid quality value (must be 1-100)
27    InvalidQuality(u8),
28    /// Invalid quantization table index
29    InvalidQuantTableIndex(usize),
30    /// Invalid component index
31    InvalidComponentIndex(usize),
32    /// Invalid Huffman table index
33    InvalidHuffmanTableIndex(usize),
34    /// Invalid sampling factor
35    InvalidSamplingFactor {
36        /// Horizontal sampling factor
37        h: u8,
38        /// Vertical sampling factor
39        v: u8,
40    },
41    /// Invalid scan specification
42    InvalidScanSpec {
43        /// Reason for the invalid specification
44        reason: &'static str,
45    },
46    /// Invalid Huffman table structure
47    InvalidHuffmanTable,
48    /// Huffman code length overflow (exceeds max allowed)
49    HuffmanCodeLengthOverflow,
50    /// Unsupported color space
51    UnsupportedColorSpace,
52    /// Unsupported feature
53    UnsupportedFeature(&'static str),
54    /// Internal encoder error
55    InternalError(&'static str),
56    /// I/O error
57    IoError(String),
58    /// Memory allocation failed
59    AllocationFailed,
60}
61
62impl fmt::Display for Error {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        match self {
65            Error::InvalidDimensions { width, height } => {
66                write!(f, "Invalid image dimensions: {}x{}", width, height)
67            }
68            Error::BufferSizeMismatch { expected, actual } => {
69                write!(
70                    f,
71                    "Buffer size mismatch: expected {}, got {}",
72                    expected, actual
73                )
74            }
75            Error::InvalidQuality(q) => {
76                write!(f, "Invalid quality value: {} (must be 1-100)", q)
77            }
78            Error::InvalidQuantTableIndex(idx) => {
79                write!(f, "Invalid quantization table index: {}", idx)
80            }
81            Error::InvalidComponentIndex(idx) => {
82                write!(f, "Invalid component index: {}", idx)
83            }
84            Error::InvalidHuffmanTableIndex(idx) => {
85                write!(f, "Invalid Huffman table index: {}", idx)
86            }
87            Error::InvalidSamplingFactor { h, v } => {
88                write!(f, "Invalid sampling factor: {}x{}", h, v)
89            }
90            Error::InvalidScanSpec { reason } => {
91                write!(f, "Invalid scan specification: {}", reason)
92            }
93            Error::InvalidHuffmanTable => {
94                write!(f, "Invalid Huffman table structure")
95            }
96            Error::HuffmanCodeLengthOverflow => {
97                write!(f, "Huffman code length overflow (exceeds 16 bits)")
98            }
99            Error::UnsupportedColorSpace => {
100                write!(f, "Unsupported color space")
101            }
102            Error::UnsupportedFeature(feature) => {
103                write!(f, "Unsupported feature: {}", feature)
104            }
105            Error::InternalError(msg) => {
106                write!(f, "Internal encoder error: {}", msg)
107            }
108            Error::IoError(msg) => {
109                write!(f, "I/O error: {}", msg)
110            }
111            Error::AllocationFailed => {
112                write!(f, "Memory allocation failed")
113            }
114        }
115    }
116}
117
118impl std::error::Error for Error {}
119
120impl From<std::io::Error> for Error {
121    fn from(e: std::io::Error) -> Self {
122        Error::IoError(e.to_string())
123    }
124}
125
126impl From<std::collections::TryReserveError> for Error {
127    fn from(_: std::collections::TryReserveError) -> Self {
128        Error::AllocationFailed
129    }
130}