use std::fmt;
use crate::fmt::BitDepth;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EncodeError {
InvalidDimensions { width: u32, height: u32 },
InvalidQuality(u8),
BufferSize { expected: usize, found: usize },
SampleOutOfRange { value: u16, depth: BitDepth },
Unsupported(&'static str),
Decode(&'static str),
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EncodeError::InvalidDimensions { width, height } => {
write!(f, "invalid image dimensions: {width}x{height}")
}
EncodeError::InvalidQuality(q) => {
write!(f, "quality {q} out of range (expected 1..=100)")
}
EncodeError::BufferSize { expected, found } => {
write!(
f,
"pixel buffer size mismatch: expected {expected} bytes, found {found}"
)
}
EncodeError::SampleOutOfRange { value, depth } => {
write!(
f,
"sample {value} exceeds maximum {} for {}-bit depth",
depth.max_val(),
depth.bits()
)
}
EncodeError::Unsupported(what) => write!(f, "unsupported: {what}"),
EncodeError::Decode(what) => write!(f, "decode error: {what}"),
}
}
}
impl std::error::Error for EncodeError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_is_human_readable() {
let e = EncodeError::InvalidDimensions {
width: 0,
height: 10,
};
assert!(e.to_string().contains("0x10"));
let e = EncodeError::InvalidQuality(200);
assert!(e.to_string().contains("200"));
}
}