use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DecodeError {
NotWebp,
Truncated,
NoImage,
MalformedChunk {
fourcc: [u8; 4],
},
TooLarge {
width: u32,
height: u32,
},
LosslessHeader,
InvalidPrefixCode,
RepeatedTransform,
InvalidColorCache,
InvalidBackwardReference,
LossyHeader,
InvalidPartitions,
InvalidAlpha,
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotWebp => f.write_str("not a WebP file"),
Self::Truncated => f.write_str("the WebP data ends early"),
Self::NoImage => f.write_str("the WebP file holds no image"),
Self::MalformedChunk { fourcc } => {
write!(
f,
"malformed WebP {} chunk",
String::from_utf8_lossy(fourcc)
)
}
Self::TooLarge { width, height } => {
write!(f, "a {width} × {height} WebP image is too large to decode")
}
Self::LosslessHeader => f.write_str("invalid VP8L header"),
Self::InvalidPrefixCode => f.write_str("invalid prefix code in VP8L data"),
Self::RepeatedTransform => f.write_str("a VP8L transform appears twice"),
Self::InvalidColorCache => f.write_str("invalid VP8L colour cache size"),
Self::InvalidBackwardReference => {
f.write_str("a VP8L backward reference leaves the image")
}
Self::LossyHeader => f.write_str("invalid VP8 frame header"),
Self::InvalidPartitions => f.write_str("VP8 partitions exceed the data"),
Self::InvalidAlpha => f.write_str("invalid WebP alpha data"),
}
}
}
impl std::error::Error for DecodeError {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EncodeError {
InvalidDimensions {
width: u32,
height: u32,
},
DataLengthMismatch {
expected: usize,
actual: usize,
},
NoFrames,
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidDimensions { width, height } => write!(
f,
"a WebP cannot be {width} × {height} pixels (each side must be 1 to 16384)"
),
Self::DataLengthMismatch { expected, actual } => {
write!(f, "{actual} bytes of pixel data, expected {expected}")
}
Self::NoFrames => f.write_str("an animation needs at least one frame"),
}
}
}
impl std::error::Error for EncodeError {}