postcard/
error.rs

1use core::fmt::{Display, Formatter};
2
3/// This is the error type used by Postcard
4#[derive(Clone, Debug, Eq, PartialEq)]
5#[cfg_attr(feature = "use-defmt", derive(defmt::Format))]
6#[non_exhaustive]
7pub enum Error {
8    /// This is a feature that postcard will never implement
9    WontImplement,
10    /// This is a feature that postcard intends to support, but does not yet
11    NotYetImplemented,
12    /// The serialize buffer is full
13    SerializeBufferFull,
14    /// The length of a sequence must be known
15    SerializeSeqLengthUnknown,
16    /// Hit the end of buffer, expected more data
17    DeserializeUnexpectedEnd,
18    /// Found a varint that didn't terminate. Is the usize too big for this platform?
19    DeserializeBadVarint,
20    /// Found a bool that wasn't 0 or 1
21    DeserializeBadBool,
22    /// Found an invalid unicode char
23    DeserializeBadChar,
24    /// Tried to parse invalid utf-8
25    DeserializeBadUtf8,
26    /// Found an Option discriminant that wasn't 0 or 1
27    DeserializeBadOption,
28    /// Found an enum discriminant that was > `u32::MAX`
29    DeserializeBadEnum,
30    /// The original data was not well encoded
31    DeserializeBadEncoding,
32    /// Bad CRC while deserializing
33    DeserializeBadCrc,
34    /// Serde Serialization Error
35    SerdeSerCustom,
36    /// Serde Deserialization Error
37    SerdeDeCustom,
38    /// Error while processing `collect_str` during serialization
39    CollectStrError,
40}
41
42impl Display for Error {
43    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
44        use Error::*;
45        write!(
46            f,
47            "{}",
48            match self {
49                WontImplement => "This is a feature that PostCard will never implement",
50                NotYetImplemented => {
51                    "This is a feature that Postcard intends to support, but does not yet"
52                }
53                SerializeBufferFull => "The serialize buffer is full",
54                SerializeSeqLengthUnknown => "The length of a sequence must be known",
55                DeserializeUnexpectedEnd => "Hit the end of buffer, expected more data",
56                DeserializeBadVarint => {
57                    "Found a varint that didn't terminate. Is the usize too big for this platform?"
58                }
59                DeserializeBadBool => "Found a bool that wasn't 0 or 1",
60                DeserializeBadChar => "Found an invalid unicode char",
61                DeserializeBadUtf8 => "Tried to parse invalid utf-8",
62                DeserializeBadOption => "Found an Option discriminant that wasn't 0 or 1",
63                DeserializeBadEnum => "Found an enum discriminant that was > u32::max_value()",
64                DeserializeBadEncoding => "The original data was not well encoded",
65                DeserializeBadCrc => "Bad CRC while deserializing",
66                SerdeSerCustom => "Serde Serialization Error",
67                SerdeDeCustom => "Serde Deserialization Error",
68                CollectStrError => "Error while processing `collect_str` during serialization",
69            }
70        )
71    }
72}
73
74/// This is the Result type used by Postcard.
75pub type Result<T> = ::core::result::Result<T, Error>;
76
77impl serde::ser::Error for Error {
78    fn custom<T>(_msg: T) -> Self
79    where
80        T: Display,
81    {
82        Error::SerdeSerCustom
83    }
84}
85
86impl serde::de::Error for Error {
87    fn custom<T>(_msg: T) -> Self
88    where
89        T: Display,
90    {
91        Error::SerdeDeCustom
92    }
93}
94
95impl serde::ser::StdError for Error {}