Skip to main content

messagepack_serde/ser/
error.rs

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