1use core::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum DecodeError {
8 Parse(ParseError),
10 Format(FormatError),
12 Segment(SegmentError),
14 Huffman(HuffmanError),
16 Region(RegionError),
18 Template(TemplateError),
20 Symbol(SymbolError),
22 Overflow,
24 Unsupported,
26}
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum ParseError {
31 UnexpectedEof,
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum FormatError {
38 InvalidHeader,
40 ReservedBits,
42 MissingPageInfo,
44 UnknownPageHeight,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum SegmentError {
51 UnknownType,
53 InvalidReferredCount,
55 InvalidReference,
57 MissingEndMarker,
59 MissingPatternDictionary,
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum HuffmanError {
66 InvalidCode,
68 InvalidSelection,
70 MissingTables,
72 UnexpectedOob,
74 MalformedTable,
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum RegionError {
83 InvalidCombinationOperator,
85 InvalidDimension,
87 GrayScaleOutOfRange,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum TemplateError {
94 Invalid,
96 InvalidAtPixel,
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102pub enum SymbolError {
103 NoSymbols,
105 TooManySymbols,
107 OutOfRange,
109 UnexpectedOob,
111 Invalid,
113}
114
115impl fmt::Display for DecodeError {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 match self {
118 Self::Parse(e) => write!(f, "{e}"),
119 Self::Format(e) => write!(f, "{e}"),
120 Self::Segment(e) => write!(f, "{e}"),
121 Self::Huffman(e) => write!(f, "{e}"),
122 Self::Region(e) => write!(f, "{e}"),
123 Self::Template(e) => write!(f, "{e}"),
124 Self::Symbol(e) => write!(f, "{e}"),
125 Self::Overflow => write!(f, "arithmetic overflow"),
126 Self::Unsupported => write!(f, "unsupported feature"),
127 }
128 }
129}
130
131impl fmt::Display for ParseError {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 match self {
134 Self::UnexpectedEof => write!(f, "unexpected end of input"),
135 }
136 }
137}
138
139impl fmt::Display for FormatError {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 match self {
142 Self::InvalidHeader => write!(f, "invalid JBIG2 file header"),
143 Self::ReservedBits => write!(f, "reserved bits must be zero"),
144 Self::MissingPageInfo => write!(f, "missing page information segment"),
145 Self::UnknownPageHeight => write!(f, "page height unknown with no stripe segments"),
146 }
147 }
148}
149
150impl fmt::Display for SegmentError {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 match self {
153 Self::UnknownType => write!(f, "unknown or reserved segment type"),
154 Self::InvalidReferredCount => write!(f, "invalid referred-to segment count"),
155 Self::InvalidReference => write!(f, "segment refers to larger segment number"),
156 Self::MissingEndMarker => write!(f, "missing end marker for unknown-length region"),
157 Self::MissingPatternDictionary => write!(f, "missing required pattern dictionary"),
158 }
159 }
160}
161
162impl fmt::Display for HuffmanError {
163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 match self {
165 Self::InvalidCode => write!(f, "invalid Huffman code"),
166 Self::InvalidSelection => write!(f, "invalid Huffman table selection"),
167 Self::MissingTables => write!(f, "not enough referred Huffman tables"),
168 Self::UnexpectedOob => write!(f, "unexpected out-of-band value"),
169 Self::MalformedTable => write!(
170 f,
171 "malformed Huffman table: code branches through a leaf node"
172 ),
173 }
174 }
175}
176
177impl fmt::Display for RegionError {
178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 match self {
180 Self::InvalidCombinationOperator => write!(f, "invalid combination operator"),
181 Self::InvalidDimension => write!(f, "invalid dimension value"),
182 Self::GrayScaleOutOfRange => write!(f, "gray-scale value exceeds pattern count"),
183 }
184 }
185}
186
187impl fmt::Display for TemplateError {
188 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 match self {
190 Self::Invalid => write!(f, "invalid template value"),
191 Self::InvalidAtPixel => write!(f, "invalid adaptive template pixel location"),
192 }
193 }
194}
195
196impl fmt::Display for SymbolError {
197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198 match self {
199 Self::NoSymbols => write!(f, "no symbols available"),
200 Self::OutOfRange => write!(f, "symbol ID out of range"),
201 Self::UnexpectedOob => write!(f, "unexpected out-of-band value"),
202 Self::TooManySymbols => write!(f, "symbol dictionary contains too many symbols"),
203 Self::Invalid => write!(f, "invalid symbol encountered"),
204 }
205 }
206}
207
208impl core::error::Error for DecodeError {}
209impl core::error::Error for ParseError {}
210impl core::error::Error for FormatError {}
211impl core::error::Error for SegmentError {}
212impl core::error::Error for HuffmanError {}
213impl core::error::Error for RegionError {}
214impl core::error::Error for TemplateError {}
215impl core::error::Error for SymbolError {}
216
217impl From<ParseError> for DecodeError {
218 fn from(e: ParseError) -> Self {
219 Self::Parse(e)
220 }
221}
222
223impl From<FormatError> for DecodeError {
224 fn from(e: FormatError) -> Self {
225 Self::Format(e)
226 }
227}
228
229impl From<SegmentError> for DecodeError {
230 fn from(e: SegmentError) -> Self {
231 Self::Segment(e)
232 }
233}
234
235impl From<HuffmanError> for DecodeError {
236 fn from(e: HuffmanError) -> Self {
237 Self::Huffman(e)
238 }
239}
240
241impl From<RegionError> for DecodeError {
242 fn from(e: RegionError) -> Self {
243 Self::Region(e)
244 }
245}
246
247impl From<TemplateError> for DecodeError {
248 fn from(e: TemplateError) -> Self {
249 Self::Template(e)
250 }
251}
252
253impl From<SymbolError> for DecodeError {
254 fn from(e: SymbolError) -> Self {
255 Self::Symbol(e)
256 }
257}
258
259pub type Result<T> = core::result::Result<T, DecodeError>;
261
262macro_rules! bail {
263 ($err:expr) => {
264 return Err($err.into())
265 };
266}
267
268macro_rules! err {
269 ($err:expr) => {
270 Err($err.into())
271 };
272}
273
274pub(crate) use bail;
275pub(crate) use err;