Skip to main content

messagepack_serde/de/
error.rs

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