1pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6#[derive(Debug)]
7pub enum Error {
8 Io(std::io::Error),
9 NotFound(String),
10 Library(libloading::Error),
11 JpegEncoding(jpeg_encoder::EncodingError),
12 JpegDecoding(zune_jpeg::errors::DecodeErrors),
13 PngDecoding(zune_png::error::PngDecodeErrors),
14 ResizeImageBuffer(fast_image_resize::ImageBufferError),
15 Resize(fast_image_resize::ResizeError),
16 Yuv(yuv::YuvError),
17 #[cfg(target_os = "linux")]
18 G2D(g2d_sys::Error),
19 Tensor(edgefirst_tensor::Error),
20 NotImplemented(String),
21 NotSupported(String),
22 InvalidShape(String),
23 #[cfg(target_os = "linux")]
24 #[cfg(feature = "opengl")]
25 EGL(khronos_egl::Error),
26 GLVersion(String),
27 #[cfg(target_os = "linux")]
28 #[cfg(feature = "opengl")]
29 EGLLoad(khronos_egl::LoadError<libloading::Error>),
30 #[cfg(target_os = "linux")]
31 #[cfg(feature = "opengl")]
32 GbmInvalidFd(gbm::InvalidFdError),
33 #[cfg(target_os = "linux")]
34 #[cfg(feature = "opengl")]
35 GbmFrontBuffer(gbm::FrontBufferError),
36 OpenGl(String),
37 Internal(String),
38 CropInvalid(String),
39 ForcedBackendUnavailable(String),
40 NoConverter,
41 NotAnImage,
42 UnsupportedFormat(String),
43}
44
45impl From<std::io::Error> for Error {
46 fn from(err: std::io::Error) -> Self {
47 Error::Io(err)
48 }
49}
50
51impl From<libloading::Error> for Error {
52 fn from(err: libloading::Error) -> Self {
53 Error::Library(err)
54 }
55}
56
57impl From<jpeg_encoder::EncodingError> for Error {
58 fn from(err: jpeg_encoder::EncodingError) -> Self {
59 Error::JpegEncoding(err)
60 }
61}
62
63impl From<zune_jpeg::errors::DecodeErrors> for Error {
64 fn from(err: zune_jpeg::errors::DecodeErrors) -> Self {
65 Error::JpegDecoding(err)
66 }
67}
68
69impl From<zune_png::error::PngDecodeErrors> for Error {
70 fn from(err: zune_png::error::PngDecodeErrors) -> Self {
71 Error::PngDecoding(err)
72 }
73}
74
75impl From<fast_image_resize::ImageBufferError> for Error {
76 fn from(err: fast_image_resize::ImageBufferError) -> Self {
77 Error::ResizeImageBuffer(err)
78 }
79}
80
81impl From<fast_image_resize::ResizeError> for Error {
82 fn from(err: fast_image_resize::ResizeError) -> Self {
83 Error::Resize(err)
84 }
85}
86
87impl From<yuv::YuvError> for Error {
88 fn from(err: yuv::YuvError) -> Self {
89 Error::Yuv(err)
90 }
91}
92
93#[cfg(target_os = "linux")]
94impl From<g2d_sys::Error> for Error {
95 fn from(err: g2d_sys::Error) -> Self {
96 Error::G2D(err)
97 }
98}
99
100impl From<edgefirst_tensor::Error> for Error {
101 fn from(err: edgefirst_tensor::Error) -> Self {
102 Error::Tensor(err)
103 }
104}
105
106#[cfg(target_os = "linux")]
107#[cfg(feature = "opengl")]
108impl From<khronos_egl::Error> for Error {
109 fn from(err: khronos_egl::Error) -> Self {
110 Error::EGL(err)
111 }
112}
113
114#[cfg(target_os = "linux")]
115#[cfg(feature = "opengl")]
116impl From<khronos_egl::LoadError<libloading::Error>> for Error {
117 fn from(err: khronos_egl::LoadError<libloading::Error>) -> Self {
118 Error::EGLLoad(err)
119 }
120}
121
122#[cfg(target_os = "linux")]
123#[cfg(feature = "opengl")]
124impl From<gbm::InvalidFdError> for Error {
125 fn from(err: gbm::InvalidFdError) -> Self {
126 Error::GbmInvalidFd(err)
127 }
128}
129
130#[cfg(target_os = "linux")]
131#[cfg(feature = "opengl")]
132impl From<gbm::FrontBufferError> for Error {
133 fn from(err: gbm::FrontBufferError) -> Self {
134 Error::GbmFrontBuffer(err)
135 }
136}
137
138#[cfg(target_os = "linux")]
139#[cfg(feature = "opengl")]
140impl From<gls::Error> for Error {
141 fn from(err: gls::Error) -> Self {
142 Error::OpenGl(err.to_string())
143 }
144}
145
146impl From<ndarray::ShapeError> for Error {
147 fn from(err: ndarray::ShapeError) -> Self {
148 Error::Internal(format!("{err}"))
149 }
150}
151
152impl From<fast_image_resize::CropBoxError> for Error {
153 fn from(err: fast_image_resize::CropBoxError) -> Self {
154 Error::Internal(format!("{err}"))
155 }
156}
157
158impl std::fmt::Display for Error {
159 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160 write!(f, "{self:?}")
161 }
162}
163
164impl std::error::Error for Error {}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169
170 #[test]
171 fn test_image_error_display() {
172 let e = Error::Io(std::io::Error::new(
173 std::io::ErrorKind::NotFound,
174 "file missing",
175 ));
176 let msg = e.to_string();
177 assert!(!msg.is_empty());
178 assert!(
179 msg.contains("Io") && msg.contains("file missing"),
180 "unexpected Io message: {msg}"
181 );
182
183 let e = Error::NotFound("texture.png".to_string());
184 let msg = e.to_string();
185 assert!(!msg.is_empty());
186 assert!(
187 msg.contains("NotFound") && msg.contains("texture.png"),
188 "unexpected NotFound message: {msg}"
189 );
190
191 let e = Error::Tensor(edgefirst_tensor::Error::InvalidSize(42));
192 let msg = e.to_string();
193 assert!(!msg.is_empty());
194 assert!(
195 msg.contains("Tensor") && msg.contains("InvalidSize"),
196 "unexpected Tensor message: {msg}"
197 );
198
199 let e = Error::NotImplemented("webp".to_string());
200 let msg = e.to_string();
201 assert!(!msg.is_empty());
202 assert!(
203 msg.contains("NotImplemented") && msg.contains("webp"),
204 "unexpected NotImplemented message: {msg}"
205 );
206
207 let e = Error::NotSupported("bmp format".to_string());
208 let msg = e.to_string();
209 assert!(!msg.is_empty());
210 assert!(
211 msg.contains("NotSupported") && msg.contains("bmp format"),
212 "unexpected NotSupported message: {msg}"
213 );
214
215 let e = Error::InvalidShape("wrong dims".to_string());
216 let msg = e.to_string();
217 assert!(!msg.is_empty());
218 assert!(
219 msg.contains("InvalidShape") && msg.contains("wrong dims"),
220 "unexpected InvalidShape message: {msg}"
221 );
222
223 let e = Error::GLVersion("4.5 required".to_string());
224 let msg = e.to_string();
225 assert!(!msg.is_empty());
226 assert!(
227 msg.contains("GLVersion") && msg.contains("4.5 required"),
228 "unexpected GLVersion message: {msg}"
229 );
230
231 let e = Error::OpenGl("shader compile failed".to_string());
232 let msg = e.to_string();
233 assert!(!msg.is_empty());
234 assert!(
235 msg.contains("OpenGl") && msg.contains("shader compile failed"),
236 "unexpected OpenGl message: {msg}"
237 );
238
239 let e = Error::Internal("unexpected state".to_string());
240 let msg = e.to_string();
241 assert!(!msg.is_empty());
242 assert!(
243 msg.contains("Internal") && msg.contains("unexpected state"),
244 "unexpected Internal message: {msg}"
245 );
246
247 let e = Error::CropInvalid("out of bounds".to_string());
248 let msg = e.to_string();
249 assert!(!msg.is_empty());
250 assert!(
251 msg.contains("CropInvalid") && msg.contains("out of bounds"),
252 "unexpected CropInvalid message: {msg}"
253 );
254
255 let e = Error::ForcedBackendUnavailable("g2d".to_string());
256 let msg = e.to_string();
257 assert!(!msg.is_empty());
258 assert!(
259 msg.contains("ForcedBackendUnavailable") && msg.contains("g2d"),
260 "unexpected ForcedBackendUnavailable message: {msg}"
261 );
262
263 let e = Error::NoConverter;
264 let msg = e.to_string();
265 assert!(!msg.is_empty());
266 assert!(
267 msg.contains("NoConverter"),
268 "unexpected NoConverter message: {msg}"
269 );
270
271 let e = Error::NotAnImage;
272 let msg = e.to_string();
273 assert!(!msg.is_empty());
274 assert!(
275 msg.contains("NotAnImage"),
276 "unexpected NotAnImage message: {msg}"
277 );
278
279 let e = Error::UnsupportedFormat("tiff".to_string());
280 let msg = e.to_string();
281 assert!(!msg.is_empty());
282 assert!(
283 msg.contains("UnsupportedFormat") && msg.contains("tiff"),
284 "unexpected UnsupportedFormat message: {msg}"
285 );
286 }
287}