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