-
Notifications
You must be signed in to change notification settings - Fork 25
/
errors.rs
269 lines (250 loc) · 7.14 KB
/
errors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! PSDDecodeErrors possible during image processing
use std::fmt::{Debug, Formatter};
use zune_core::colorspace::ColorSpace;
/// All possible image errors that can occur.
///
/// This is the grandfather of image errors and contains
/// all decoding,processing and encoding errors possible
pub enum ImgErrors
{
#[cfg(feature = "zune-jpeg")]
JpegDecodeErrors(zune_jpeg::errors::DecodeErrors),
#[cfg(feature = "png")]
PngDecodeErrors(zune_png::error::PngErrors),
#[cfg(feature = "ppm")]
PPMDecodeErrors(zune_ppm::PPMDecodeErrors),
#[cfg(feature = "psd")]
PSDDecodeErrors(zune_psd::errors::PSDDecodeErrors),
#[cfg(feature = "qoi")]
QoiDecodeErrors(zune_qoi::QoiErrors),
DimensionsMisMatch(usize, usize),
UnsupportedColorspace(ColorSpace, &'static str, &'static [ColorSpace]),
NoImageForOperations,
NoImageForEncoding,
NoImageBuffer,
OperationsError(ImgOperationsErrors),
EncodeErrors(ImgEncodeErrors),
GenericString(String),
GenericStr(&'static str)
}
/// PSDDecodeErrors that may occur during image operations
pub enum ImgOperationsErrors
{
/// Unexpected colorspace
WrongColorspace(ColorSpace, ColorSpace),
/// Wrong number of components
WrongComponents(usize, usize),
/// Channel layout does not match expected
InvalidChannelLayout(&'static str),
/// Generic errors
Generic(&'static str),
/// Generic errors which have more context
GenericString(String)
}
/// All errors possible during image encoding
pub enum ImgEncodeErrors
{
Generic(String),
GenericStatic(&'static str),
UnsupportedColorspace(ColorSpace, &'static [ColorSpace]),
#[cfg(feature = "ppm")]
PPMEncodeErrors(zune_ppm::PPMErrors)
}
impl Debug for ImgErrors
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
#[cfg(feature = "jpeg")]
Self::JpegDecodeErrors(ref error) =>
{
writeln!(f, "Jpeg decoding failed:{:?}", error)
}
#[cfg(feature = "png")]
Self::PngDecodeErrors(ref error) =>
{
writeln!(f, "Png decoding failed:{:?}", error)
}
#[cfg(feature = "ppm")]
Self::PPMDecodeErrors(ref error) =>
{
writeln!(f, "PPM decoding failed:{:?}", error)
}
#[cfg(feature = "psd")]
Self::PSDDecodeErrors(ref error) =>
{
writeln!(f, "PSD decoding failed:{:?}", error)
}
#[cfg(feature = "qoi")]
Self::QoiDecodeErrors(ref error) =>
{
writeln!(f, "QOI decoding failed:{:?}", error)
}
Self::GenericStr(err) =>
{
writeln!(f, "{}", err)
}
Self::GenericString(err) =>
{
writeln!(f, "{}", err)
}
Self::NoImageForOperations =>
{
writeln!(f, "No image found for which we can execute operations")
}
Self::NoImageForEncoding =>
{
writeln!(f, "No image found for which we can encode")
}
Self::NoImageBuffer => writeln!(f, "No image buffer present"),
Self::OperationsError(ref error) => writeln!(f, "{:?}", error),
Self::EncodeErrors(ref err) => writeln!(f, "{:?}", err),
ImgErrors::UnsupportedColorspace(present, operation, supported) =>
{
writeln!(f,"Unsupported colorspace {:?}, for the operation {}\nSupported colorspaces are {:?}",present,operation,supported)
}
ImgErrors::DimensionsMisMatch(expected, found) =>
{
writeln!(
f,
"Dimensions mismatch, expected {} but found {}",
expected, found
)
}
}
}
}
#[cfg(feature = "zune-jpeg")]
impl From<zune_jpeg::errors::DecodeErrors> for ImgErrors
{
fn from(from: zune_jpeg::errors::DecodeErrors) -> Self
{
ImgErrors::JpegDecodeErrors(from)
}
}
#[cfg(feature = "zune-png")]
impl From<zune_png::error::PngErrors> for ImgErrors
{
fn from(from: zune_png::error::PngErrors) -> Self
{
ImgErrors::PngDecodeErrors(from)
}
}
#[cfg(feature = "ppm")]
impl From<zune_ppm::PPMDecodeErrors> for ImgErrors
{
fn from(from: zune_ppm::PPMDecodeErrors) -> Self
{
ImgErrors::PPMDecodeErrors(from)
}
}
impl From<ImgOperationsErrors> for ImgErrors
{
fn from(from: ImgOperationsErrors) -> Self
{
ImgErrors::OperationsError(from)
}
}
impl From<ImgEncodeErrors> for ImgErrors
{
fn from(from: ImgEncodeErrors) -> Self
{
ImgErrors::EncodeErrors(from)
}
}
impl Debug for ImgOperationsErrors
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
Self::InvalidChannelLayout(reason) =>
{
writeln!(f, "{:}", reason)
}
Self::Generic(reason) =>
{
writeln!(f, "{:}", reason)
}
Self::GenericString(err) =>
{
writeln!(f, "{}", err)
}
Self::WrongColorspace(ref expected, ref found) =>
{
writeln!(
f,
"Expected {:?} colorspace but found {:?}",
expected, found
)
}
Self::WrongComponents(expected, found) =>
{
writeln!(f, "Expected {} components and found {}", expected, found)
}
}
}
}
impl Debug for ImgEncodeErrors
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
Self::Generic(ref string) => writeln!(f, "{}", string),
Self::GenericStatic(ref string) => writeln!(f, "{}", string),
Self::UnsupportedColorspace(ref found, ref expected) =>
{
writeln!(
f,
"Found colorspace {:?} but the encoder supports {:?}",
found, expected
)
}
#[cfg(feature = "ppm")]
Self::PPMEncodeErrors(ref error) =>
{
writeln!(f, "{:?}", error)
}
}
}
}
#[cfg(feature = "ppm")]
impl From<zune_ppm::PPMErrors> for ImgEncodeErrors
{
fn from(error: zune_ppm::PPMErrors) -> Self
{
ImgEncodeErrors::PPMEncodeErrors(error)
}
}
#[cfg(feature = "psd")]
impl From<zune_psd::errors::PSDDecodeErrors> for ImgErrors
{
fn from(error: zune_psd::errors::PSDDecodeErrors) -> Self
{
ImgErrors::PSDDecodeErrors(error)
}
}
#[cfg(feature = "qoi")]
impl From<zune_qoi::QoiErrors> for ImgErrors
{
fn from(error: zune_qoi::QoiErrors) -> Self
{
ImgErrors::QoiDecodeErrors(error)
}
}
impl From<String> for ImgErrors
{
fn from(s: String) -> ImgErrors
{
ImgErrors::GenericString(s)
}
}
impl From<&'static str> for ImgErrors
{
fn from(s: &'static str) -> ImgErrors
{
ImgErrors::GenericStr(s)
}
}