cadmpeg_core/decode/error.rs
1// SPDX-License-Identifier: Apache-2.0
2//! Resource-failure types shared by the budget and [`CodecError`].
3//!
4//! [`CodecError`]: crate::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 /// Bytes held by scoped materializations.
16 MaterializedBytes,
17 /// Bytes retained for the decode session outside expansion output.
18 RetainedBytes,
19 /// Admitted semantic or native entities.
20 Entities,
21 /// Admitted items across decoded collections.
22 CollectionItems,
23 /// Active recursive nesting depth.
24 RecursionDepth,
25 /// Algorithm work units.
26 WorkUnits,
27 /// A codec-local resource dimension.
28 Codec(&'static str),
29}
30
31/// Why a resource request failed.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum ResourceFailure {
34 /// Policy refused: the request would exceed the allowance.
35 BudgetExceeded,
36 /// The allocator refused, surfaced via `try_reserve`.
37 AllocationFailed,
38}
39
40/// An offset qualified by its address space.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct SourceLocation {
43 /// The space the offset indexes.
44 pub space: SpaceId,
45 /// The absolute offset within that space.
46 pub offset: u64,
47}
48
49/// A resource refusal.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct ResourceLimit {
52 /// The dimension that refused the request.
53 pub dimension: ResourceDimension,
54 /// Whether policy or the allocator refused.
55 pub reason: ResourceFailure,
56 /// The allowance in force.
57 pub limit: u64,
58 /// The amount already charged before this request.
59 pub used: u64,
60 /// The saturating size of the request that failed.
61 pub additional: u64,
62 /// The operation that failed, as a static label.
63 pub operation: &'static str,
64}
65
66impl ResourceLimit {
67 /// Reports an allocator refusal before any resource is charged.
68 #[must_use]
69 pub const fn allocation_failed(
70 dimension: ResourceDimension,
71 limit: u64,
72 additional: u64,
73 operation: &'static str,
74 ) -> Self {
75 Self {
76 dimension,
77 reason: ResourceFailure::AllocationFailed,
78 limit,
79 used: 0,
80 additional,
81 operation,
82 }
83 }
84}