use alloc::fmt::Display;
#[cfg(feature = "std")]
use std::error::Error;
#[derive(Debug)]
pub enum EncodingError {
InvalidAppSegment(u8),
AppSegmentTooLarge(usize),
IccTooLarge(usize),
BadImageData { length: usize, required: usize },
ZeroImageDimensions { width: u16, height: u16 },
#[cfg(feature = "std")]
IoError(std::io::Error),
Write(alloc::string::String),
}
#[cfg(feature = "std")]
impl From<std::io::Error> for EncodingError {
fn from(err: std::io::Error) -> EncodingError {
EncodingError::IoError(err)
}
}
impl Display for EncodingError {
fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result {
use EncodingError::*;
match self {
InvalidAppSegment(nr) => write!(f, "Invalid app segment number: {}", nr),
AppSegmentTooLarge(length) => write!(
f,
"App segment exceeds maximum allowed data length of 65533: {}",
length
),
IccTooLarge(length) => write!(
f,
"ICC profile exceeds maximum allowed data length: {}",
length
),
BadImageData { length, required } => write!(
f,
"Image data too small for dimensions and color_type: {} need at least {}",
length, required
),
ZeroImageDimensions { width, height } => {
write!(f, "Image dimensions must be non zero: {}x{}", width, height)
}
#[cfg(feature = "std")]
IoError(err) => err.fmt(f),
Write(err) => write!(f, "{}", err),
}
}
}
#[cfg(feature = "std")]
impl Error for EncodingError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
EncodingError::IoError(err) => Some(err),
_ => None,
}
}
}