1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use core::fmt;
use crate::core::{ enc, dec };


#[derive(Debug)]
pub enum DecodeError<E> {
    Core(dec::Error<E>),
    Custom(crate::alloc::boxed::Box<str>)
}

impl<E> From<dec::Error<E>> for DecodeError<E> {
    #[inline]
    #[cold]
    fn from(err: dec::Error<E>) -> DecodeError<E> {
        DecodeError::Core(err)
    }
}

impl<E> From<E> for DecodeError<E> {
    #[inline]
    #[cold]
    fn from(err: E) -> DecodeError<E> {
        DecodeError::Core(dec::Error::Read(err))
    }
}

#[cfg(feature = "serde1")]
#[cfg(feature = "use_std")]
impl<E: std::error::Error + 'static> serde::de::Error for DecodeError<E> {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        DecodeError::Custom(msg.to_string().into_boxed_str())
    }
}

#[cfg(feature = "serde1")]
#[cfg(not(feature = "use_std"))]
impl<E: fmt::Debug> serde::de::Error for DecodeError<E> {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        #[cfg(not(feature = "use_std"))]
        use crate::alloc::string::ToString;

        DecodeError::Custom(msg.to_string().into_boxed_str())
    }
}

impl<E: fmt::Debug> fmt::Display for DecodeError<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[cfg(feature = "serde1")]
#[cfg(not(feature = "use_std"))]
impl<E: fmt::Debug> serde::ser::StdError for DecodeError<E> {}

#[cfg(feature = "use_std")]
impl<E: std::error::Error + 'static> std::error::Error for DecodeError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            DecodeError::Core(err) => Some(err),
            _ => None
        }
    }
}

#[derive(Debug)]
pub enum EncodeError<E> {
    Core(enc::Error<E>),
    Custom(crate::alloc::boxed::Box<str>)
}

impl<E> From<enc::Error<E>> for EncodeError<E> {
    #[inline]
    #[cold]
    fn from(err: enc::Error<E>) -> EncodeError<E> {
        EncodeError::Core(err)
    }
}

impl<E> From<E> for EncodeError<E> {
    #[inline]
    #[cold]
    fn from(err: E) -> EncodeError<E> {
        EncodeError::Core(enc::Error::Write(err))
    }
}

#[cfg(feature = "serde1")]
#[cfg(feature = "use_std")]
impl<E: std::error::Error + 'static> serde::ser::Error for EncodeError<E> {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        EncodeError::Custom(msg.to_string().into_boxed_str())
    }
}

#[cfg(feature = "serde1")]
#[cfg(not(feature = "use_std"))]
impl<E: fmt::Debug> serde::ser::Error for EncodeError<E> {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        #[cfg(not(feature = "use_std"))]
        use crate::alloc::string::ToString;

        EncodeError::Custom(msg.to_string().into_boxed_str())
    }
}

impl<E: fmt::Debug> fmt::Display for EncodeError<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[cfg(feature = "serde1")]
#[cfg(not(feature = "use_std"))]
impl<E: fmt::Debug> serde::ser::StdError for EncodeError<E> {}

#[cfg(feature = "use_std")]
impl<E: std::error::Error + 'static> std::error::Error for EncodeError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            EncodeError::Core(err) => Some(err),
            _ => None
        }
    }
}