Skip to main content

rust_j2k/
error.rs

1//! Error type for the decoder. One flat enum; each failure maps to the variant
2//! that names its class, so a caller can tell a malformed input from an
3//! unsupported feature. Structural corruption anywhere in the pipeline —
4//! including packet headers and coded data — reports as [`Error::Codestream`].
5
6use core::fmt;
7
8/// Crate result alias.
9pub type Result<T> = core::result::Result<T, Error>;
10
11/// A decode failure, tagged by the class of fault.
12#[derive(Debug, Clone, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum Error {
15    /// Not a JPEG 2000 codestream, or its structure is broken: a truncated or
16    /// missing segment, lost sync, or corrupt packet / coded data.
17    Codestream(String),
18    /// A marker segment carried an illegal field encoding (a reserved value,
19    /// a bad length, an out-of-range field).
20    Marker(String),
21    /// A valid-but-not-decoded feature (a JP2 container, an HTJ2K codestream,
22    /// a Part 2 extension), or an input past one of the decoder's resource
23    /// guards.
24    Unsupported(String),
25    /// Declared geometry and decoded sample counts disagreed.
26    Inconsistent(String),
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Error::Codestream(m) => write!(f, "codestream: {m}"),
33            Error::Marker(m) => write!(f, "marker: {m}"),
34            Error::Unsupported(m) => write!(f, "unsupported: {m}"),
35            Error::Inconsistent(m) => write!(f, "inconsistent: {m}"),
36        }
37    }
38}
39
40impl std::error::Error for Error {}