2021-12-31 10:33:45 +00:00
|
|
|
use std::convert::Infallible;
|
2021-11-28 12:36:12 +00:00
|
|
|
use std::error::Error as StdError;
|
|
|
|
use std::fmt::{self, Display};
|
2022-01-01 21:03:47 +00:00
|
|
|
use std::io;
|
2021-11-28 12:36:12 +00:00
|
|
|
use std::result::Result as StdResult;
|
|
|
|
|
2021-12-29 13:00:16 +00:00
|
|
|
use crate::consts::{QOI_MAGIC, QOI_PIXELS_MAX};
|
2021-11-28 12:36:12 +00:00
|
|
|
|
2022-01-01 21:03:47 +00:00
|
|
|
#[derive(Debug)]
|
2021-11-28 12:36:12 +00:00
|
|
|
pub enum Error {
|
|
|
|
InvalidChannels { channels: u8 },
|
|
|
|
EmptyImage { width: u32, height: u32 },
|
2021-12-29 13:00:16 +00:00
|
|
|
ImageTooLarge { width: u32, height: u32 },
|
2021-11-28 12:36:12 +00:00
|
|
|
BadEncodingDataSize { size: usize, expected: usize },
|
2021-12-01 16:01:17 +00:00
|
|
|
InputBufferTooSmall { size: usize, required: usize },
|
2021-11-30 02:46:04 +00:00
|
|
|
OutputBufferTooSmall { size: usize, required: usize },
|
2021-11-29 22:38:09 +00:00
|
|
|
InvalidMagic { magic: u32 },
|
2021-12-01 17:13:50 +00:00
|
|
|
UnexpectedBufferEnd,
|
2021-12-31 10:34:27 +00:00
|
|
|
InvalidColorSpace { colorspace: u8 },
|
2022-01-01 21:02:08 +00:00
|
|
|
InvalidPadding,
|
2022-01-01 21:03:47 +00:00
|
|
|
IoError(io::Error),
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = StdResult<T, Error>;
|
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Self::InvalidChannels { channels } => {
|
|
|
|
write!(f, "invalid number of channels: {}", channels)
|
|
|
|
}
|
|
|
|
Self::EmptyImage { width, height } => {
|
|
|
|
write!(f, "image contains no pixels: {}x{}", width, height)
|
|
|
|
}
|
2021-12-29 13:00:16 +00:00
|
|
|
Self::ImageTooLarge { width, height } => {
|
|
|
|
let mp = QOI_PIXELS_MAX / 1_000_000;
|
|
|
|
write!(f, "image is too large: {}x{} (max={}Mp)", width, height, mp)
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
Self::BadEncodingDataSize { size, expected } => {
|
|
|
|
write!(f, "bad data size when encoding: {} (expected: {})", size, expected)
|
|
|
|
}
|
2021-12-01 16:01:17 +00:00
|
|
|
Self::InputBufferTooSmall { size, required } => {
|
|
|
|
write!(f, "input buffer size too small: {} (minimum required: {})", size, required)
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
2021-11-30 02:46:04 +00:00
|
|
|
Self::OutputBufferTooSmall { size, required } => {
|
|
|
|
write!(f, "output buffer size too small: {} (minimum required: {})", size, required)
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
Self::InvalidMagic { magic } => {
|
|
|
|
write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic)
|
|
|
|
}
|
2021-12-01 17:13:50 +00:00
|
|
|
Self::UnexpectedBufferEnd => {
|
|
|
|
write!(f, "unexpected input buffer end while decoding")
|
|
|
|
}
|
2021-12-31 10:34:27 +00:00
|
|
|
Self::InvalidColorSpace { colorspace } => {
|
|
|
|
write!(f, "invalid color space: {} (expected 0 or 1)", colorspace)
|
|
|
|
}
|
2022-01-01 21:02:08 +00:00
|
|
|
Self::InvalidPadding => {
|
|
|
|
write!(f, "invalid padding (stream end marker)")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError 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
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
Self::IoError(err)
|
|
|
|
}
|
|
|
|
}
|