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
|
// The following code is from https://github.com/withoutboats/display_derive/blob/232a32ee19e262aacbd2c93be5b4ce9e89a5fc30/tests/tests.rs
// Written by without boats originally
#[macro_use]
extern crate derive_more;
#[derive(Display)]
#[display(fmt = "An error has occurred.")]
struct UnitError;
#[test]
fn unit_struct() {
let s = format!("{}", UnitError);
assert_eq!(&s[..], "An error has occurred.");
}
#[derive(Display)]
#[display(fmt = "Error code: {}", code)]
struct RecordError {
code: u32,
}
#[test]
fn record_struct() {
let s = format!("{}", RecordError { code: 0 });
assert_eq!(&s[..], "Error code: 0");
}
#[derive(Display)]
#[display(fmt = "Error code: {}", _0)]
struct TupleError(i32);
#[test]
fn tuple_struct() {
let s = format!("{}", TupleError(2));
assert_eq!(&s[..], "Error code: 2");
}
#[derive(Display)]
enum EnumError {
#[display(fmt = "Error code: {}", code)]
StructVariant { code: i32 },
#[display(fmt = "Error: {}", _0)]
TupleVariant(&'static str),
#[display(fmt = "An error has occurred.")]
UnitVariant,
}
#[test]
fn enum_error() {
let s = format!("{}", EnumError::StructVariant { code: 2 });
assert_eq!(&s[..], "Error code: 2");
let s = format!("{}", EnumError::TupleVariant("foobar"));
assert_eq!(&s[..], "Error: foobar");
let s = format!("{}", EnumError::UnitVariant);
assert_eq!(&s[..], "An error has occurred.");
}
|