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