1use std::fmt;
2use std::io;
3
4#[derive(Debug)]
6pub enum AsepriteError {
7 Io(io::Error),
9 InvalidMagic,
11 UnsupportedColorDepth(u16),
13 FrameOutOfBounds(usize),
15 PixelSizeMismatch { expected: usize, actual: usize },
17 InvalidFrameRange,
19 MissingPalette,
21 LinkedCelNotFound { layer: usize, source_frame: usize },
23 InvalidChunkSize,
25 UnsupportedChunkType(u16),
27 FormatLimitExceeded { field: &'static str, value: usize, max: usize },
29}
30
31impl fmt::Display for AsepriteError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 Self::Io(e) => write!(f, "I/O error: {e}"),
35 Self::InvalidMagic => write!(f, "invalid magic number (expected 0xA5E0)"),
36 Self::UnsupportedColorDepth(d) => write!(f, "unsupported color depth: {d}"),
37 Self::FrameOutOfBounds(i) => write!(f, "frame index {i} out of bounds"),
38 Self::PixelSizeMismatch { expected, actual } => {
39 write!(f, "pixel data size mismatch: expected {expected}, got {actual}")
40 }
41 Self::InvalidFrameRange => write!(f, "invalid frame range"),
42 Self::MissingPalette => write!(f, "indexed color mode requires a palette"),
43 Self::LinkedCelNotFound { layer, source_frame } => {
44 write!(f, "linked cel not found: layer {layer}, source frame {source_frame}")
45 }
46 Self::InvalidChunkSize => write!(f, "invalid chunk size"),
47 Self::UnsupportedChunkType(t) => write!(f, "unsupported chunk type: 0x{t:04X}"),
48 Self::FormatLimitExceeded { field, value, max } => {
49 write!(f, "format limit exceeded for {field}: {value} > {max}")
50 }
51 }
52 }
53}
54
55impl std::error::Error for AsepriteError {
56 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57 match self {
58 Self::Io(e) => Some(e),
59 _ => None,
60 }
61 }
62}
63
64impl From<io::Error> for AsepriteError {
65 fn from(e: io::Error) -> Self {
66 Self::Io(e)
67 }
68}