Skip to main content

codec_cbor/
error.rs

1// SPDX-FileCopyrightText: 2026 ReallyMe LLC
2//
3// SPDX-License-Identifier: MIT OR 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 rejected simple-value code, or the additional-info code
77        /// (25, 26, or 27) for a rejected float. Never contains float payload bits.
78        value: u64,
79    },
80
81    /// A CBOR major type not permitted by the DAG-CBOR profile was found.
82    #[error("CBOR: major type not allowed in DAG-CBOR")]
83    DisallowedMajorType {
84        /// The CBOR major-type number that was rejected.
85        major: u8,
86    },
87
88    /// An oversized or indefinite-length additional-info value was found.
89    #[error("CBOR: value too large or indefinite length not allowed")]
90    UnsupportedAdditionalInfo,
91
92    /// A declared array/map element count is larger than the remaining
93    /// input could possibly satisfy. Rejected before allocating so an
94    /// attacker-chosen length prefix cannot drive an out-of-memory abort.
95    #[error("CBOR: declared container length exceeds remaining input")]
96    ContainerLengthExceedsInput,
97
98    /// Nesting exceeded [`MAX_NESTING_DEPTH`](crate::MAX_NESTING_DEPTH).
99    /// Bounds recursion so a deeply nested input cannot overflow the stack.
100    #[error("CBOR: nesting depth limit exceeded")]
101    DepthExceeded,
102
103    /// A document contained more semantic nodes than the profile allows.
104    #[error("CBOR: node limit exceeded")]
105    NodeLimitExceeded,
106
107    /// One array or map contained more entries than the profile allows.
108    #[error("CBOR: container entry limit exceeded")]
109    ContainerEntriesExceeded,
110}