Add generic streaming decoder

This commit is contained in:
Ivan Smirnov 2022-01-02 00:03:47 +03:00
commit 074fc72b22
2 changed files with 189 additions and 1 deletions

View file

@ -1,11 +1,12 @@
use std::convert::Infallible;
use std::error::Error as StdError;
use std::fmt::{self, Display};
use std::io;
use std::result::Result as StdResult;
use crate::consts::{QOI_MAGIC, QOI_PIXELS_MAX};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug)]
pub enum Error {
InvalidChannels { channels: u8 },
EmptyImage { width: u32, height: u32 },
@ -17,6 +18,7 @@ pub enum Error {
UnexpectedBufferEnd,
InvalidColorSpace { colorspace: u8 },
InvalidPadding,
IoError(io::Error),
}
pub type Result<T> = StdResult<T, Error>;
@ -55,6 +57,9 @@ impl Display for Error {
Self::InvalidPadding => {
write!(f, "invalid padding (stream end marker)")
}
Self::IoError(ref err) => {
write!(f, "i/o error: {}", err)
}
}
}
}
@ -66,3 +71,9 @@ impl From<Infallible> for Error {
unreachable!()
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::IoError(err)
}
}