Skip to main content

rust_j2k/
error.rs

1//! Error type for the decoder. One flat enum; each stage maps its failures to
2//! the variant that names the layer, so a caller can tell a malformed header
3//! from an unsupported feature from a Tier-1 decode fault.
4
5use core::fmt;
6
7/// Crate result alias.
8pub type Result<T> = core::result::Result<T, Error>;
9
10/// A decode failure, tagged by the pipeline stage that raised it.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum Error {
13    /// Not a JPEG 2000 codestream, or a marker segment was truncated / malformed.
14    Codestream(String),
15    /// A required marker (SIZ / COD / QCD …) was missing or carried bad fields.
16    Marker(String),
17    /// A valid-but-out-of-scope feature for the GRIB2 subset (JP2 boxes,
18    /// multiple components, a color transform, an unimplemented progression).
19    Unsupported(String),
20    /// Tier-2 packet or tag-tree parsing failed.
21    Packet(String),
22    /// Tier-1 (MQ arithmetic / EBCOT bit-plane) decode failed.
23    Tier1(String),
24    /// Declared geometry and decoded sample counts disagreed.
25    Inconsistent(String),
26}
27
28impl fmt::Display for Error {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Error::Codestream(m) => write!(f, "codestream: {m}"),
32            Error::Marker(m) => write!(f, "marker: {m}"),
33            Error::Unsupported(m) => write!(f, "unsupported: {m}"),
34            Error::Packet(m) => write!(f, "packet: {m}"),
35            Error::Tier1(m) => write!(f, "tier-1: {m}"),
36            Error::Inconsistent(m) => write!(f, "inconsistent: {m}"),
37        }
38    }
39}
40
41impl std::error::Error for Error {}