1type BoxError = Box<dyn std::error::Error + Send + Sync>;
6
7#[derive(thiserror::Error, Debug)]
8#[non_exhaustive]
9pub enum Error {
10 #[error("io error: {0}")]
11 Io(#[from] std::io::Error),
12
13 #[error("corrupt .sb3 archive")]
15 ZipCorrupt(#[source] BoxError),
16
17 #[error("unsupported .sb3 archive")]
18 ZipUnsupported(#[source] BoxError),
19
20 #[error("missing archive entry: {name}")]
21 ZipMissingEntry { name: String },
22
23 #[error("zip entry too large: {name} exceeds {limit} bytes")]
24 ZipEntryTooLarge { name: String, limit: u64 },
25
26 #[error("invalid JSON syntax at line {line}, column {column}")]
28 JsonSyntax {
29 line: usize,
30 column: usize,
31 #[source]
32 source: BoxError,
33 },
34
35 #[error("project.json has an invalid structure at line {line}, column {column}: {source}")]
36 JsonData {
37 line: usize,
38 column: usize,
39 #[source]
40 source: BoxError,
41 },
42
43 #[error("project.json ended unexpectedly")]
44 JsonUnexpectedEof(#[source] BoxError),
45
46 #[cfg(feature = "costumes")]
48 #[error("corrupt costume data")]
49 CostumeCorrupt(#[source] BoxError),
50
51 #[cfg(feature = "costumes")]
52 #[error("unsupported costume data")]
53 CostumeUnsupported(#[source] BoxError),
54
55 #[cfg(feature = "costumes")]
56 #[error("costume exceeds size limits")]
57 CostumeTooLarge(#[source] BoxError),
58
59 #[cfg(feature = "costumes")]
60 #[error("invalid costume size: {w}x{h}")]
61 InvalidCostumeSize { w: u32, h: u32 },
62
63 #[error("unknown costume format: {0}")]
64 UnknownCostumeFormat(String),
65
66 #[cfg(feature = "sounds")]
68 #[error("corrupt sound data")]
69 SoundCorrupt(#[source] BoxError),
70
71 #[cfg(feature = "sounds")]
72 #[error("unsupported sound data")]
73 SoundUnsupported(#[source] BoxError),
74
75 #[cfg(feature = "sounds")]
76 #[error("sound contains no audio")]
77 SoundEmpty,
78
79 #[error("unknown sound format: {0}")]
80 UnknownSoundFormat(String),
81
82 #[error("invalid rotation style: {0}")]
84 InvalidRotationStyle(String),
85
86 #[error("invalid loudness/timer selection: {0}")]
87 InvalidLoudnessTimer(String),
88
89 #[error("invalid graphic effect: {0}")]
90 InvalidGraphicEffect(String),
91
92 #[error("invalid front/back selection: {0}")]
93 InvalidFrontBack(String),
94
95 #[error("invalid forward/backward selection: {0}")]
96 InvalidForwardBackward(String),
97
98 #[error("invalid sound effect: {0}")]
99 InvalidSoundEffect(String),
100
101 #[error("invalid stop option: {0}")]
102 InvalidStop(String),
103
104 #[error("invalid draggable/not draggable selection: {0}")]
105 InvalidDraggable(String),
106
107 #[error("invalid number/name selection: {0}")]
108 InvalidNumberName(String),
109
110 #[error("invalid current time selection: {0}")]
111 InvalidCurrent(String),
112
113 #[error("invalid math function selection: {0}")]
114 InvalidMathFunc(String),
115
116 #[error("invalid block mutation")]
117 InvalidMutation,
118
119 #[error("more than one stage found")]
120 MultipleStages,
121
122 #[error("no stage found")]
123 NoStage,
124
125 #[error("missing block: {id}")]
126 MissingBlock { id: String },
127
128 #[error("block depth exceeded: block depth went past {limit}")]
129 DepthExceeded { limit: usize },
130
131 #[error("block cycle: cycle at {id}")]
132 BlockCycle { id: String },
133
134 #[error("unknown opcode: {0}")]
135 UnknownOpcode(String),
136
137 #[error("invalid primitive: {0}")]
138 InvalidPrimitive(&'static str),
139
140 #[error("missing rotation style: no rotation style in target {0}")]
141 MissingRotationStyle(String),
142
143 #[error("missing field {field}")]
144 MissingField { field: String },
145
146 #[error("missing id part of field {field}")]
147 MissingIDOfField { field: String },
148
149 #[error("missing block mutation")]
150 MissingMutation,
151}
152
153impl Error {
154 pub(crate) fn json(e: serde_json::Error) -> Self {
155 use serde_json::error::Category;
156 let (line, column) = (e.line(), e.column());
157 match e.classify() {
158 Category::Syntax => Error::JsonSyntax {
159 line,
160 column,
161 source: Box::new(e),
162 },
163 Category::Eof => Error::JsonUnexpectedEof(Box::new(e)),
164 _ => Error::JsonData {
165 line,
166 column,
167 source: Box::new(e),
168 },
169 }
170 }
171
172 pub(crate) fn zip(e: zip::result::ZipError, entry: &str) -> Self {
173 use zip::result::ZipError as Z;
174 match e {
175 Z::Io(e) => Error::Io(e),
176 Z::FileNotFound => Error::ZipMissingEntry {
177 name: entry.to_owned(),
178 },
179 Z::UnsupportedArchive(_) => Error::ZipUnsupported(Box::new(e)),
180 e => Error::ZipCorrupt(Box::new(e)),
181 }
182 }
183}
184
185#[cfg(feature = "costumes")]
186impl Error {
187 pub(crate) fn image(e: image::ImageError) -> Self {
188 use image::ImageError as I;
189 match e {
190 I::Unsupported(_) => Error::CostumeUnsupported(Box::new(e)),
191 I::Limits(_) => Error::CostumeTooLarge(Box::new(e)),
192 _ => Error::CostumeCorrupt(Box::new(e)),
193 }
194 }
195
196 pub(crate) fn usvg(e: resvg::usvg::Error) -> Self {
197 Error::CostumeCorrupt(Box::new(e))
198 }
199}
200
201#[cfg(feature = "sounds")]
202impl Error {
203 pub(crate) fn hound(e: hound::Error) -> Self {
204 use hound::Error as H;
205 match e {
206 H::Unsupported => Error::SoundUnsupported(Box::new(e)),
207 H::IoError(io_err) => Error::Io(io_err),
208 _ => Error::SoundCorrupt(Box::new(e)),
209 }
210 }
211
212 pub(crate) fn nanomp3(e: nanomp3::Error) -> Self {
213 use nanomp3::Error as N;
214 match e {
215 N::Io(io_err) => Error::Io(io_err),
216 _ => Error::SoundCorrupt(Box::new(e)),
217 }
218 }
219}