1#[derive(Debug)]
2#[non_exhaustive]
3pub enum Error {
4 Bitstream(jxl_bitstream::Error),
5 Decoder(jxl_coding::Error),
6 Buffer(jxl_grid::Error),
7 Modular(jxl_modular::Error),
8 VarDct(jxl_vardct::Error),
9 InvalidTocPermutation,
10 IncompleteFrameData { field: &'static str },
11 OutOfMemory,
12 HadError,
13}
14
15impl From<jxl_bitstream::Error> for Error {
16 fn from(err: jxl_bitstream::Error) -> Self {
17 Self::Bitstream(err)
18 }
19}
20
21impl From<jxl_coding::Error> for Error {
22 fn from(err: jxl_coding::Error) -> Self {
23 Self::Decoder(err)
24 }
25}
26
27impl From<jxl_grid::Error> for Error {
28 fn from(err: jxl_grid::Error) -> Self {
29 Self::Buffer(err)
30 }
31}
32
33impl From<jxl_modular::Error> for Error {
34 fn from(err: jxl_modular::Error) -> Self {
35 Self::Modular(err)
36 }
37}
38
39impl From<jxl_vardct::Error> for Error {
40 fn from(err: jxl_vardct::Error) -> Self {
41 Self::VarDct(err)
42 }
43}
44
45impl From<std::collections::TryReserveError> for Error {
46 fn from(_: std::collections::TryReserveError) -> Self {
47 Self::OutOfMemory
48 }
49}
50
51impl std::fmt::Display for Error {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 match self {
54 Self::Bitstream(err) => write!(f, "bitstream error: {}", err),
55 Self::Decoder(err) => write!(f, "entropy decoder error: {}", err),
56 Self::Buffer(err) => write!(f, "{}", err),
57 Self::Modular(err) => write!(f, "modular stream error: {}", err),
58 Self::VarDct(err) => write!(f, "vardct error: {}", err),
59 Self::InvalidTocPermutation => write!(f, "invalid TOC permutation"),
60 Self::IncompleteFrameData { field } => {
61 write!(f, "incomplete frame data: {} is missing", field)
62 }
63 Self::OutOfMemory => write!(f, "out of memory"),
64 Self::HadError => write!(f, "previous parsing errored"),
65 }
66 }
67}
68
69impl std::error::Error for Error {
70 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71 match self {
72 Self::Bitstream(err) => Some(err),
73 Self::Decoder(err) => Some(err),
74 Self::Buffer(err) => Some(err),
75 Self::Modular(err) => Some(err),
76 Self::VarDct(err) => Some(err),
77 _ => None,
78 }
79 }
80}
81
82impl Error {
83 pub fn unexpected_eof(&self) -> bool {
85 let bitstream_err = match self {
86 Self::Bitstream(b)
87 | Self::Decoder(jxl_coding::Error::Bitstream(b))
88 | Self::Modular(jxl_modular::Error::Decoder(jxl_coding::Error::Bitstream(b)))
89 | Self::Modular(jxl_modular::Error::Bitstream(b))
90 | Self::VarDct(jxl_vardct::Error::Bitstream(b))
91 | Self::VarDct(jxl_vardct::Error::Decoder(jxl_coding::Error::Bitstream(b)))
92 | Self::VarDct(jxl_vardct::Error::Modular(jxl_modular::Error::Bitstream(b)))
93 | Self::VarDct(jxl_vardct::Error::Modular(jxl_modular::Error::Decoder(
94 jxl_coding::Error::Bitstream(b),
95 ))) => b,
96 _ => return false,
97 };
98 if let jxl_bitstream::Error::Io(e) = bitstream_err {
99 e.kind() == std::io::ErrorKind::UnexpectedEof
100 } else {
101 false
102 }
103 }
104}
105
106pub type Result<T> = std::result::Result<T, Error>;