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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#![allow(clippy::needless_raw_string_hashes, clippy::uninlined_format_args)]
use std::fmt::{self, Display};
use thiserror::Error;
fn assert<T: Display>(expected: &str, value: T) {
assert_eq!(expected, value.to_string());
}
#[test]
fn test_braced() {
#[derive(Error, Debug)]
#[error("braced error: {msg}")]
struct Error {
msg: String,
}
let msg = "T".to_owned();
assert("braced error: T", Error { msg });
}
#[test]
fn test_braced_unused() {
#[derive(Error, Debug)]
#[error("braced error")]
struct Error {
extra: usize,
}
assert("braced error", Error { extra: 0 });
}
#[test]
fn test_tuple() {
#[derive(Error, Debug)]
#[error("tuple error: {0}")]
struct Error(usize);
assert("tuple error: 0", Error(0));
}
#[test]
fn test_unit() {
#[derive(Error, Debug)]
#[error("unit error")]
struct Error;
assert("unit error", Error);
}
#[test]
fn test_enum() {
#[derive(Error, Debug)]
enum Error {
#[error("braced error: {id}")]
Braced { id: usize },
#[error("tuple error: {0}")]
Tuple(usize),
#[error("unit error")]
Unit,
}
assert("braced error: 0", Error::Braced { id: 0 });
assert("tuple error: 0", Error::Tuple(0));
assert("unit error", Error::Unit);
}
#[test]
fn test_constants() {
#[derive(Error, Debug)]
#[error("{MSG}: {id:?} (code {CODE:?})")]
struct Error {
id: &'static str,
}
const MSG: &str = "failed to do";
const CODE: usize = 9;
assert("failed to do: \"\" (code 9)", Error { id: "" });
}
#[test]
fn test_inherit() {
#[derive(Error, Debug)]
#[error("{0}")]
enum Error {
Some(&'static str),
#[error("other error")]
Other(&'static str),
}
assert("some error", Error::Some("some error"));
assert("other error", Error::Other("..."));
}
#[test]
fn test_brace_escape() {
#[derive(Error, Debug)]
#[error("fn main() {{}}")]
struct Error;
assert("fn main() {}", Error);
}
#[test]
fn test_expr() {
#[derive(Error, Debug)]
#[error("1 + 1 = {}", 1 + 1)]
struct Error;
assert("1 + 1 = 2", Error);
}
#[test]
fn test_nested() {
#[derive(Error, Debug)]
#[error("!bool = {}", not(.0))]
struct Error(bool);
#[allow(clippy::trivially_copy_pass_by_ref)]
fn not(bool: &bool) -> bool {
!*bool
}
assert("!bool = false", Error(true));
}
#[test]
fn test_match() {
#[derive(Error, Debug)]
#[error("{}: {0}", match .1 {
Some(n) => format!("error occurred with {}", n),
None => "there was an empty error".to_owned(),
})]
struct Error(String, Option<usize>);
assert(
"error occurred with 1: ...",
Error("...".to_owned(), Some(1)),
);
assert(
"there was an empty error: ...",
Error("...".to_owned(), None),
);
}
#[test]
fn test_nested_display() {
// Same behavior as the one in `test_match`, but without String allocations.
#[derive(Error, Debug)]
#[error("{}", {
struct Msg<'a>(&'a String, &'a Option<usize>);
impl<'a> Display for Msg<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.1 {
Some(n) => write!(formatter, "error occurred with {}", n),
None => write!(formatter, "there was an empty error"),
}?;
write!(formatter, ": {}", self.0)
}
}
Msg(.0, .1)
})]
struct Error(String, Option<usize>);
assert(
"error occurred with 1: ...",
Error("...".to_owned(), Some(1)),
);
assert(
"there was an empty error: ...",
Error("...".to_owned(), None),
);
}
#[test]
fn test_void() {
#[allow(clippy::empty_enum)]
#[derive(Error, Debug)]
#[error("...")]
pub enum Error {}
let _: Error;
}
#[test]
fn test_mixed() {
#[derive(Error, Debug)]
#[error("a={a} :: b={} :: c={c} :: d={d}", 1, c = 2, d = 3)]
struct Error {
a: usize,
d: usize,
}
assert("a=0 :: b=1 :: c=2 :: d=3", Error { a: 0, d: 0 });
}
#[test]
fn test_ints() {
#[derive(Error, Debug)]
enum Error {
#[error("error {0}")]
Tuple(usize, usize),
#[error("error {0}", '?')]
Struct { v: usize },
}
assert("error 9", Error::Tuple(9, 0));
assert("error ?", Error::Struct { v: 0 });
}
#[test]
fn test_trailing_comma() {
#[derive(Error, Debug)]
#[error(
"error {0}",
)]
#[rustfmt::skip]
struct Error(char);
assert("error ?", Error('?'));
}
#[test]
fn test_field() {
#[derive(Debug)]
struct Inner {
data: usize,
}
#[derive(Error, Debug)]
#[error("{}", .0.data)]
struct Error(Inner);
assert("0", Error(Inner { data: 0 }));
}
#[test]
fn test_macro_rules() {
// Regression test for https://github.com/dtolnay/thiserror/issues/86
macro_rules! decl_error {
($variant:ident($value:ident)) => {
#[derive(Debug, Error)]
pub enum Error0 {
#[error("{0:?}")]
$variant($value),
}
#[derive(Debug, Error)]
#[error("{0:?}")]
pub enum Error1 {
$variant($value),
}
};
}
decl_error!(Repro(u8));
assert("0", Error0::Repro(0));
assert("0", Error1::Repro(0));
}
#[test]
fn test_raw() {
#[derive(Error, Debug)]
#[error("braced raw error: {r#fn}")]
struct Error {
r#fn: &'static str,
}
assert("braced raw error: T", Error { r#fn: "T" });
}
#[test]
fn test_raw_enum() {
#[derive(Error, Debug)]
enum Error {
#[error("braced raw error: {r#fn}")]
Braced { r#fn: &'static str },
}
assert("braced raw error: T", Error::Braced { r#fn: "T" });
}
#[test]
fn test_raw_conflict() {
#[derive(Error, Debug)]
enum Error {
#[error("braced raw error: {r#func}, {func}", func = "U")]
Braced { r#func: &'static str },
}
assert("braced raw error: T, U", Error::Braced { r#func: "T" });
}
#[test]
fn test_keyword() {
#[derive(Error, Debug)]
#[error("error: {type}", type = 1)]
struct Error;
assert("error: 1", Error);
}
#[test]
fn test_str_special_chars() {
#[derive(Error, Debug)]
pub enum Error {
#[error("brace left {{")]
BraceLeft,
#[error("brace left 2 \x7B\x7B")]
BraceLeft2,
#[error("brace left 3 \u{7B}\u{7B}")]
BraceLeft3,
#[error("brace right }}")]
BraceRight,
#[error("brace right 2 \x7D\x7D")]
BraceRight2,
#[error("brace right 3 \u{7D}\u{7D}")]
BraceRight3,
#[error(
"new_\
line"
)]
NewLine,
#[error("escape24 \u{78}")]
Escape24,
}
assert("brace left {", Error::BraceLeft);
assert("brace left 2 {", Error::BraceLeft2);
assert("brace left 3 {", Error::BraceLeft3);
assert("brace right }", Error::BraceRight);
assert("brace right 2 }", Error::BraceRight2);
assert("brace right 3 }", Error::BraceRight3);
assert("new_line", Error::NewLine);
assert("escape24 x", Error::Escape24);
}
#[test]
fn test_raw_str() {
#[derive(Error, Debug)]
pub enum Error {
#[error(r#"raw brace left {{"#)]
BraceLeft,
#[error(r#"raw brace left 2 \x7B"#)]
BraceLeft2,
#[error(r#"raw brace right }}"#)]
BraceRight,
#[error(r#"raw brace right 2 \x7D"#)]
BraceRight2,
}
assert(r#"raw brace left {"#, Error::BraceLeft);
assert(r#"raw brace left 2 \x7B"#, Error::BraceLeft2);
assert(r#"raw brace right }"#, Error::BraceRight);
assert(r#"raw brace right 2 \x7D"#, Error::BraceRight2);
}
|