use std::{fmt::Debug, string::FromUtf8Error};
use flate2::DecompressError;
use nom::{error::ParseError, IResult};
#[derive(Debug, thiserror::Error)]
pub enum AsepriteParseError<I: std::fmt::Debug> {
#[error("Found invalid color depth {0}. Expected 32/16/8.")]
InvalidColorDepth(u16),
#[error("Found invalid UTF-8 {0}")]
InvalidUtf8(FromUtf8Error),
#[error("Found invalid layer type {0}. Expected 0 (Normal) / 1 (Group)")]
InvalidLayerType(u16),
#[error("Found invalid blend mode {0}")]
InvalidBlendMode(u16),
#[error("Found invalid compressed data {0}")]
InvalidCompressedData(DecompressError),
#[error("Did not find enough compressed data. File invalid.")]
NotEnoughCompressedData,
#[error("Found invalid cel while decompressing")]
InvalidCel,
#[error("Found invalid cel type {0}")]
InvalidCelType(u16),
#[error("Found invalid animation type {0}")]
InvalidAnimationDirection(u8),
#[error("Nom error: {nom:?}")]
GenericNom {
input: I,
nom: nom::error::ErrorKind,
},
#[error("An error occured while parsing a layer_chunk")]
InvalidLayerChunk(Box<AsepriteParseError<I>>),
#[error("An error occured while parsing a layer_chunk")]
InvalidCelChunk(Box<AsepriteParseError<I>>),
#[error("An error occured while parsing a layer_chunk")]
InvalidCelExtraChunk(Box<AsepriteParseError<I>>),
#[error("An error occured while parsing a layer_chunk")]
InvalidTagsChunk(Box<AsepriteParseError<I>>),
#[error("An error occured while parsing a layer_chunk")]
InvalidPaletteChunk(Box<AsepriteParseError<I>>),
#[error("An error occured while parsing a layer_chunk")]
InvalidUserDataChunk(Box<AsepriteParseError<I>>),
#[error("An error occured while parsing a layer_chunk")]
InvalidSliceChunk(Box<AsepriteParseError<I>>),
#[error("An error occured while parsing a layer_chunk")]
InvalidColorProfileChunk(Box<AsepriteParseError<I>>),
}
impl<I: Debug> ParseError<I> for AsepriteParseError<I> {
fn from_error_kind(input: I, kind: nom::error::ErrorKind) -> Self {
AsepriteParseError::GenericNom { input, nom: kind }
}
fn append(_input: I, _kind: nom::error::ErrorKind, other: Self) -> Self {
other
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum AsepriteError {
#[error("An error occured during parsing: {0}")]
Parse(String),
#[error("An IO error occured")]
Io(#[from] std::io::Error),
#[error("Invalid configuration of the aseprite file")]
InvalidConfiguration(#[from] AsepriteInvalidError),
}
impl<'a> From<AsepriteParseError<&'a [u8]>> for AsepriteError {
fn from(other: AsepriteParseError<&'a [u8]>) -> Self {
AsepriteError::Parse(other.to_string())
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum AsepriteInvalidError {
#[error("An invalid layer was specified")]
InvalidLayer(usize),
#[error("An invalid frame was specified")]
InvalidFrame(usize),
#[error("An invalid palette index was specified as a color")]
InvalidPaletteIndex(usize),
}
pub(crate) type AseParseResult<'a, R> = IResult<&'a [u8], R, AsepriteParseError<&'a [u8]>>;
pub(crate) type AseResult<R> = std::result::Result<R, AsepriteError>;