use std::fmt;
use std::io;
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
EndOfStream,
Truncated,
Parse(&'static str),
Unsupported(String),
Limits,
Io(io::Error),
BadInput,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::EndOfStream => write!(f, "end of stream"),
Error::Truncated => write!(f, "unexpected EOF inside a frame"),
Error::Parse(detail) => write!(f, "parse error: {detail}"),
Error::Unsupported(tag) => write!(f, "unsupported colorspace tag: C{tag}"),
Error::Limits => write!(f, "header or frame size limit exceeded"),
Error::Io(e) => write!(f, "i/o error: {e}"),
Error::BadInput => write!(f, "writer input does not match the stream header"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
if e.kind() == io::ErrorKind::UnexpectedEof {
Error::Truncated
} else {
Error::Io(e)
}
}
}