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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Trait governing what error types associated with the encoding framework must
//! support.
//!
//! The most important component in here is `Error::custom` which allows custom
//! encoding implementations to raise custom errors based on types that
//! implement [Display][core::fmt::Display].

use core::fmt;

/// Trait governing errors raised during encodeing or decoding.
pub trait Error: Sized {
    /// Construct a custom error.
    fn custom<T>(message: T) -> Self
    where
        T: 'static + Send + Sync + fmt::Display + fmt::Debug;

    /// Collect an error from something that can be displayed.
    ///
    /// This is made available to format custom error messages in `no_std`
    /// environments. The error message is to be collected by formatting `T`.
    fn collect_from_display<T>(message: T) -> Self
    where
        T: fmt::Display;

    /// Trying to decode an uninhabitable type.
    #[inline]
    fn uninhabitable(type_name: &'static str) -> Self {
        Self::collect_from_display(Uninhabitable { type_name })
    }

    /// Indicate that a variant wasn't supported by tag.
    #[inline]
    fn unsupported_variant<T>(type_name: &'static str, tag: T) -> Self
    where
        T: fmt::Debug,
    {
        Self::collect_from_display(UnsupportedVariant { type_name, tag })
    }

    /// Missing a field of the given tag.
    #[inline]
    fn missing_field<T>(type_name: &'static str, tag: T) -> Self
    where
        T: fmt::Debug,
    {
        Self::collect_from_display(MissingField { type_name, tag })
    }

    /// Encountered an unsupported number tag.
    #[inline]
    fn unsupported_tag<T>(type_name: &'static str, tag: T) -> Self
    where
        T: fmt::Debug,
    {
        Self::collect_from_display(UnsupportedTag { type_name, tag })
    }

    /// Invalid value.
    #[inline]
    fn invalid_value(type_name: &'static str) -> Self {
        Self::collect_from_display(InvalidValue { type_name })
    }

    /// Found an unexpected field.
    #[inline]
    fn unexpected_field(type_name: &'static str) -> Self {
        Self::collect_from_display(UnexpectedField { type_name })
    }
}

#[cfg(feature = "std")]
impl Error for std::io::Error {
    fn custom<T>(message: T) -> Self
    where
        T: 'static + Send + Sync + fmt::Display + fmt::Debug,
    {
        std::io::Error::new(std::io::ErrorKind::Other, message.to_string())
    }

    fn collect_from_display<T>(message: T) -> Self
    where
        T: fmt::Display,
    {
        std::io::Error::new(std::io::ErrorKind::Other, message.to_string())
    }
}

struct Uninhabitable {
    type_name: &'static str,
}

impl fmt::Display for Uninhabitable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: cannot decode uninhabitable types", self.type_name)
    }
}

struct UnsupportedVariant<T> {
    type_name: &'static str,
    tag: T,
}

impl<T> fmt::Display for UnsupportedVariant<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: unsupported variant {:?}", self.type_name, self.tag)
    }
}

struct MissingField<T> {
    type_name: &'static str,
    tag: T,
}

impl<T> fmt::Display for MissingField<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: missing field {:?}", self.type_name, self.tag)
    }
}

struct UnsupportedTag<T> {
    type_name: &'static str,
    tag: T,
}

impl<T> fmt::Display for UnsupportedTag<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: unsupported tag {:?}", self.type_name, self.tag)
    }
}

struct InvalidValue {
    type_name: &'static str,
}

impl fmt::Display for InvalidValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: trying to construct from invalid value",
            self.type_name
        )
    }
}

struct UnexpectedField {
    type_name: &'static str,
}

impl fmt::Display for UnexpectedField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: got a field but expected none", self.type_name)
    }
}