2022-01-03 14:14:01 +00:00
|
|
|
use core::convert::Infallible;
|
|
|
|
use core::fmt::{self, Display};
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2022-01-03 18:12:06 +00:00
|
|
|
use crate::consts::QOI_MAGIC;
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2022-01-03 18:12:06 +00:00
|
|
|
/// Errors that can occur during encoding or decoding.
|
2022-01-01 21:03:47 +00:00
|
|
|
#[derive(Debug)]
|
2021-11-28 12:36:12 +00:00
|
|
|
pub enum Error {
|
2022-01-03 19:10:03 +00:00
|
|
|
/// Leading 4 magic bytes don't match when decoding
|
|
|
|
InvalidMagic { magic: u32 },
|
|
|
|
/// Invalid number of channels: expected 3 or 4
|
|
|
|
InvalidChannels { channels: u8 },
|
|
|
|
/// Invalid color space: expected 0 or 1
|
|
|
|
InvalidColorSpace { colorspace: u8 },
|
|
|
|
/// Invalid image dimensions: can't be empty or larger than 400Mp
|
|
|
|
InvalidImageDimensions { width: u32, height: u32 },
|
|
|
|
/// Image dimensions are inconsistent with image buffer length
|
|
|
|
InvalidImageLength { size: usize, width: u32, height: u32 },
|
|
|
|
/// Output buffer is too small to fit encoded/decoded image
|
|
|
|
OutputBufferTooSmall { size: usize, required: usize },
|
|
|
|
/// Input buffer ended unexpectedly before decoding was finished
|
2021-12-01 17:13:50 +00:00
|
|
|
UnexpectedBufferEnd,
|
2022-01-03 19:10:03 +00:00
|
|
|
/// Invalid stream end marker encountered when decoding
|
2022-01-01 21:02:08 +00:00
|
|
|
InvalidPadding,
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-01-03 19:10:03 +00:00
|
|
|
/// Generic I/O error from the wrapped reader/writer
|
2022-01-03 14:14:01 +00:00
|
|
|
IoError(std::io::Error),
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 23:57:42 +00:00
|
|
|
/// Alias for [`Result`](std::result::Result) with the error type of [`Error`].
|
2022-01-03 14:14:01 +00:00
|
|
|
pub type Result<T> = core::result::Result<T, Error>;
|
2021-11-28 12:36:12 +00:00
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2022-01-03 18:12:06 +00:00
|
|
|
Self::InvalidMagic { magic } => {
|
|
|
|
write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic.to_be_bytes())
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
Self::InvalidChannels { channels } => {
|
|
|
|
write!(f, "invalid number of channels: {}", channels)
|
|
|
|
}
|
2022-01-03 18:12:06 +00:00
|
|
|
Self::InvalidColorSpace { colorspace } => {
|
|
|
|
write!(f, "invalid color space: {} (expected 0 or 1)", colorspace)
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2022-01-03 18:12:06 +00:00
|
|
|
Self::InvalidImageDimensions { width, height } => {
|
|
|
|
write!(f, "invalid image dimensions: {}x{}", width, height)
|
2021-12-29 13:00:16 +00:00
|
|
|
}
|
2022-01-02 21:47:13 +00:00
|
|
|
Self::InvalidImageLength { size, width, height } => {
|
2022-01-03 18:12:06 +00:00
|
|
|
write!(f, "invalid image length: {} bytes for {}x{}", size, width, height)
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2021-11-30 02:46:04 +00:00
|
|
|
Self::OutputBufferTooSmall { size, required } => {
|
2022-01-03 18:12:06 +00:00
|
|
|
write!(f, "output buffer size too small: {} (required: {})", size, required)
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2021-12-01 17:13:50 +00:00
|
|
|
Self::UnexpectedBufferEnd => {
|
|
|
|
write!(f, "unexpected input buffer end while decoding")
|
|
|
|
}
|
2022-01-01 21:02:08 +00:00
|
|
|
Self::InvalidPadding => {
|
2022-01-03 18:12:06 +00:00
|
|
|
write!(f, "invalid padding (stream end marker mismatch)")
|
2022-01-01 21:02:08 +00:00
|
|
|
}
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-01-01 21:03:47 +00:00
|
|
|
Self::IoError(ref err) => {
|
|
|
|
write!(f, "i/o error: {}", err)
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl std::error::Error for Error {}
|
2021-12-31 10:33:45 +00:00
|
|
|
|
|
|
|
impl From<Infallible> for Error {
|
|
|
|
fn from(_: Infallible) -> Self {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
2022-01-01 21:03:47 +00:00
|
|
|
|
2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(err: std::io::Error) -> Self {
|
2022-01-01 21:03:47 +00:00
|
|
|
Self::IoError(err)
|
|
|
|
}
|
|
|
|
}
|