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
use crateErrBoxCheckFailure;
use Error;
/// Tests whether the given [Result] is actually
/// an [Err] with a [Box] containing an [Error] implementation
/// equal to the *expected* instance:
///
/// * If the equality check succeeds, just returns [Ok] with
/// a no-op `()` value;
///
/// * otherwise, returns [Err] - with a descriptive [ErrBoxCheckFailure].
///
/// ```
/// use dyn_error::*;
/// use std::fmt::Display;
/// use std::error::Error;
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct MyErr(pub u8);
///
/// impl Display for MyErr {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "Custom error: {}", self.0)
/// }
/// }
///
/// impl Error for MyErr {};
///
/// let result: Result<String, Box<dyn Error>> = Err(Box::new(
/// MyErr(90)
/// ));
///
/// assert_eq!(
/// check_err_box(
/// result,
/// MyErr(90)
/// ),
/// Ok(())
/// );
/// ```
///
/// In case of inequality, the function returns [Err]
/// with [ErrBoxCheckFailure::NotEqual], containing the
/// string representations of the two instances:
///
/// ```
/// use dyn_error::*;
/// use std::fmt::Display;
/// use std::error::Error;
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct MyErr(pub u8);
///
/// impl Display for MyErr {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "Custom error: {}", self.0)
/// }
/// }
///
/// impl Error for MyErr {};
///
/// let result: Result<String, Box<dyn Error>> = Err(Box::new(
/// MyErr(90)
/// ));
///
/// assert_eq!(
/// check_err_box(result, MyErr(7)),
/// Err(ErrBoxCheckFailure::NotEqual {
/// expected: "Custom error: 7".to_string(),
/// actual: "Custom error: 90".to_string()
/// })
/// );
/// ```
///
/// Of course, the check also fails if the boxed error and the expected error belong to unrelated types:
///
/// ```
/// use dyn_error::*;
/// use std::fmt::Display;
/// use std::error::Error;
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct AlphaErr(pub u8);
/// impl Display for AlphaErr {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "Alpha error: {}", self.0)
/// }
/// }
/// impl Error for AlphaErr {};
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct BetaErr(pub u8);
/// impl Display for BetaErr {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "Beta error: {}", self.0)
/// }
/// }
/// impl Error for BetaErr {};
///
/// let result: Result<String, Box<dyn Error>> = Err(Box::new(
/// BetaErr(90)
/// ));
///
/// assert_eq!(
/// check_err_box(result, AlphaErr(90)),
/// Err(ErrBoxCheckFailure::DowncastFailed)
/// );
/// ```
///
/// Finally, the function fails also if the result is just [Ok]:
///
/// ```
/// use dyn_error::*;
/// use std::fmt::Display;
/// use std::error::Error;
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct MyErr(pub u8);
///
/// impl Display for MyErr {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "Custom error: {}", self.0)
/// }
/// }
///
/// impl Error for MyErr {};
///
/// let result: Result<String, Box<dyn Error>> = Ok("Dodo".to_string());
///
/// check_err_box(result, MyErr(7));
/// ```