codec_cbor/error.rs
1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use thiserror::Error;
6
7/// Error returned when decoding fails to conform to the DAG-CBOR profile.
8///
9/// Decoding fails closed: any malformed or non-canonical input yields one of
10/// these variants rather than a partial or best-effort value.
11#[derive(Debug, Clone, Copy, Error, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum CborError {
14 /// Extra bytes remain after decoding the top-level value.
15 #[error("CBOR: trailing bytes after top-level value")]
16 TrailingBytes,
17
18 /// Input ended before the current value was fully decoded.
19 #[error("CBOR: unexpected end of input")]
20 UnexpectedEnd,
21
22 /// The encoded input exceeded the absolute DAG-CBOR boundary cap.
23 #[error("CBOR: input too large")]
24 InputTooLarge,
25
26 /// Encoding would exceed the absolute DAG-CBOR boundary cap.
27 #[error("CBOR: encoded output too large")]
28 OutputTooLarge,
29
30 /// Computing an offset into the input would overflow.
31 #[error("CBOR: offset arithmetic overflow")]
32 OffsetOverflow,
33
34 /// A declared length is too large to represent as a platform `usize`.
35 #[error("CBOR: length does not fit in platform usize")]
36 LengthTooLarge,
37
38 /// An encoded integer falls outside the supported `i64` range.
39 #[error("CBOR: integer is outside supported i64 range")]
40 IntegerOutOfRange,
41
42 /// An integer used a longer-than-minimal (non-canonical) encoding.
43 #[error("CBOR: non-canonical integer encoding")]
44 NonCanonicalInteger,
45
46 /// The numeric argument (length/value) bytes were truncated.
47 #[error("CBOR: truncated numeric argument")]
48 TruncatedArgument,
49
50 /// A byte string ended before its declared length was read.
51 #[error("CBOR: truncated byte string")]
52 TruncatedBytes,
53
54 /// A text string contained bytes that are not valid UTF-8.
55 #[error("CBOR: invalid UTF-8 string")]
56 InvalidUtf8,
57
58 /// A map key was not a text string, which DAG-CBOR requires.
59 #[error("CBOR: map key must be text string")]
60 MapKeyMustBeString,
61
62 /// A map contained the same text key more than once.
63 ///
64 /// Duplicate keys make the data model ambiguous and would cause the
65 /// encoder to produce bytes that the canonical decoder must reject.
66 #[error("CBOR: duplicate map key")]
67 DuplicateMapKey,
68
69 /// Map keys were not in the canonical (sorted) order DAG-CBOR requires.
70 #[error("CBOR: map keys out of canonical order")]
71 MapKeysOutOfOrder,
72
73 /// A CBOR simple value not permitted by the DAG-CBOR profile was found.
74 #[error("CBOR: simple value not allowed in DAG-CBOR")]
75 DisallowedSimpleValue {
76 /// The CBOR simple-value code that was rejected.
77 value: u64,
78 },
79
80 /// A CBOR major type not permitted by the DAG-CBOR profile was found.
81 #[error("CBOR: major type not allowed in DAG-CBOR")]
82 DisallowedMajorType {
83 /// The CBOR major-type number that was rejected.
84 major: u8,
85 },
86
87 /// An oversized or indefinite-length additional-info value was found.
88 #[error("CBOR: value too large or indefinite length not allowed")]
89 UnsupportedAdditionalInfo,
90
91 /// A declared array/map element count is larger than the remaining
92 /// input could possibly satisfy. Rejected before allocating so an
93 /// attacker-chosen length prefix cannot drive an out-of-memory abort.
94 #[error("CBOR: declared container length exceeds remaining input")]
95 ContainerLengthExceedsInput,
96
97 /// Nesting exceeded [`MAX_NESTING_DEPTH`](crate::MAX_NESTING_DEPTH).
98 /// Bounds recursion so a deeply nested input cannot overflow the stack.
99 #[error("CBOR: nesting depth limit exceeded")]
100 DepthExceeded,
101}