use crate::chunk::ColorType;
pub type Result<T> = std::result::Result<T, Error>;
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(std::sync::Arc::new(err))
}
}
#[derive(Clone)]
#[allow(variant_size_differences)]
pub enum Error {
Io(std::sync::Arc<std::io::Error>),
ColorType(u8),
BitDepth(u8),
ColorMode(ColorType, u8),
BackgroundSize(ColorType),
InvalidSignature,
AdlerChecksum,
Inflate(miniz_oxide::inflate::TINFLStatus),
PresetDict,
CompressionMethod,
ZlibHeader,
ZlibTooSmall,
InterlaceMethod,
FilterMethod,
ImageDimensions,
Empty,
TextSize(usize),
HuffmanEnd,
IllegalFilterType,
AlphaPaletteLen,
ChunkSize,
AlphaPaletteWithAlphaMode,
NoEnd,
PhysUnits,
NulTerm,
ChunkLength([u8; 4]),
UnknownChunkType([u8; 4]),
Eof,
ChunkOrder,
NoImageData,
TrailingChunk,
Multiple([u8; 4]),
Crc32([u8; 4]),
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Error::*;
match self {
Io(io) => write!(f, "I/O Error: {:?}", io),
ColorType(_) => write!(f, "Unrecognized color type"),
BitDepth(_) => write!(f, "Out of bounds bit depth"),
ColorMode(_ct, _bd) => write!(f, "Invalid color type / bit depth combination"),
BackgroundSize(_) => write!(f, "Background color type mismatch with image color type"),
InvalidSignature => write!(f, "Not a PNG file"),
AdlerChecksum => write!(f, "Adler checksum not correct, data must be corrupted"),
Inflate(e) => write!(f, "Inflate: {:?}", e),
PresetDict => write!(f, "ZLib compression using preset dictionary, PNG doesn't allow"),
CompressionMethod => write!(f, "Invalid compression method in zlib header"),
ZlibHeader => write!(f, "Invalid FCHECK in zlib header"),
ZlibTooSmall => write!(f, "ZLib data is too small"),
InterlaceMethod => write!(f, "Invalid interlace method"),
FilterMethod => write!(f, "Invalid filter method"),
ImageDimensions => write!(f, "Invalid image dimensions, must be greater than 0"),
Empty => write!(f, "File doesn't contain any chunks."),
TextSize(size) => write!(f, "Text size ({}) doesn't fit inequality 1 ≤ x ≤ 79", size),
HuffmanEnd => write!(f, "The length of the END symbol 256 in the Huffman tree is 0"),
IllegalFilterType => write!(f, "Unrecognized filter type"),
AlphaPaletteLen => write!(f, "Alpha palette is larger than the palette."),
ChunkSize => write!(f, "Chunk is the wrong size"),
AlphaPaletteWithAlphaMode => write!(f, "Mode has an alpha channel, but also an alpha palette (must pick one)"),
NoEnd => write!(f, "Chunk was expected to end, but didn't"),
PhysUnits => write!(f, "Unknown physical units (must be unspecified or meter)"),
NulTerm => write!(f, "Expected null terminator, but not found"),
ChunkLength(bytes) => write!(f, "{:?} chunk wrong length", String::from_utf8_lossy(bytes)),
UnknownChunkType(bytes) => write!(f, "{:?} chunk unrecognized", String::from_utf8_lossy(bytes)),
Eof => write!(f, "Unexpected end of file"),
ChunkOrder => write!(f, "PNG chunks are out of order"),
NoImageData => write!(f, "No IDAT chunk exists, invalid PNG file"),
TrailingChunk => write!(f, "Trailing chunks were found after IEND, which is invalid"),
Multiple(bytes) => write!(f, "Only one {:?} chunk allowed, but found multiple", String::from_utf8_lossy(bytes)),
Crc32(bytes) => write!(f, "CRC32 Checksum failed for {:?} chunk", String::from_utf8_lossy(bytes)),
}
}
}