postcard/
error.rs

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