-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
94 lines (79 loc) · 2.19 KB
/
main.rs
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
pub mod test {
use flex_error::*;
use thiserror::Error;
#[derive(Debug, Error, Eq, PartialEq, Clone)]
#[error("external")]
pub struct ExternalError;
define_error! {
/// This is documentation for foo error
#[derive(Debug, Clone)]
#[derive(Eq, PartialEq)]
FooError {
/// This is documentation for bar error
Bar
{ code: u32 }
[ DisplayError<ExternalError> ]
| e | { format_args!("Bar error with code {}", e.code) },
/// This is documentation for baz error
#[derive(Ord, PartialOrd)]
Baz
{ extra: String }
| e | { format_args!("General Baz error with extra detail: {}", e.extra) },
}
}
}
pub mod foo {
use flex_error::*;
use thiserror::Error;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct PrimitiveError;
#[derive(Debug, Clone, Error, PartialEq)]
pub enum SystemError {
#[error("error1")]
Error1,
#[error("error2")]
Error2,
}
define_error! {
#[derive(Debug, Clone, PartialEq, Eq)]
FooError {
Foo
{ foo_val: String }
[ DetailOnly<PrimitiveError> ]
| err | { format_args!("foo error: {}", err.foo_val) },
System
[ TraceError<SystemError> ]
| _ | { "system error" },
Unknown
| _ | { "unknown error" },
Nested
[ Self ]
| _ | { format_args!("nested foo error") }
}
}
}
pub mod bar {
use super::foo;
use flex_error::*;
define_error! {
#[derive(Debug, Clone, PartialEq, Eq)]
BarError {
Bar
{ bar: String }
| err | { format_args!("bar error {}", err.bar) },
Foo
{ detail: String }
[ foo::FooError ]
| err | { format_args!("error caused by foo: {}", err.detail) },
}
}
}
fn main() -> Result<(), bar::BarError> {
color_eyre::install().unwrap();
let err1 = foo::FooError::system(foo::SystemError::Error1);
let err2 = foo::FooError::nested(err1);
let err3 = bar::BarError::foo("Foo has failed".into(), err2);
println!("error: {:?}", err3);
// Err(err3)
Ok(())
}