Skip to main content

cbor_core/
error.rs

1use std::{fmt, io, string::FromUtf8Error};
2
3use crate::DataType;
4
5/// Errors produced by this crate.
6///
7/// Errors fall into three categories:
8///
9/// **Decoding errors** are returned when input cannot be parsed as a valid CBOR
10/// value, whether the input is binary, hex, or diagnostic notation. Produced by
11/// [`Value::decode`](crate::Value::decode),
12/// [`Value::decode_hex`](crate::Value::decode_hex),
13/// [`Value::read_from`](crate::Value::read_from),
14/// [`Value::read_hex_from`](crate::Value::read_hex_from),
15/// `Value::from_str` (via [`FromStr`](std::str::FromStr)),
16/// and the iterators returned by [`DecodeOptions`](crate::DecodeOptions):
17/// [`Malformed`](Self::Malformed), [`NonDeterministic`](Self::NonDeterministic),
18/// [`UnexpectedEof`](Self::UnexpectedEof), [`LengthTooLarge`](Self::LengthTooLarge),
19/// [`NestingTooDeep`](Self::NestingTooDeep),
20/// [`InvalidUtf8`](Self::InvalidUtf8), [`InvalidHex`](Self::InvalidHex),
21/// [`InvalidBase64`](Self::InvalidBase64), [`InvalidFormat`](Self::InvalidFormat).
22///
23/// [`Malformed`](Self::Malformed) and [`NonDeterministic`](Self::NonDeterministic)
24/// apply to binary and hex input; [`InvalidFormat`](Self::InvalidFormat) is the
25/// catch-all for diagnostic-notation syntax errors and also signals trailing
26/// data after a complete single-item decode.
27///
28/// **Accessor errors** are returned by the `to_*`, `as_*`, and `into_*`
29/// methods on [`Value`](crate::Value) when the value does not match the requested type:
30/// [`IncompatibleType`](Self::IncompatibleType), [`Overflow`](Self::Overflow),
31/// [`NegativeUnsigned`](Self::NegativeUnsigned), [`Precision`](Self::Precision),
32/// [`InvalidSimpleValue`](Self::InvalidSimpleValue).
33///
34/// **Validation errors** are returned during construction of typed helpers
35/// like [`DateTime`](crate::DateTime) and [`EpochTime`](crate::EpochTime):
36/// [`InvalidFormat`](Self::InvalidFormat) (reused from the decoding category)
37/// and [`InvalidValue`](Self::InvalidValue).
38///
39/// `Error` is `Copy`, `Eq`, `Ord`, and `Hash`, so it can be matched,
40/// compared, and used as a map key without allocation. I/O errors are
41/// handled separately by [`IoError`], which wraps either an `Error` or
42/// a [`std::io::Error`]. This separation keeps `Error` small and
43/// `Copy`-able while still supporting streaming operations that can
44/// fail with I/O problems.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
46#[non_exhaustive]
47pub enum Error {
48    // --- Decoding errors ---
49    //
50    /// Binary CBOR data is structurally broken.
51    Malformed,
52    /// CBOR encoding is valid but not deterministic (non-shortest form, unsorted map keys, etc.).
53    NonDeterministic,
54    /// Input ended before a complete data item was read.
55    UnexpectedEof,
56    /// Declared length exceeds addressable memory or reasonable size.
57    LengthTooLarge,
58    /// Nesting depth of arrays, maps, or tags exceeds the recursion limit.
59    NestingTooDeep,
60    /// Text string contains invalid UTF-8.
61    InvalidUtf8,
62    /// Hex input contains invalid characters.
63    InvalidHex,
64    /// Base64 input contains invalid characters.
65    InvalidBase64,
66
67    // --- Accessor errors ---
68    //
69    /// Accessor called on a value of the wrong CBOR type.
70    IncompatibleType(DataType),
71    /// Integer does not fit in the target type.
72    Overflow,
73    /// Attempted to read a negative integer as an unsigned type.
74    NegativeUnsigned,
75    /// Float conversion would lose precision.
76    Precision,
77    /// Simple value number is in the reserved range 24-31.
78    InvalidSimpleValue,
79
80    // --- Validation errors ---
81    //
82    /// Textual input did not match the expected syntax. Used for
83    /// diagnostic-notation parse errors, trailing data after a
84    /// single-item decode, and invalid date/time strings.
85    InvalidFormat,
86    /// A value violates semantic constraints.
87    InvalidValue,
88}
89
90impl fmt::Display for Error {
91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92        match self {
93            Self::Malformed => write!(f, "malformed CBOR encoding"),
94            Self::NonDeterministic => write!(f, "non-deterministic CBOR encoding"),
95            Self::UnexpectedEof => write!(f, "unexpected end of input"),
96            Self::LengthTooLarge => write!(f, "length exceeds reasonable size"),
97            Self::NestingTooDeep => write!(f, "nesting exceeds recursion limit"),
98            Self::InvalidUtf8 => write!(f, "invalid UTF-8 in text string"),
99            Self::InvalidHex => write!(f, "invalid hex character"),
100            Self::InvalidBase64 => write!(f, "invalid base64 character"),
101            Self::IncompatibleType(t) => write!(f, "incompatible CBOR type {name}", name = t.name()),
102            Self::Overflow => write!(f, "integer overflow"),
103            Self::NegativeUnsigned => write!(f, "negative value for unsigned type"),
104            Self::Precision => write!(f, "float precision loss"),
105            Self::InvalidSimpleValue => write!(f, "invalid CBOR simple value"),
106            Self::InvalidFormat => write!(f, "invalid syntax for expected format"),
107            Self::InvalidValue => write!(f, "invalid value"),
108        }
109    }
110}
111
112impl std::error::Error for Error {}
113
114/// Convenience alias used throughout this crate.
115pub type Result<T> = std::result::Result<T, Error>;
116
117impl From<FromUtf8Error> for Error {
118    fn from(_error: FromUtf8Error) -> Self {
119        Self::InvalidUtf8
120    }
121}
122
123impl<T> From<Error> for Result<T> {
124    fn from(error: Error) -> Self {
125        Err(error)
126    }
127}
128
129/// Error type for IO related operations.
130///
131/// For streaming CBOR operations that may fail with either
132/// an I/O error or a data-level [`Error`].
133#[derive(Debug)]
134pub enum IoError {
135    /// Underlying I/O error from the reader or writer.
136    Io(io::Error),
137    /// CBOR-level error (malformed data, non-deterministic encoding, etc.).
138    Data(Error),
139}
140
141impl fmt::Display for IoError {
142    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143        match self {
144            Self::Io(e) => write!(f, "I/O error: {e}"),
145            Self::Data(e) => fmt::Display::fmt(e, f),
146        }
147    }
148}
149
150impl std::error::Error for IoError {
151    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
152        match self {
153            Self::Io(e) => Some(e),
154            Self::Data(e) => Some(e),
155        }
156    }
157}
158
159impl From<io::Error> for IoError {
160    fn from(error: io::Error) -> Self {
161        match error.kind() {
162            io::ErrorKind::UnexpectedEof => Error::UnexpectedEof.into(),
163            _other => Self::Io(error),
164        }
165    }
166}
167
168impl<E: Into<Error>> From<E> for IoError {
169    fn from(error: E) -> Self {
170        Self::Data(error.into())
171    }
172}
173
174impl<T> From<Error> for IoResult<T> {
175    fn from(error: Error) -> Self {
176        Err(IoError::Data(error))
177    }
178}
179
180/// Convenience alias for streaming CBOR operations.
181pub type IoResult<T> = std::result::Result<T, IoError>;
182
183pub(crate) trait WithEof {
184    fn is_eof(&self) -> bool;
185}
186
187impl WithEof for Error {
188    fn is_eof(&self) -> bool {
189        matches!(self, Error::UnexpectedEof)
190    }
191}
192
193impl WithEof for IoError {
194    fn is_eof(&self) -> bool {
195        matches!(self, IoError::Data(Error::UnexpectedEof))
196    }
197}