use core::fmt;
#[non_exhaustive]
#[derive(Debug)]
pub enum AvifError {
InvalidData(String),
Unsupported(String),
LimitExceeded(String),
Io(std::io::Error),
}
impl AvifError {
pub fn invalid(msg: impl Into<String>) -> Self {
Self::InvalidData(msg.into())
}
pub fn unsupported(msg: impl Into<String>) -> Self {
Self::Unsupported(msg.into())
}
pub fn limit(msg: impl Into<String>) -> Self {
Self::LimitExceeded(msg.into())
}
}
impl fmt::Display for AvifError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidData(s) => write!(f, "invalid data: {s}"),
Self::Unsupported(s) => write!(f, "unsupported: {s}"),
Self::LimitExceeded(s) => write!(f, "limit exceeded: {s}"),
Self::Io(e) => write!(f, "I/O error: {e}"),
}
}
}
impl std::error::Error for AvifError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for AvifError {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
impl From<oxideav_heif::HeifError> for AvifError {
fn from(e: oxideav_heif::HeifError) -> Self {
match e {
oxideav_heif::HeifError::InvalidData(s) => Self::InvalidData(s),
oxideav_heif::HeifError::Unsupported(s) => Self::Unsupported(s),
oxideav_heif::HeifError::LimitExceeded(s) => Self::InvalidData(s),
other => Self::InvalidData(other.to_string()),
}
}
}
pub type Result<T> = core::result::Result<T, AvifError>;