use std::fmt;
use std::io;
#[derive(Debug)]
pub enum AsepriteError {
Io(io::Error),
InvalidMagic,
UnsupportedColorDepth(u16),
FrameOutOfBounds(usize),
PixelSizeMismatch { expected: usize, actual: usize },
InvalidFrameRange,
MissingPalette,
LinkedCelNotFound { layer: usize, source_frame: usize },
InvalidChunkSize,
UnsupportedChunkType(u16),
FormatLimitExceeded {
field: &'static str,
value: usize,
max: usize,
},
}
impl fmt::Display for AsepriteError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "I/O error: {e}"),
Self::InvalidMagic => write!(f, "invalid magic number (expected 0xA5E0)"),
Self::UnsupportedColorDepth(d) => write!(f, "unsupported color depth: {d}"),
Self::FrameOutOfBounds(i) => write!(f, "frame index {i} out of bounds"),
Self::PixelSizeMismatch { expected, actual } => {
write!(
f,
"pixel data size mismatch: expected {expected}, got {actual}"
)
}
Self::InvalidFrameRange => write!(f, "invalid frame range"),
Self::MissingPalette => write!(f, "indexed color mode requires a palette"),
Self::LinkedCelNotFound {
layer,
source_frame,
} => {
write!(
f,
"linked cel not found: layer {layer}, source frame {source_frame}"
)
}
Self::InvalidChunkSize => write!(f, "invalid chunk size"),
Self::UnsupportedChunkType(t) => write!(f, "unsupported chunk type: 0x{t:04X}"),
Self::FormatLimitExceeded { field, value, max } => {
write!(f, "format limit exceeded for {field}: {value} > {max}")
}
}
}
}
impl std::error::Error for AsepriteError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for AsepriteError {
fn from(e: io::Error) -> Self {
Self::Io(e)
}
}