Coverage Report

Created: 2025-07-01 06:50

/rust/registry/src/index.crates.io-6f17d22bba15001f/qoi-0.4.1/src/error.rs
Line
Count
Source (jump to first uncovered line)
1
use core::convert::Infallible;
2
use core::fmt::{self, Display};
3
4
use crate::consts::QOI_MAGIC;
5
6
/// Errors that can occur during encoding or decoding.
7
#[derive(Debug)]
8
pub 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`].
31
pub type Result<T> = core::result::Result<T, Error>;
32
33
impl Display for Error {
34
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35
0
        match *self {
36
0
            Self::InvalidMagic { magic } => {
37
0
                write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic.to_be_bytes())
38
            }
39
0
            Self::InvalidChannels { channels } => {
40
0
                write!(f, "invalid number of channels: {}", channels)
41
            }
42
0
            Self::InvalidColorSpace { colorspace } => {
43
0
                write!(f, "invalid color space: {} (expected 0 or 1)", colorspace)
44
            }
45
0
            Self::InvalidImageDimensions { width, height } => {
46
0
                write!(f, "invalid image dimensions: {}x{}", width, height)
47
            }
48
0
            Self::InvalidImageLength { size, width, height } => {
49
0
                write!(f, "invalid image length: {} bytes for {}x{}", size, width, height)
50
            }
51
0
            Self::OutputBufferTooSmall { size, required } => {
52
0
                write!(f, "output buffer size too small: {} (required: {})", size, required)
53
            }
54
            Self::UnexpectedBufferEnd => {
55
0
                write!(f, "unexpected input buffer end while decoding")
56
            }
57
            Self::InvalidPadding => {
58
0
                write!(f, "invalid padding (stream end marker mismatch)")
59
            }
60
            #[cfg(feature = "std")]
61
0
            Self::IoError(ref err) => {
62
0
                write!(f, "i/o error: {}", err)
63
            }
64
        }
65
0
    }
66
}
67
68
#[cfg(feature = "std")]
69
impl std::error::Error for Error {}
70
71
impl From<Infallible> for Error {
72
0
    fn from(_: Infallible) -> Self {
73
0
        unreachable!()
74
    }
75
}
76
77
#[cfg(feature = "std")]
78
impl From<std::io::Error> for Error {
79
0
    fn from(err: std::io::Error) -> Self {
80
0
        Self::IoError(err)
81
0
    }
82
}