1use std::fmt;
3use valo::ImageError;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
7pub enum DecodeError {
8 NoDecoder,
10
11 Unsupported(Vec<Declined>),
13
14 InvalidData(String),
16
17 LimitExceeded(&'static str),
20
21 Failed(String),
24
25 Image(ImageError),
27
28 Closed,
30}
31
32#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct Declined {
35 pub decoder: &'static str,
37
38 pub reason: String,
40}
41
42impl From<ImageError> for DecodeError {
43 fn from(error: ImageError) -> Self {
44 Self::Image(error)
45 }
46}
47
48impl fmt::Display for DecodeError {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 match self {
51 Self::NoDecoder => f.write_str("no image decoder is registered"),
52 Self::Unsupported(declined) => {
53 f.write_str("no registered decoder reads this image")?;
54 for entry in declined {
55 write!(f, "; {}: {}", entry.decoder, entry.reason)?;
56 }
57 Ok(())
58 }
59 Self::InvalidData(reason) => write!(f, "damaged image: {reason}"),
60 Self::LimitExceeded(limit) => write!(f, "decode limit exceeded: {limit}"),
61 Self::Failed(reason) => write!(f, "decode failed: {reason}"),
62 Self::Image(error) => write!(f, "image creation failed: {error}"),
63 Self::Closed => f.write_str("the image loader was closed before decoding"),
64 }
65 }
66}
67
68impl std::error::Error for DecodeError {}