use std::path::PathBuf;
use spreet::SpreetError;
use spreet::resvg::usvg::Error as ResvgError;
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum SpriteError {
#[error("Sprite {0} not found")]
SpriteNotFound(String),
#[error("Requested {requested} sprite ids, but at most {max} are allowed per request")]
TooManySpriteIds {
requested: usize,
max: usize,
},
#[error(
"Sprite alias {0:?} is invalid: alias names must be non-empty, must not contain commas and must not end with @2x"
)]
InvalidAliasName(String),
#[error("Sprite alias {0:?} does not reference any sprite sources")]
EmptyAlias(String),
#[error(
"Sprite alias {alias:?} references {requested} sprite sources, but at most {max} are allowed"
)]
TooManySpritesInAlias {
alias: String,
requested: usize,
max: usize,
},
#[error(
"Sprite alias {alias:?} references {sprite:?}, which is itself an alias; aliases may only reference sprite sources"
)]
AliasWithinAlias {
alias: String,
sprite: String,
},
#[error("Sprite alias {alias:?} references unknown sprite source {sprite:?}")]
AliasSpriteNotFound {
alias: String,
sprite: String,
},
#[error("IO error {0}: {1}")]
IoError(#[source] std::io::Error, PathBuf),
#[error("Sprite path is not a file: {0}")]
InvalidFilePath(PathBuf),
#[error("Sprite {0} uses bad file {1}")]
InvalidSpriteFilePath(String, PathBuf),
#[error("No sprite SVG files found in {0} to generate spritesheets from")]
NoSpriteFilesFound(PathBuf),
#[error("Sprite {0} could not be loaded")]
UnableToReadSprite(PathBuf),
#[error("{0} in file {1}")]
SpriteProcessingError(#[source] SpreetError, PathBuf),
#[error("{0} in file {1}")]
SpriteParsingError(#[source] ResvgError, PathBuf),
#[error("Unable to generate spritesheet")]
UnableToGenerateSpritesheet,
#[error("Unable to create a sprite from file {0}")]
SpriteInstError(PathBuf),
}
impl crate::Classify for SpriteError {
fn kind(&self) -> crate::ErrorKind {
use crate::ErrorKind::{Internal, InvalidInput, NotFound};
match self {
Self::SpriteNotFound(_) => NotFound,
Self::TooManySpriteIds { .. }
| Self::InvalidAliasName(_)
| Self::EmptyAlias(_)
| Self::TooManySpritesInAlias { .. }
| Self::AliasWithinAlias { .. }
| Self::AliasSpriteNotFound { .. } => InvalidInput,
Self::IoError(..)
| Self::InvalidFilePath(_)
| Self::InvalidSpriteFilePath(..)
| Self::NoSpriteFilesFound(_)
| Self::UnableToReadSprite(_)
| Self::SpriteProcessingError(..)
| Self::SpriteParsingError(..)
| Self::UnableToGenerateSpritesheet
| Self::SpriteInstError(_) => Internal,
}
}
}