1use alloc::string::{String, ToString};
16use core::fmt::{Display, Formatter};
17
18#[derive(Clone, Debug, Eq, PartialEq)]
20pub enum Error {
21 Custom(String),
23 DeserializeBadBool,
25 DeserializeBadChar,
27 DeserializeBadOption,
29 DeserializeBadUtf8,
31 DeserializeUnexpectedEnd,
33 NotSupported,
35 SerializeBufferFull,
37}
38
39pub type Result<T> = core::result::Result<T, Error>;
41
42impl Display for Error {
43 fn fmt(&self, formatter: &mut Formatter) -> core::fmt::Result {
44 formatter.write_str(match self {
45 Self::Custom(msg) => msg,
46 Self::DeserializeBadBool => "Found a bool that wasn't 0 or 1",
47 Self::DeserializeBadChar => "Found an invalid unicode char",
48 Self::DeserializeBadOption => "Found an Option discriminant that wasn't 0 or 1",
49 Self::DeserializeBadUtf8 => "Tried to parse invalid utf-8",
50 Self::DeserializeUnexpectedEnd => "Unexpected end during deserialization",
51 Self::NotSupported => "Not supported",
52 Self::SerializeBufferFull => "The serialize buffer is full",
53 })
54 }
55}
56
57impl serde::ser::Error for Error {
58 fn custom<T: Display>(msg: T) -> Self {
59 Error::Custom(msg.to_string())
60 }
61}
62
63impl serde::de::Error for Error {
64 fn custom<T: Display>(msg: T) -> Self {
65 Error::Custom(msg.to_string())
66 }
67}
68
69impl serde::ser::StdError for Error {}