messagepack_serde/de/
error.rs

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