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 retained for the decode session outside expansion output.
16 RetainedBytes,
17 /// Admitted semantic or native entities.
18 Entities,
19 /// Active recursive nesting depth.
20 RecursionDepth,
21}
22
23/// Why a resource request failed.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum ResourceFailure {
26 /// Policy refused: the request would exceed the allowance.
27 BudgetExceeded,
28 /// The allocator refused, surfaced via `try_reserve`.
29 AllocationFailed,
30}
31
32/// An offset qualified by its address space.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub struct SourceLocation {
35 /// The space the offset indexes.
36 pub space: SpaceId,
37 /// The absolute offset within that space.
38 pub offset: u64,
39}
40
41/// A resource refusal.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub struct ResourceLimit {
44 /// The dimension that refused the request.
45 pub dimension: ResourceDimension,
46 /// Whether policy or the allocator refused.
47 pub reason: ResourceFailure,
48 /// The allowance in force.
49 pub limit: u64,
50 /// The amount already charged before this request.
51 pub used: u64,
52 /// The saturating size of the request that failed.
53 pub additional: u64,
54 /// The operation that failed, as a static label.
55 pub operation: &'static str,
56}
57
58impl ResourceLimit {
59 /// Reports an allocator refusal before any resource is charged.
60 #[must_use]
61 pub const fn allocation_failed(
62 dimension: ResourceDimension,
63 limit: u64,
64 additional: u64,
65 operation: &'static str,
66 ) -> Self {
67 Self {
68 dimension,
69 reason: ResourceFailure::AllocationFailed,
70 limit,
71 used: 0,
72 additional,
73 operation,
74 }
75 }
76}