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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[allow(dead_code)]
pub struct Error {
code: ErrCode,
reason: String,
description: String,
source: Option<ErrSource>,
}
impl Error {
fn new<U>(code: ErrCode, reason: U, source: Option<ErrSource>) -> Self
where
U: Into<String>,
{
let reason = reason.into();
let code_str: &str = code.into();
let description = format!("{code_str}: {reason}");
Self {
code,
reason,
description,
source,
}
}
pub(crate) fn extract_left() -> Self {
Self::new(ErrCode::Left, "Unable to extract Left value", None)
}
pub(crate) fn extract_right() -> Self {
Self::new(ErrCode::Right, "Unable to extract Right value", None)
}
pub(crate) fn invalid() -> Self {
Self::new(ErrCode::Invalid, "Invalid Either", None)
}
}
impl std::error::Error for Error {
#[must_use]
fn description(&self) -> &str {
&self.description
}
#[must_use]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let Some(ref x) = self.source {
Some(x)
} else {
None
}
}
}
impl fmt::Display for Error {
#[cfg(all(nightly_lints, feature = "unstable"))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use std::error::Error;
let res = <dyn Error>::sources(self).fold(format!("{self}"), |mut s, e| {
s.push_str(&format!(" => {}", e));
s
});
write!(f, "{}", res)
}
#[cfg(any(stable_lints, beta_lints))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description)
}
#[cfg(all(nightly_lints, not(feature = "unstable")))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description)
}
}
impl From<&str> for Error {
#[must_use]
fn from(text: &str) -> Self {
let split = text.split(':');
let vec = split.collect::<Vec<&str>>();
let code = vec.first().unwrap_or(&"");
let reason = vec.get(1).unwrap_or(&"");
Self::new((*code).into(), *reason, None)
}
}
#[derive(Copy, Clone, Debug)]
enum ErrCode {
Left,
Right,
Invalid,
Unknown,
}
impl From<ErrCode> for &str {
fn from(value: ErrCode) -> &'static str {
match value {
ErrCode::Left => "left",
ErrCode::Right => "right",
ErrCode::Invalid => "invalid",
ErrCode::Unknown => "unknown",
}
}
}
impl From<ErrCode> for String {
fn from(value: ErrCode) -> String {
match value {
ErrCode::Left => "left",
ErrCode::Right => "right",
ErrCode::Invalid => "invalid",
ErrCode::Unknown => "unknown",
}
.to_string()
}
}
impl From<&str> for ErrCode {
#[must_use]
fn from(text: &str) -> Self {
match text {
"left" => Self::Left,
"right" => Self::Right,
"invalid" => Self::Invalid,
_ => Self::Unknown,
}
}
}
macro_rules! dep_error {
($error:ty, $kind:expr, $code:expr, $reason:expr) => {
impl From<$error> for Error {
#[must_use]
fn from(inner: $error) -> Self {
Self::new($code, $reason, Some($kind(inner)))
}
}
};
}
dep_error!(
std::io::Error,
ErrSource::Io,
ErrCode::Unknown,
"There was an I/O error"
);
#[cfg(all(test, feature = "serde"))]
dep_error!(
serde_json::Error,
ErrSource::SerdeJson,
ErrCode::Unknown,
"There was an error converting JSON"
);
#[cfg(all(test, feature = "serde"))]
dep_error!(
toml::de::Error,
ErrSource::TomlDe,
ErrCode::Unknown,
"There was an error deserializing TOML"
);
#[cfg(all(test, feature = "serde"))]
dep_error!(
toml::ser::Error,
ErrSource::TomlSer,
ErrCode::Unknown,
"There was an error serializing TOML"
);
#[derive(Debug)]
#[allow(clippy::large_enum_variant, variant_size_differences)]
enum ErrSource {
Io(std::io::Error),
#[cfg(all(test, feature = "serde"))]
SerdeJson(serde_json::Error),
#[cfg(all(test, feature = "serde"))]
TomlDe(toml::de::Error),
#[cfg(all(test, feature = "serde"))]
TomlSer(toml::ser::Error),
}
impl std::error::Error for ErrSource {}
impl fmt::Display for ErrSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(source) => write!(f, "{source}"),
#[cfg(all(test, feature = "serde"))]
Self::SerdeJson(source) => write!(f, "{source}"),
#[cfg(all(test, feature = "serde"))]
Self::TomlDe(source) => write!(f, "{source}"),
#[cfg(all(test, feature = "serde"))]
Self::TomlSer(source) => write!(f, "{source}"),
}
}
}