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
228
229
230
231
232
233
234
235
236
237
238
239
240
#[macro_export]
macro_rules! errormake {
($structname:ident) => {
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
struct $structname<T: ?Sized + 'static> {
source: Option<Box<T>>,
description: Option<String>,
}
errormake!(impl $structname);
};
($(#[$meta:meta])* pub $structname:ident) => {
$(#[$meta])*
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct $structname<T: ?Sized + 'static> {
source: Option<Box<T>>,
description: Option<String>,
}
errormake!(impl $structname);
};
(impl $structname:ident) => {
#[allow(dead_code)]
impl $structname<std::convert::Infallible> {
pub fn new() -> $structname<std::convert::Infallible> {
$structname {
source: None,
description: None,
}
}
pub fn with_description(description: String) -> $structname<std::convert::Infallible> {
$structname {
source: None,
description: Some(description),
}
}
}
#[allow(dead_code)]
impl<T: 'static> $structname<T> {
pub fn with_source(source: T) -> $structname<T> {
$structname {
source: Some(Box::new(source)),
description: None,
}
}
pub fn with_source_and_description(source: T, description: String) -> $structname<T> {
$structname {
source: Some(Box::new(source)),
description: Some(description),
}
}
}
#[allow(dead_code)]
impl<T: ?Sized + 'static> $structname<T> {
pub fn with_optional_data(
source: Option<Box<T>>,
description: Option<String>,
) -> $structname<T> {
$structname {
source,
description,
}
}
}
#[allow(dead_code)]
impl<T: std::error::Error + 'static> $structname<T> {
pub fn into_dynamic(self) -> $structname<dyn std::error::Error + 'static> {
$structname {
source: self.source.map(|source| source as Box<dyn std::error::Error + 'static>),
description: self.description,
}
}
}
impl<T: std::fmt::Display + ?Sized + 'static> std::fmt::Display for $structname<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.source {
Some(source) => write!(
f,
"{}\n\nThe above error caused the following error:\n\n",
source
)?,
None => {}
}
write!(
f,
concat!(stringify!($structname), ": {}"),
match self.description.as_ref() {
Some(description) => description,
None => "No description provided",
}
)?;
Ok(())
}
}
impl<T> std::error::Error for $structname<T>
where T: std::error::Error + 'static
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_ref()
.map(|err| err.as_ref() as &(dyn std::error::Error + 'static))
}
}
impl std::error::Error for $structname<dyn std::error::Error + 'static> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_ref()
.map(|err| err.as_ref())
}
}
};
}
errormake!(#[doc="An example of an error struct made by `errormake`"] pub ExampleErrorStruct);
#[cfg(test)]
mod tests {
use super::errormake;
use std::error::Error;
errormake!(TestingError);
errormake!(pub PublicTestingError);
#[test]
fn test_stable() {
let error1 = TestingError::new();
assert_eq!(
"TestingError: No description provided",
format!("{}", error1)
);
assert!(error1.source().is_none());
let error2 = TestingError::with_description(String::from("Custom error message"));
assert_eq!("TestingError: Custom error message", format!("{}", error2));
assert!(error2.source().is_none());
let error3 = TestingError::with_source(Box::new(error2));
assert_eq!("TestingError: Custom error message\n\nThe above error caused the following error:\n\nTestingError: No description provided", format!("{}", error3));
assert!(error3.source().is_some());
let error4 = TestingError::with_source_and_description(
Box::new(TestingError::with_description(String::from(
"Custom error message",
))),
String::from("Another message"),
);
assert_eq!("TestingError: Custom error message\n\nThe above error caused the following error:\n\nTestingError: Another message", format!("{}", error4));
assert!(error4.source().is_some());
}
#[test]
fn test_derives() {
let error1 = TestingError::new();
assert_eq!(error1, error1.clone());
let error2 = TestingError::with_source(error1.clone());
assert_eq!(error2, error2.clone());
assert_eq!(error2, error2);
let error3 =
TestingError::with_source_and_description(error1.clone(), String::from("description"));
assert_ne!(error3, error2);
let error4 = TestingError::with_description(String::from("description"));
assert_ne!(error1, error4);
}
#[test]
fn test_dynamic() {
let error = TestingError::new();
let error = TestingError::with_source(error).into_dynamic();
assert!(error.source().is_some());
let box_error: Box<dyn Error + 'static> = Box::new(error);
let error = TestingError::with_optional_data(Some(box_error), None);
assert!(error.source().is_some());
}
}