jxl_oxide_cli/
error.rs

1#[derive(Debug)]
2#[non_exhaustive]
3pub enum Error {
4    ReadJxl(Box<dyn std::error::Error + Send + Sync + 'static>),
5    ReadIcc(std::io::Error),
6    WriteIcc(std::io::Error),
7    WriteImage(std::io::Error),
8    Render(Box<dyn std::error::Error + Send + Sync + 'static>),
9    Reconstruct(Box<dyn std::error::Error + Send + Sync + 'static>),
10    #[cfg(feature = "__ffmpeg")]
11    Ffmpeg {
12        msg: Option<&'static str>,
13        averror: Option<std::ffi::c_int>,
14    },
15}
16
17impl Error {
18    #[cfg(feature = "__ffmpeg")]
19    #[inline]
20    pub(crate) fn from_averror(averror: std::ffi::c_int) -> Self {
21        Self::Ffmpeg {
22            msg: None,
23            averror: Some(averror),
24        }
25    }
26
27    #[cfg(feature = "__ffmpeg")]
28    #[inline]
29    pub(crate) fn from_ffmpeg_msg(msg: &'static str) -> Self {
30        Self::Ffmpeg {
31            msg: Some(msg),
32            averror: None,
33        }
34    }
35}
36
37impl std::fmt::Display for Error {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            Error::ReadJxl(e) => write!(f, "failed reading JPEG XL image: {e}"),
41            Error::ReadIcc(e) => write!(f, "failed reading ICC profile: {e}"),
42            Error::WriteIcc(e) => write!(f, "failed writing ICC profile: {e}"),
43            Error::WriteImage(e) => write!(f, "failed writing output image: {e}"),
44            Error::Render(e) => write!(f, "failed to render image: {e}"),
45            Error::Reconstruct(e) => write!(f, "failed to reconstruct: {e}"),
46            #[cfg(feature = "__ffmpeg")]
47            Error::Ffmpeg { msg, averror } => {
48                write!(f, "FFmpeg error")?;
49                let e = averror.map(rusty_ffmpeg::ffi::av_err2str);
50                match (msg, e) {
51                    (None, None) => {}
52                    (Some(msg), None) => {
53                        write!(f, ": {msg}")?;
54                    }
55                    (None, Some(e)) => {
56                        write!(f, ": {e}")?;
57                    }
58                    (Some(msg), Some(e)) => {
59                        write!(f, ": {msg} ({e})")?;
60                    }
61                }
62
63                Ok(())
64            }
65        }
66    }
67}
68
69impl std::error::Error for Error {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        match self {
72            Error::ReadJxl(e) => Some(&**e),
73            Error::ReadIcc(e) => Some(e),
74            Error::WriteIcc(e) => Some(e),
75            Error::WriteImage(e) => Some(e),
76            Error::Render(e) => Some(&**e),
77            Error::Reconstruct(e) => Some(&**e),
78            #[cfg(feature = "__ffmpeg")]
79            Error::Ffmpeg { .. } => None,
80        }
81    }
82}