1use ::core::fmt::Display;
4
5use crate::format::Type;
6
7#[derive(Debug)]
9pub enum Error {
10 UnexpectedEnd,
12 ExcessData,
14 BufferTooSmall,
16 Allocation,
18 UsizeOverflow,
20 LimitReached,
22
23 InvalidType(u8),
25 VarIntTooLarge,
27 WrongType(Type, &'static [Type]),
29 NotOneChar,
31
32 Format(::core::fmt::Error),
35 StringNotUtf8(::core::str::Utf8Error),
37 #[cfg(feature = "std")]
39 Io(::std::io::Error),
40
41 Custom,
44 #[cfg(feature = "alloc")]
47 Message(::alloc::string::String),
48}
49
50impl Display for Error {
51 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
52 match self {
53 Error::UnexpectedEnd => {
54 write!(f, "Expected more data but encountered the end of the input")
55 }
56 Error::ExcessData => write!(f, "Excess data appeared at the end of the input"),
57 Error::BufferTooSmall => write!(f, "Output or scratch buffer was too small"),
58 Error::Allocation => write!(f, "Allocator failed on allocating more space"),
59 Error::UsizeOverflow => write!(f, "Tried using more bytes than usize allows for"),
60 Error::LimitReached => write!(f, "Configured size limit reached"),
61
62 Error::InvalidType(v) => {
63 write!(f, "Invalid data type designator encountered: {v:#02X}")
64 }
65 Error::VarIntTooLarge => write!(f, "VarInt too large for the given expected type"),
66 Error::WrongType(found, expected) => write!(
67 f,
68 "Wrong data type encountered. Found `{found:?}`, but expected one of `{expected:?}`"
69 ),
70 Error::NotOneChar => write!(f, "String is not exactly one character"),
71
72 Error::Format(err) => write!(f, "Value formatting error: {err:#}"),
73 Error::StringNotUtf8(err) => write!(f, "String is not valid UTF-8: {err:#}"),
74 #[cfg(feature = "std")]
75 Error::Io(err) => write!(f, "IO error: {err:#}"),
76
77 Error::Custom => write!(f, "Unknown custom error"),
78 #[cfg(feature = "alloc")]
79 Error::Message(msg) => write!(f, "Custom error: {msg}"),
80 }
81 }
82}
83
84impl ::core::error::Error for Error {
85 fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
86 match self {
87 Error::Format(err) => Some(err),
88 Error::StringNotUtf8(err) => Some(err),
89 #[cfg(feature = "std")]
90 Error::Io(err) => Some(err),
91 _ => None,
92 }
93 }
94}
95
96impl From<::core::fmt::Error> for Error {
97 #[inline]
98 fn from(err: ::core::fmt::Error) -> Self {
99 Self::Format(err)
100 }
101}
102
103impl From<::core::str::Utf8Error> for Error {
104 #[inline]
105 fn from(err: ::core::str::Utf8Error) -> Self {
106 Self::StringNotUtf8(err)
107 }
108}
109
110#[cfg(feature = "std")]
111impl From<::std::io::Error> for Error {
112 #[inline]
113 fn from(err: ::std::io::Error) -> Self {
114 Self::Io(err)
115 }
116}
117
118impl ::serde::ser::Error for Error {
119 #[cfg(not(feature = "alloc"))]
120 #[inline]
121 fn custom<T>(_msg: T) -> Self
122 where
123 T: Display,
124 {
125 Self::Custom
126 }
127
128 #[cfg(feature = "alloc")]
129 #[inline]
130 fn custom<T>(msg: T) -> Self
131 where
132 T: Display,
133 {
134 Self::Message(::alloc::format!("{msg}"))
135 }
136}
137
138impl ::serde::de::Error for Error {
139 #[cfg(not(feature = "alloc"))]
140 #[inline]
141 fn custom<T>(_msg: T) -> Self
142 where
143 T: Display,
144 {
145 Self::Custom
146 }
147
148 #[cfg(feature = "alloc")]
149 #[inline]
150 fn custom<T>(msg: T) -> Self
151 where
152 T: Display,
153 {
154 Self::Message(::alloc::format!("{msg}"))
155 }
156}