1use core::fmt::{Display, Formatter};
2
3#[derive(Clone, Debug, Eq, PartialEq)]
5#[cfg_attr(feature = "use-defmt", derive(defmt::Format))]
6#[non_exhaustive]
7pub enum Error {
8 WontImplement,
10 NotYetImplemented,
12 SerializeBufferFull,
14 SerializeSeqLengthUnknown,
16 DeserializeUnexpectedEnd,
18 DeserializeBadVarint,
20 DeserializeBadBool,
22 DeserializeBadChar,
24 DeserializeBadUtf8,
26 DeserializeBadOption,
28 DeserializeBadEnum,
30 DeserializeBadEncoding,
32 DeserializeBadCrc,
34 SerdeSerCustom,
36 SerdeDeCustom,
38 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
74pub 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 {}