1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! Error type for the decoder. One flat enum; each failure maps to the variant
//! that names its class, so a caller can tell a malformed input from an
//! unsupported feature: [`Error::Codestream`] for structural damage,
//! [`Error::Marker`] for an invalid field value, [`Error::FileFormat`] for a
//! malformed JP2 box structure, [`Error::Unsupported`] for
//! a valid-but-out-of-subset feature, [`Error::Limit`] for an input past a
//! decoder resource guard, [`Error::InvalidOptions`] for bad caller options,
//! and [`Error::Inconsistent`] for a violated internal invariant. Structural
//! corruption anywhere in the pipeline — including packet headers and coded
//! data — reports as [`Error::Codestream`].
use fmt;
/// Crate result alias.
pub type Result<T> = Result;
/// A decode failure, tagged by the class of fault.
///
/// Match on the variant to decide what to do; the `String` inside is a
/// diagnostic for a log. Its wording can change between releases, so do not
/// parse it or compare it. Input that is not JPEG 2000 at all, empty input
/// and plain text included, is [`Error::Codestream`].
///
/// The variant also says what to tell a user:
///
/// | Variant | A message for a user |
/// | ------- | -------------------- |
/// | `Codestream`, `Marker`, `FileFormat` | The file is damaged, truncated or not a JPEG 2000 image. |
/// | `Unsupported` | The file uses a JPEG 2000 feature this decoder does not read. |
/// | `Limit` | The file is larger or costlier than the decoder allows by default. If the message names a `DecodeOptions` limit, it can be retried with a higher [`WorkLimit`](crate::WorkLimit). |
/// | `InvalidOptions` | The decode options are invalid, for any file (`WorkLimit::Raised(0)`, sRGB output with `raw_channels`) or for this one (a reduction past the file's levels). |
/// | `Inconsistent` | An internal error; please file a bug. |
///
/// Damage does not always show as the first row. A flipped bit in a header
/// field can read as an absurd image size (`Limit`), an unknown marker or a
/// feature the decoder does not read (`Unsupported`), so a corrupt file can
/// come back as any of those. Do not retry a `Limit` with
/// [`WorkLimit::Unlimited`](crate::WorkLimit::Unlimited) unless the source
/// is trusted.
///
/// ```
/// use rust_j2k::{Error, decode};
///
/// fn explain(error: &Error) -> &'static str {
/// match error {
/// Error::Codestream(_) | Error::Marker(_) | Error::FileFormat(_) => {
/// "the file is damaged, truncated or not a JPEG 2000 image"
/// }
/// Error::Unsupported(_) => "the file uses a feature this decoder does not read",
/// Error::Limit(_) => "the file is larger or costlier than the decoder allows",
/// Error::InvalidOptions(_) => "the decode options do not fit this file",
/// // The enum is `#[non_exhaustive]`: a later version can add variants.
/// _ => "the file could not be decoded",
/// }
/// }
///
/// let error = decode(b"").unwrap_err();
/// assert!(matches!(error, Error::Codestream(_)));
/// assert_eq!(
/// explain(&error),
/// "the file is damaged, truncated or not a JPEG 2000 image"
/// );
/// ```