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
126
127
128
129
130
131
132
133
134
135
136
137
use core::fmt;

#[cfg(feature = "alloc")]
use alloc::string::ToString;

/// Encoding error.
#[derive(Debug)]
pub struct Error<E> {
    err: ErrorImpl<E>,
    #[cfg(not(feature = "alloc"))]
    msg: &'static str,
    #[cfg(feature = "alloc")]
    msg: alloc::string::String
}

impl<E> Error<E> {
    /// Construct an error with a generic message.
    ///
    /// With feature `"alloc"` any type `T: Display` is accepted which allows
    /// formatted strings. Otherwise only a `&'static str` can be used as a
    /// message.
    #[cfg(not(feature = "alloc"))]
    pub fn message(msg: &'static str) -> Self {
        Error { err: ErrorImpl::Message, msg }
    }

    /// Construct an error with a generic message.
    ///
    /// With feature `"alloc"` any type `T: Display` is accepted which allows
    /// formatted strings. Otherwise only a `&'static str` can be used as a
    /// message.
    #[cfg(feature = "alloc")]
    pub fn message<T: fmt::Display>(msg: T) -> Self {
        Error { err: ErrorImpl::Message, msg: msg.to_string() }
    }

    /// A write error happened.
    pub fn write(e: E) -> Self {
        Error { err: ErrorImpl::Write(e), msg: Default::default() }
    }

    /// A custom error.
    ///
    /// *Requires feature* `"std"`.
    #[cfg(feature = "std")]
    pub fn custom<T: std::error::Error + Send + Sync + 'static>(err: T) -> Self {
        Error { err: ErrorImpl::Custom(Box::new(err)), msg: Default::default() }
    }

    /// Add a message to this error value.
    ///
    /// With feature `"alloc"` any type `T: Display` is accepted which allows
    /// formatted strings. Otherwise only a `&'static str` can be used as a
    /// message.
    #[cfg(not(feature = "alloc"))]
    pub fn with_message(mut self, msg: &'static str) -> Self {
        self.msg = msg;
        self
    }

    /// Add a message to this error value.
    ///
    /// With feature `"alloc"` any type `T: Display` is accepted which allows
    /// formatted strings. Otherwise only a `&'static str` can be used as a
    /// message.
    #[cfg(feature = "alloc")]
    pub fn with_message<T: fmt::Display>(mut self, msg: T) -> Self {
        self.msg = msg.to_string();
        self
    }

    pub fn is_message(&self) -> bool {
        matches!(self.err, ErrorImpl::Message)
    }

    pub fn is_write(&self) -> bool {
        matches!(self.err, ErrorImpl::Write(_))
    }

    #[cfg(feature = "std")]
    pub fn is_custom(&self) -> bool {
        matches!(self.err, ErrorImpl::Custom(_))
    }
}

/// Internal error representation.
#[derive(Debug)]
enum ErrorImpl<E> {
    /// Error writing bytes to a `Write` impl.
    Write(E),
    /// Generic error message.
    Message,
    /// Custom error.
    #[cfg(feature = "std")]
    Custom(Box<dyn std::error::Error + Send + Sync>)
}

impl<E: fmt::Display> fmt::Display for Error<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.err {
            ErrorImpl::Message => write!(f, "{}", self.msg),
            #[cfg(not(feature = "std"))]
            ErrorImpl::Write(e) =>
                if self.msg.is_empty() {
                    write!(f, "write error: {e}")
                } else {
                    write!(f, "write error: {e}, {}", self.msg)
                }
            #[cfg(feature = "std")]
            ErrorImpl::Write(_) =>
                if self.msg.is_empty() {
                    write!(f, "write error")
                } else {
                    write!(f, "write error: {}", self.msg)
                }
            #[cfg(feature = "std")]
            ErrorImpl::Custom(_) =>
                if self.msg.is_empty() {
                    write!(f, "encode error")
                } else {
                    write!(f, "encode error: {}", self.msg)
                }
        }
    }
}

#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for Error<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.err {
            ErrorImpl::Message   => None,
            ErrorImpl::Write(e)  => Some(e),
            ErrorImpl::Custom(e) => Some(&**e)
        }
    }
}