1use core::fmt::{Display, Formatter};
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
6#[cfg_attr(feature = "use-defmt", derive(defmt::Format))]
7#[non_exhaustive]
8pub enum Error {
9 WontImplement,
11 NotYetImplemented,
13 SerializeBufferFull,
15 SerializeSeqLengthUnknown,
17 DeserializeUnexpectedEnd,
19 DeserializeBadVarint,
21 DeserializeBadBool,
23 DeserializeBadChar,
25 DeserializeBadUtf8,
27 DeserializeBadOption,
29 DeserializeBadEnum,
31 DeserializeBadEncoding,
33 DeserializeBadCrc,
35 SerdeSerCustom,
37 SerdeDeCustom,
39 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
75pub 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 {}