Skip to main content

edgefirst_image/
error.rs

1// SPDX-FileCopyrightText: Copyright 2025 Au-Zone Technologies
2// SPDX-License-Identifier: Apache-2.0
3
4pub 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 {}