qoi/
error.rs

1use core::convert::Infallible;
2use core::fmt::{self, Display};
3
4use crate::consts::QOI_MAGIC;
5
6/// Errors that can occur during encoding or decoding.
7#[derive(Debug)]
8pub enum Error {
9    /// Leading 4 magic bytes don't match when decoding
10    InvalidMagic { magic: u32 },
11    /// Invalid number of channels: expected 3 or 4
12    InvalidChannels { channels: u8 },
13    /// Invalid color space: expected 0 or 1
14    InvalidColorSpace { colorspace: u8 },
15    /// Invalid image dimensions: can't be empty or larger than 400Mp
16    InvalidImageDimensions { width: u32, height: u32 },
17    /// Image dimensions are inconsistent with image buffer length
18    InvalidImageLength { size: usize, width: u32, height: u32 },
19    /// Output buffer is too small to fit encoded/decoded image
20    OutputBufferTooSmall { size: usize, required: usize },
21    /// Input buffer ended unexpectedly before decoding was finished
22    UnexpectedBufferEnd,
23    /// Invalid stream end marker encountered when decoding
24    InvalidPadding,
25    #[cfg(feature = "std")]
26    /// Generic I/O error from the wrapped reader/writer
27    IoError(std::io::Error),
28}
29
30/// Alias for [`Result`](std::result::Result) with the error type of [`Error`].
31pub type Result<T> = core::result::Result<T, Error>;
32
33impl Display for Error {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match *self {
36            Self::InvalidMagic { magic } => {
37                write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic.to_be_bytes())
38            }
39            Self::InvalidChannels { channels } => {
40                write!(f, "invalid number of channels: {}", channels)
41            }
42            Self::InvalidColorSpace { colorspace } => {
43                write!(f, "invalid color space: {} (expected 0 or 1)", colorspace)
44            }
45            Self::InvalidImageDimensions { width, height } => {
46                write!(f, "invalid image dimensions: {}x{}", width, height)
47            }
48            Self::InvalidImageLength { size, width, height } => {
49                write!(f, "invalid image length: {} bytes for {}x{}", size, width, height)
50            }
51            Self::OutputBufferTooSmall { size, required } => {
52                write!(f, "output buffer size too small: {} (required: {})", size, required)
53            }
54            Self::UnexpectedBufferEnd => {
55                write!(f, "unexpected input buffer end while decoding")
56            }
57            Self::InvalidPadding => {
58                write!(f, "invalid padding (stream end marker mismatch)")
59            }
60            #[cfg(feature = "std")]
61            Self::IoError(ref err) => {
62                write!(f, "i/o error: {}", err)
63            }
64        }
65    }
66}
67
68#[cfg(feature = "std")]
69impl std::error::Error for Error {}
70
71impl From<Infallible> for Error {
72    fn from(_: Infallible) -> Self {
73        unreachable!()
74    }
75}
76
77#[cfg(feature = "std")]
78impl From<std::io::Error> for Error {
79    fn from(err: std::io::Error) -> Self {
80        Self::IoError(err)
81    }
82}