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