cbor_smol/
error.rs

1#![allow(unused_variables)]
2
3use core::fmt::{Display, Formatter};
4use serde_core as serde;
5
6/// This is the Result type used by cbor-smol.
7pub type Result<T, Err = Error> = core::result::Result<T, Err>;
8
9/// This is the error type used by cbor-smol
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11#[repr(u8)]
12#[non_exhaustive]
13pub enum Error {
14    /// This is a feature that cbor-smol will never implement
15    WontImplement,
16    /// This is a feature that cbor-smol intends to support, but does not yet
17    NotYetImplemented,
18    /// The serialize buffer is full
19    SerializeBufferFull,
20    // /// The length of a sequence must be known
21    // SerializeSeqLengthUnknown,
22    /// Hit the end of buffer, expected more data
23    DeserializeUnexpectedEnd,
24    // /// Found a varint that didn't terminate. Is the usize too big for this platform?
25    // DeserializeBadVarint,
26    /// Found a bool that wasn't 0xf4 or 0xf5
27    DeserializeBadBool,
28    // /// Found an invalid unicode char
29    // DeserializeBadChar,
30    /// Tried to parse invalid utf-8
31    DeserializeBadUtf8,
32    // /// Found an Option discriminant that wasn't 0 or 1
33    // DeserializeBadOption,
34    // /// Found an enum discriminant that was > u32::max_value()
35    /// Could not parse an enum
36    DeserializeBadEnum,
37    // /// The original data was not well encoded
38    // DeserializeBadEncoding,
39    /// Expected a different major type
40    DeserializeBadMajor,
41    /// Expected a i8, was too large
42    DeserializeBadI8,
43    /// Expected a i16, was too large
44    DeserializeBadI16,
45    /// Expected a i32, was too large
46    DeserializeBadI32,
47    /// Expected a i64, was too large
48    DeserializeBadI64,
49    /// Expected a u8
50    DeserializeBadU8,
51    /// Expected a u16
52    DeserializeBadU16,
53    /// Expected a u32
54    DeserializeBadU32,
55    /// Expected a u64
56    DeserializeBadU64,
57    /// Expected a NULL marker
58    DeserializeExpectedNull,
59    /// Inexistent slice-to-array cast error. Used here to avoid calling unwrap.
60    InexistentSliceToArrayError,
61    /// Value may be valid, but not encoded in minimal way
62    DeserializeNonMinimal,
63    /// Serde Serialization Error
64    SerdeSerCustom,
65    /// Serde Deserialization Error
66    SerdeDeCustom,
67    /// Serde Missing required value
68    SerdeMissingField,
69}
70
71impl Display for Error {
72    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
73        use Error::*;
74        write!(
75            f,
76            "{}",
77            match self {
78                WontImplement => "This is a feature that cbor-smol will never implement",
79                NotYetImplemented => {
80                    "This is a feature that cbor-smol intends to support, but does not yet"
81                }
82                SerializeBufferFull => "The serialize buffer is full",
83                // SerializeSeqLengthUnknown => "The length of a sequence must be known",
84                DeserializeUnexpectedEnd => "Hit the end of buffer, expected more data",
85                // DeserializeBadVarint => {
86                //     "Found a varint that didn't terminate. Is the usize too big for this platform?"
87                // }
88                DeserializeBadBool => "Found a bool that wasn't 0xf4 or 0xf5",
89                // DeserializeBadChar => "Found an invalid unicode char",
90                DeserializeBadUtf8 => "Tried to parse invalid utf-8",
91                // DeserializeBadOption => "Found an Option discriminant that wasn't 0 or 1",
92                // DeserializeBadEnum => "Found an enum discriminant that was > u32::max_value()",
93                DeserializeBadEnum => "Could not parse an enum",
94                // DeserializeBadEncoding => "The original data was not well encoded",
95                DeserializeBadI8 => "Expected a i8",
96                DeserializeBadI16 => "Expected a i16",
97                DeserializeBadI32 => "Expected a i32",
98                DeserializeBadI64 => "Expected a i64",
99                DeserializeBadMajor => "Expected a different major type",
100                DeserializeBadU8 => "Expected a u8",
101                DeserializeBadU16 => "Expected a u16",
102                DeserializeBadU32 => "Expected a u32",
103                DeserializeBadU64 => "Expected a u64",
104                DeserializeExpectedNull => "Expected 0xf6",
105                InexistentSliceToArrayError => "",
106                DeserializeNonMinimal => "Value may be valid, but not encoded in minimal way",
107                SerdeSerCustom => "Serde Serialization Error",
108                SerdeDeCustom => "Serde Deserialization Error",
109                SerdeMissingField => "Serde Missing Required Field",
110            }
111        )
112    }
113}
114
115impl serde::ser::Error for Error {
116    fn custom<T>(_msg: T) -> Self
117    where
118        T: Display,
119    {
120        Error::SerdeSerCustom
121    }
122}
123
124impl serde::de::Error for Error {
125    fn custom<T>(msg: T) -> Self
126    where
127        T: Display,
128    {
129        // TODO: Would be helpful to log this to system logger
130        // This shows e.g.
131        // - missing fields
132        // - expected sequence, received X
133        // - etc.
134        //
135        // Particularly helpful would be better errors when receiving
136        // structures are undersized.
137        //
138        // E.g. if there is a `Bytes<N>` and more than N bytes are delivered,
139        // currently the error _msg: T is:
140        //
141        // `invalid length 297, expected a sequence`
142        //
143        info_now!("deser error: {}", &msg);
144        Error::SerdeDeCustom
145    }
146    fn missing_field(field: &'static str) -> Self {
147        info_now!("deser missing: {}", field);
148        Error::SerdeMissingField
149    }
150}
151
152impl serde::ser::StdError for Error {}