messagepack_serde/ser/
error.rs

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