messagepack_serde/de/
error.rs

1use serde::de;
2
3pub type CoreError = messagepack_core::decode::Error;
4
5#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
6pub enum Error {
7    Decode(CoreError),
8    AnyIsUnsupported,
9    #[cfg(not(feature = "std"))]
10    Custom,
11    #[cfg(feature = "std")]
12    Message(String),
13}
14
15impl core::fmt::Display for Error {
16    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17        match self {
18            Error::Decode(e) => e.fmt(f),
19            Error::AnyIsUnsupported => write!(f, "Any is unsupported"),
20            #[cfg(not(feature = "std"))]
21            Error::Custom => write!(f, "Cannot deserialize format"),
22            #[cfg(feature = "std")]
23            Error::Message(msg) => f.write_str(msg),
24        }
25    }
26}
27
28impl From<CoreError> for Error {
29    fn from(err: CoreError) -> Self {
30        Error::Decode(err)
31    }
32}
33
34impl de::StdError for Error {}
35impl de::Error for Error {
36    #[allow(unused_variables)]
37    fn custom<T>(msg: T) -> Self
38    where
39        T: core::fmt::Display,
40    {
41        #[cfg(not(feature = "std"))]
42        {
43            Self::Custom
44        }
45
46        #[cfg(feature = "std")]
47        {
48            Self::Message(msg.to_string())
49        }
50    }
51}