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
//! Convenience macros.
/// Define a simple [`Whatever`](crate::Whatever) error type.
///
/// The generated type never carries its own message. Every report built from it needs an
/// explicit description at the call site, via `.whatever("...")`, `bail!("...")`, etc.
///
/// ```
/// reportify::new_whatever_type! {
/// /// Application-level error.
/// pub AppError
/// }
/// ```
}
};
}
/// Create a freeform report and return it as an error.
/// Create a freeform report.
/// Return a freeform report if a condition does not hold.
/// Unwrap a `Result`, returning its error directly, for diverging functions that run
/// forever until something fails: unlike `?`, which needs the enclosing function to
/// return a `Result` (or another [`FromResidual`](std::ops::FromResidual) type), this is
/// for a function whose return type is already the bare error/[`Report`](crate::Report)
/// itself, because it never produces a value, only ever an eventual failure.
///
/// ```
/// use reportify::{Report, ResultExt, new_whatever_type, return_error};
///
/// new_whatever_type! { ServerError }
///
/// /// Stands in for something like `TcpListener::accept`, failing on the third call.
/// fn accept(calls: &mut u32) -> std::io::Result<u32> {
/// *calls += 1;
/// if *calls < 3 {
/// Ok(*calls)
/// } else {
/// Err(std::io::Error::other("connection reset"))
/// }
/// }
///
/// fn run() -> Report<ServerError> {
/// let mut calls = 0;
/// loop {
/// let connection = return_error!(accept(&mut calls).whatever("accept failed"));
/// println!("handling connection {connection}");
/// }
/// }
///
/// let report = run();
/// assert!(format!("{report}").contains("accept failed"));
/// ```