aseprite_reader2/
error.rs1use std::{fmt::Debug, string::FromUtf8Error};
2
3use flate2::DecompressError;
4use nom::{error::ParseError, IResult};
5
6#[derive(Debug, thiserror::Error)]
15pub enum AsepriteParseError<I: std::fmt::Debug> {
16 #[error("Found invalid color depth {0}. Expected 32/16/8.")]
18 InvalidColorDepth(u16),
19 #[error("Found invalid UTF-8 {0}")]
21 InvalidUtf8(FromUtf8Error),
22 #[error("Found invalid layer type {0}. Expected 0 (Normal) / 1 (Group)")]
24 InvalidLayerType(u16),
25 #[error("Found invalid blend mode {0}")]
27 InvalidBlendMode(u16),
28 #[error("Found invalid compressed data {0}")]
30 InvalidCompressedData(DecompressError),
31 #[error("Did not find enough compressed data. File invalid.")]
33 NotEnoughCompressedData,
34 #[error("Found invalid cel while decompressing")]
36 InvalidCel,
37 #[error("Found invalid cel type {0}")]
39 InvalidCelType(u16),
40 #[error("Found invalid animation type {0}")]
42 InvalidAnimationDirection(u8),
43
44 #[error("Nom error: {nom:?}")]
46 GenericNom {
47 input: I,
49 nom: nom::error::ErrorKind,
51 },
52
53 #[error("An error occured while parsing a layer_chunk")]
55 InvalidLayerChunk(Box<AsepriteParseError<I>>),
56 #[error("An error occured while parsing a layer_chunk")]
58 InvalidCelChunk(Box<AsepriteParseError<I>>),
59 #[error("An error occured while parsing a layer_chunk")]
61 InvalidCelExtraChunk(Box<AsepriteParseError<I>>),
62 #[error("An error occured while parsing a layer_chunk")]
64 InvalidTagsChunk(Box<AsepriteParseError<I>>),
65 #[error("An error occured while parsing a layer_chunk")]
67 InvalidPaletteChunk(Box<AsepriteParseError<I>>),
68 #[error("An error occured while parsing a layer_chunk")]
70 InvalidUserDataChunk(Box<AsepriteParseError<I>>),
71 #[error("An error occured while parsing a layer_chunk")]
73 InvalidSliceChunk(Box<AsepriteParseError<I>>),
74 #[error("An error occured while parsing a layer_chunk")]
76 InvalidColorProfileChunk(Box<AsepriteParseError<I>>),
77}
78
79impl<I: Debug> ParseError<I> for AsepriteParseError<I> {
80 fn from_error_kind(input: I, kind: nom::error::ErrorKind) -> Self {
81 AsepriteParseError::GenericNom { input, nom: kind }
82 }
83
84 fn append(_input: I, _kind: nom::error::ErrorKind, other: Self) -> Self {
85 other
86 }
87}
88
89#[derive(Debug, thiserror::Error)]
91#[non_exhaustive]
92pub enum AsepriteError {
93 #[error("An error occured during parsing: {0}")]
97 Parse(String),
98 #[error("An IO error occured")]
100 Io(#[from] std::io::Error),
101 #[error("Invalid configuration of the aseprite file")]
103 InvalidConfiguration(#[from] AsepriteInvalidError),
104}
105
106impl<'a> From<AsepriteParseError<&'a [u8]>> for AsepriteError {
107 fn from(other: AsepriteParseError<&'a [u8]>) -> Self {
108 AsepriteError::Parse(other.to_string())
109 }
110}
111
112#[derive(Debug, thiserror::Error)]
116#[non_exhaustive]
117pub enum AsepriteInvalidError {
118 #[error("An invalid layer was specified")]
120 InvalidLayer(usize),
121 #[error("An invalid frame was specified")]
123 InvalidFrame(usize),
124 #[error("An invalid palette index was specified as a color")]
126 InvalidPaletteIndex(usize),
127}
128
129pub(crate) type AseParseResult<'a, R> = IResult<&'a [u8], R, AsepriteParseError<&'a [u8]>>;
130pub(crate) type AseResult<R> = std::result::Result<R, AsepriteError>;