use std::fmt;
use valo::ImageError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DecodeError {
NoDecoder,
Unsupported(Vec<Declined>),
InvalidData(String),
LimitExceeded(&'static str),
Failed(String),
Image(ImageError),
Closed,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Declined {
pub decoder: &'static str,
pub reason: String,
}
impl From<ImageError> for DecodeError {
fn from(error: ImageError) -> Self {
Self::Image(error)
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoDecoder => f.write_str("no image decoder is registered"),
Self::Unsupported(declined) => {
f.write_str("no registered decoder reads this image")?;
for entry in declined {
write!(f, "; {}: {}", entry.decoder, entry.reason)?;
}
Ok(())
}
Self::InvalidData(reason) => write!(f, "damaged image: {reason}"),
Self::LimitExceeded(limit) => write!(f, "decode limit exceeded: {limit}"),
Self::Failed(reason) => write!(f, "decode failed: {reason}"),
Self::Image(error) => write!(f, "image creation failed: {error}"),
Self::Closed => f.write_str("the image loader was closed before decoding"),
}
}
}
impl std::error::Error for DecodeError {}