Skip to main content

martin_core/resources/sprites/
error.rs

1//! Error types for sprite processing operations.
2
3use std::path::PathBuf;
4
5use spreet::SpreetError;
6use spreet::resvg::usvg::Error as ResvgError;
7
8/// Errors that can occur during sprite processing.
9#[non_exhaustive]
10#[derive(thiserror::Error, Debug)]
11pub enum SpriteError {
12    /// Sprite source ID not found.
13    #[error("Sprite {0} not found")]
14    SpriteNotFound(String),
15
16    /// Too many distinct sprite source IDs requested at once.
17    #[error("Requested {requested} sprite ids, but at most {max} are allowed per request")]
18    TooManySpriteIds {
19        /// Number of distinct ids in the request.
20        requested: usize,
21        /// Maximum number of distinct ids allowed per request.
22        max: usize,
23    },
24
25    /// A sprite alias name is empty, contains a comma, or ends with `@2x`.
26    #[error(
27        "Sprite alias {0:?} is invalid: alias names must be non-empty, must not contain commas and must not end with @2x"
28    )]
29    InvalidAliasName(String),
30
31    /// A sprite alias does not reference any sprite sources.
32    #[error("Sprite alias {0:?} does not reference any sprite sources")]
33    EmptyAlias(String),
34
35    /// A sprite alias references more sources than a single request may use.
36    #[error(
37        "Sprite alias {alias:?} references {requested} sprite sources, but at most {max} are allowed"
38    )]
39    TooManySpritesInAlias {
40        /// Alias name.
41        alias: String,
42        /// Referenced source count.
43        requested: usize,
44        /// Allowed maximum.
45        max: usize,
46    },
47
48    /// A sprite alias references another alias instead of a sprite source.
49    #[error(
50        "Sprite alias {alias:?} references {sprite:?}, which is itself an alias; aliases may only reference sprite sources"
51    )]
52    AliasWithinAlias {
53        /// Alias name.
54        alias: String,
55        /// The referenced alias.
56        sprite: String,
57    },
58
59    /// A sprite alias references a sprite source that was not configured.
60    #[error("Sprite alias {alias:?} references unknown sprite source {sprite:?}")]
61    AliasSpriteNotFound {
62        /// Alias name.
63        alias: String,
64        /// The referenced sprite source.
65        sprite: String,
66    },
67
68    /// I/O error accessing sprite file or directory.
69    #[error("IO error {0}: {1}")]
70    IoError(#[source] std::io::Error, PathBuf),
71
72    /// Path is not a valid file.
73    #[error("Sprite path is not a file: {0}")]
74    InvalidFilePath(PathBuf),
75
76    /// Sprite source has invalid file path.
77    #[error("Sprite {0} uses bad file {1}")]
78    InvalidSpriteFilePath(String, PathBuf),
79
80    /// No SVG files found in directory.
81    #[error("No sprite SVG files found in {0} to generate spritesheets from")]
82    NoSpriteFilesFound(PathBuf),
83
84    /// Failed to read sprite file.
85    #[error("Sprite {0} could not be loaded")]
86    UnableToReadSprite(PathBuf),
87
88    /// Sprite processing error.
89    #[error("{0} in file {1}")]
90    SpriteProcessingError(#[source] SpreetError, PathBuf),
91
92    /// SVG parsing error.
93    #[error("{0} in file {1}")]
94    SpriteParsingError(#[source] ResvgError, PathBuf),
95
96    /// Failed to generate spritesheet.
97    #[error("Unable to generate spritesheet")]
98    UnableToGenerateSpritesheet,
99
100    /// Failed to create sprite from SVG file.
101    #[error("Unable to create a sprite from file {0}")]
102    SpriteInstError(PathBuf),
103}
104
105impl crate::Classify for SpriteError {
106    fn kind(&self) -> crate::ErrorKind {
107        use crate::ErrorKind::{Internal, InvalidInput, NotFound};
108        match self {
109            Self::SpriteNotFound(_) => NotFound,
110            Self::TooManySpriteIds { .. }
111            | Self::InvalidAliasName(_)
112            | Self::EmptyAlias(_)
113            | Self::TooManySpritesInAlias { .. }
114            | Self::AliasWithinAlias { .. }
115            | Self::AliasSpriteNotFound { .. } => InvalidInput,
116            Self::IoError(..)
117            | Self::InvalidFilePath(_)
118            | Self::InvalidSpriteFilePath(..)
119            | Self::NoSpriteFilesFound(_)
120            | Self::UnableToReadSprite(_)
121            | Self::SpriteProcessingError(..)
122            | Self::SpriteParsingError(..)
123            | Self::UnableToGenerateSpritesheet
124            | Self::SpriteInstError(_) => Internal,
125        }
126    }
127}