aseprite_loader/binary/
errors.rs

1use std::str::Utf8Error;
2
3use nom::IResult;
4
5use super::{
6    palette::PaletteError,
7    scalars::{Dword, Word},
8};
9
10#[derive(Debug, strum::Display)]
11pub enum ParseError<'a> {
12    /// This variant is used when the conversion between
13    /// DWORD (u32) to usize fails. The only way this can
14    /// happen is when running this code on a 16-bit system.
15    DwordToUsize(Dword),
16    /// This variant is used when the frame size is <4
17    InvalidFrameSize(Dword),
18    /// This variant is used when the chunk size is <4
19    InvalidChunkSize(Dword),
20    /// This variant is used when a string does not contain
21    /// valid UTF-8 data and `str::from_utf8` returned an error.
22    Utf8Error(Utf8Error),
23    /// The uses index colors but the palette could not be
24    /// generated due to errors in the palette chunks.
25    PaletteError(PaletteError),
26    /// The range of frame indices was invalid (from > to)
27    InvalidFrameRange(Word, Word),
28    /// This variant is used when a layer index is out
29    /// of bounds (layer_index >= layer_count)
30    LayerIndexOutOfBounds,
31    /// This variant is used when a unsupported property
32    /// type is found in the user data.
33    InvalidPropertyType(Word),
34    /// This variant is used when the nom combinators return
35    /// an error.
36    Nom(nom::error::Error<&'a [u8]>),
37}
38
39impl<'a> std::error::Error for ParseError<'a> {}
40
41impl<'a> nom::error::ParseError<&'a [u8]> for ParseError<'a> {
42    fn from_error_kind(input: &'a [u8], kind: nom::error::ErrorKind) -> Self {
43        Self::Nom(nom::error::Error::from_error_kind(input, kind))
44    }
45    fn append(_input: &[u8], _kind: nom::error::ErrorKind, other: Self) -> Self {
46        other
47    }
48}
49
50impl<'a> nom::error::FromExternalError<&'a [u8], Utf8Error> for ParseError<'a> {
51    fn from_external_error(_input: &'a [u8], _kind: nom::error::ErrorKind, e: Utf8Error) -> Self {
52        ParseError::Utf8Error(e)
53    }
54}
55
56pub type ParseResult<'a, O> = IResult<&'a [u8], O, ParseError<'a>>;