Skip to main content

capsule_core/media/
error.rs

1//! The typed failure set of the still pipeline.
2//!
3//! Every one of these is a *report*, never a rejection: Capsule is a backup tool, so an original
4//! whose pixels cannot be read is still imported as a signed, encrypted, `verify_asset`-accepting
5//! asset. What varies is only whether a placeholder and a thumbnail could be produced beside it.
6//! The point of the enum is that the reasons stay apart — a missing codec is a known, deferred
7//! gap, while a *supported* format that fails to decode is a defect somebody should look at.
8
9use thiserror::Error;
10
11use super::detect::StillFormat;
12use crate::derivative_format::DerivativeFormat;
13
14/// Which direction of a codec a format was needed for. A build can decode a format it cannot
15/// encode (every format here except WebP) and the message has to say which half is missing.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum FormatOp {
18    /// Reading pixels out of the format.
19    Decode,
20    /// Writing pixels into the format.
21    Encode,
22}
23
24impl FormatOp {
25    /// The lowercase word used in log fields and messages.
26    pub const fn as_str(self) -> &'static str {
27        match self {
28            Self::Decode => "decode",
29            Self::Encode => "encode",
30        }
31    }
32}
33
34/// Why the still pipeline could not produce what was asked of it.
35#[derive(Debug, Clone, PartialEq, Eq, Error)]
36pub enum MediaError {
37    /// The format was identified and this build links no codec for it in that direction —
38    /// HEIC, AVIF and the RAW families for decode; everything but WebP for encode. The
39    /// **expected** gap: the format table is honest about it and
40    /// [`StillFormat::is_decodable`](super::StillFormat::is_decodable) is the same predicate the
41    /// pipeline gates on, so this is reached only when a caller bypasses that gate.
42    #[error("this build links no {} codec for {format}", op.as_str())]
43    UnsupportedFormat {
44        /// The format that was identified.
45        format: StillFormat,
46        /// Which half of the codec was missing.
47        op: FormatOp,
48    },
49    /// The bytes are not any still image Capsule models — a video, an XMP sidecar, an SVG, or
50    /// noise. Distinct from [`UnsupportedFormat`](Self::UnsupportedFormat): there is nothing to
51    /// defer, because there is no still here to decode later either.
52    #[error("not a still image Capsule models")]
53    NotAStillImage,
54    /// A format this build *does* decode did not decode these particular bytes — truncation,
55    /// corruption, or a decoder bug. `detail` is the underlying decoder's message, flattened to
56    /// a `String` (rather than held as a `#[source]`) so this type stays `Clone + PartialEq` and
57    /// so no pre-1.0 dependency type appears in Capsule's public error shape.
58    #[error("decoding {format} failed: {detail}")]
59    Decode {
60        /// The format that was being decoded.
61        format: StillFormat,
62        /// The decoder's own message.
63        detail: String,
64    },
65    /// A derivative encode failed. Same shape and same reasoning as
66    /// [`Decode`](Self::Decode).
67    #[error("encoding {format} failed: {detail}")]
68    Encode {
69        /// The derivative format that was being written.
70        format: DerivativeFormat,
71        /// The encoder's own message.
72        detail: String,
73    },
74    /// The decoded frame has a zero dimension. Guarded rather than trusted because
75    /// [`crate::lqip::Lqip::encode`] and the downscale both need a non-empty frame, and a
76    /// hand-crafted header can claim one.
77    #[error("decoded frame has a zero dimension ({width}x{height})")]
78    ZeroDimension {
79        /// The width the decoder reported.
80        width: u32,
81        /// The height the decoder reported.
82        height: u32,
83    },
84    /// The header claims more pixels than [`MAX_DECODE_PIXELS`](super::MAX_DECODE_PIXELS)
85    /// allows, and the decode was refused **before** allocating.
86    ///
87    /// This is the decode-bomb guard, and it has to be a pre-decode check rather than a
88    /// post-decode sanity assert: `rawshift-image` decodes to interleaved RGB `u16`, i.e. six
89    /// bytes per pixel, so a 60000x60000 PNG asks for ~21 GB inside the decoder before Capsule
90    /// ever sees a buffer.
91    #[error("{pixels} pixels exceeds the {limit}-pixel decode budget")]
92    PixelBudgetExceeded {
93        /// The pixel count the header claims, or, for a frame refused after decode because its
94        /// header understated it, the decoded frame's.
95        pixels: u64,
96        /// The budget in force.
97        limit: u64,
98    },
99    /// The decoder's buffer length does not match the dimensions it reported. `RgbImage`
100    /// validates this itself at the pinned revision; it is rechecked at the boundary rather than
101    /// assumed across bumps of a pre-1.0 dependency.
102    #[error(
103        "{format} decoder returned {actual} samples for {width}x{height} (expected {expected})"
104    )]
105    BufferLengthMismatch {
106        /// The format that was decoded.
107        format: StillFormat,
108        /// The width the decoder reported.
109        width: u32,
110        /// The height the decoder reported.
111        height: u32,
112        /// The sample count the dimensions imply.
113        expected: u128,
114        /// The sample count the decoder actually returned.
115        actual: u128,
116    },
117    /// Signing or sealing a derivative manifest failed — a hardware device signer refused, or
118    /// the album's write-tier key for this epoch is missing.
119    ///
120    /// **The one derivative failure that is not about pixels**, and the reason it has its own
121    /// variant rather than being folded into [`Encode`](Self::Encode): every other error here
122    /// says "this asset has no thumbnail", which an import survives, while this one says the
123    /// *workspace* cannot author a signed record — the same fault that would stop the asset's
124    /// own manifest. The import path propagates this and degrades on everything else, and it can
125    /// only tell them apart if the type does.
126    #[error("signing the derivative manifest failed: {detail}")]
127    Sign {
128        /// The underlying crypto error's message.
129        detail: String,
130    },
131    /// A third-party decoder panicked and the unwind was caught at the pipeline boundary.
132    ///
133    /// A pre-1.0 decoder fed untrusted bytes is exactly the place a panic is plausible, and an
134    /// import must never abort over a thumbnail. Reported as a decode failure rather than
135    /// swallowed, because a panic is a defect worth seeing.
136    #[error("the decoder panicked")]
137    DecoderPanic,
138}