martin_core/resources/sprites/
error.rs1use std::path::PathBuf;
4
5use spreet::SpreetError;
6use spreet::resvg::usvg::Error as ResvgError;
7
8#[non_exhaustive]
10#[derive(thiserror::Error, Debug)]
11pub enum SpriteError {
12 #[error("Sprite {0} not found")]
14 SpriteNotFound(String),
15
16 #[error("Requested {requested} sprite ids, but at most {max} are allowed per request")]
18 TooManySpriteIds {
19 requested: usize,
21 max: usize,
23 },
24
25 #[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 #[error("Sprite alias {0:?} does not reference any sprite sources")]
33 EmptyAlias(String),
34
35 #[error(
37 "Sprite alias {alias:?} references {requested} sprite sources, but at most {max} are allowed"
38 )]
39 TooManySpritesInAlias {
40 alias: String,
42 requested: usize,
44 max: usize,
46 },
47
48 #[error(
50 "Sprite alias {alias:?} references {sprite:?}, which is itself an alias; aliases may only reference sprite sources"
51 )]
52 AliasWithinAlias {
53 alias: String,
55 sprite: String,
57 },
58
59 #[error("Sprite alias {alias:?} references unknown sprite source {sprite:?}")]
61 AliasSpriteNotFound {
62 alias: String,
64 sprite: String,
66 },
67
68 #[error("IO error {0}: {1}")]
70 IoError(#[source] std::io::Error, PathBuf),
71
72 #[error("Sprite path is not a file: {0}")]
74 InvalidFilePath(PathBuf),
75
76 #[error("Sprite {0} uses bad file {1}")]
78 InvalidSpriteFilePath(String, PathBuf),
79
80 #[error("No sprite SVG files found in {0} to generate spritesheets from")]
82 NoSpriteFilesFound(PathBuf),
83
84 #[error("Sprite {0} could not be loaded")]
86 UnableToReadSprite(PathBuf),
87
88 #[error("{0} in file {1}")]
90 SpriteProcessingError(#[source] SpreetError, PathBuf),
91
92 #[error("{0} in file {1}")]
94 SpriteParsingError(#[source] ResvgError, PathBuf),
95
96 #[error("Unable to generate spritesheet")]
98 UnableToGenerateSpritesheet,
99
100 #[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}