Skip to main content

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)]
12pub enum CborError {
13    /// Extra bytes remain after decoding the top-level value.
14    #[error("CBOR: trailing bytes after top-level value")]
15    TrailingBytes,
16
17    /// Input ended before the current value was fully decoded.
18    #[error("CBOR: unexpected end of input")]
19    UnexpectedEnd,
20
21    /// The encoded input exceeded the absolute DAG-CBOR boundary cap.
22    #[error("CBOR: input too large")]
23    InputTooLarge,
24
25    /// Encoding would exceed the absolute DAG-CBOR boundary cap.
26    #[error("CBOR: encoded output too large")]
27    OutputTooLarge,
28
29    /// Computing an offset into the input would overflow.
30    #[error("CBOR: offset arithmetic overflow")]
31    OffsetOverflow,
32
33    /// A declared length is too large to represent as a platform `usize`.
34    #[error("CBOR: length does not fit in platform usize")]
35    LengthTooLarge,
36
37    /// An encoded integer falls outside the supported `i64` range.
38    #[error("CBOR: integer is outside supported i64 range")]
39    IntegerOutOfRange,
40
41    /// An integer used a longer-than-minimal (non-canonical) encoding.
42    #[error("CBOR: non-canonical integer encoding")]
43    NonCanonicalInteger,
44
45    /// The numeric argument (length/value) bytes were truncated.
46    #[error("CBOR: truncated numeric argument")]
47    TruncatedArgument,
48
49    /// A byte string ended before its declared length was read.
50    #[error("CBOR: truncated byte string")]
51    TruncatedBytes,
52
53    /// A text string contained bytes that are not valid UTF-8.
54    #[error("CBOR: invalid UTF-8 string")]
55    InvalidUtf8,
56
57    /// A map key was not a text string, which DAG-CBOR requires.
58    #[error("CBOR: map key must be text string")]
59    MapKeyMustBeString,
60
61    /// Map keys were not in the canonical (sorted) order DAG-CBOR requires.
62    #[error("CBOR: map keys out of canonical order")]
63    MapKeysOutOfOrder,
64
65    /// A CBOR simple value not permitted by the DAG-CBOR profile was found.
66    #[error("CBOR: simple value not allowed in DAG-CBOR")]
67    DisallowedSimpleValue {
68        /// The CBOR simple-value code that was rejected.
69        value: u64,
70    },
71
72    /// A CBOR major type not permitted by the DAG-CBOR profile was found.
73    #[error("CBOR: major type not allowed in DAG-CBOR")]
74    DisallowedMajorType {
75        /// The CBOR major-type number that was rejected.
76        major: u8,
77    },
78
79    /// An oversized or indefinite-length additional-info value was found.
80    #[error("CBOR: value too large or indefinite length not allowed")]
81    UnsupportedAdditionalInfo,
82
83    /// A declared array/map element count is larger than the remaining
84    /// input could possibly satisfy. Rejected before allocating so an
85    /// attacker-chosen length prefix cannot drive an out-of-memory abort.
86    #[error("CBOR: declared container length exceeds remaining input")]
87    ContainerLengthExceedsInput,
88
89    /// Nesting exceeded [`MAX_NESTING_DEPTH`](crate::MAX_NESTING_DEPTH).
90    /// Bounds recursion so a deeply nested input cannot overflow the stack.
91    #[error("CBOR: nesting depth limit exceeded")]
92    DepthExceeded,
93}