Skip to main content

cadmpeg_ir/decode/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Resource-failure types shared by the budget and [`CodecError`].
3//!
4//! [`CodecError`]: crate::codec::CodecError
5
6use super::space::SpaceId;
7
8/// Which resource a limit governs.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum ResourceDimension {
11    /// Physical input bytes read at the root.
12    InputBytes,
13    /// Bytes produced by decompression.
14    DecompressedBytes,
15}
16
17/// Why a resource request failed.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum ResourceFailure {
20    /// Policy refused: the request would exceed the allowance.
21    BudgetExceeded,
22    /// The allocator refused, surfaced via `try_reserve`.
23    AllocationFailed,
24}
25
26/// The extent a limit applies to.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum LimitScope {
29    /// The whole decode.
30    Global,
31    /// One expansion.
32    PerExpand,
33}
34
35/// An offset qualified by its address space.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub struct SourceLocation {
38    /// The space the offset indexes.
39    pub space: SpaceId,
40    /// The absolute offset within that space.
41    pub offset: u64,
42}
43
44/// Allocation-free context attached to a failure.
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub struct ErrorContext {
47    /// The operation that failed, as a static label.
48    pub operation: &'static str,
49    /// Where it failed, when a location is known.
50    pub location: Option<SourceLocation>,
51}
52
53/// A resource refusal.
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub struct ResourceLimit {
56    /// The dimension that refused the request.
57    pub dimension: ResourceDimension,
58    /// Whether policy or the allocator refused.
59    pub reason: ResourceFailure,
60    /// The extent the limit applies to.
61    pub scope: LimitScope,
62    /// The allowance in force.
63    pub limit: u64,
64    /// The amount already charged before this request.
65    pub used: u64,
66    /// The saturating size of the request that failed.
67    pub additional: u64,
68    /// Static context for the failure.
69    pub context: ErrorContext,
70}