dyn_error/err_box/
macro_impl.rs

1/// Macro that internally calls [check_err_box](crate::check_err_box),
2/// issuing [panic] in case of failure.
3///
4/// If the actual error is equal to the expected error, the macro has no effects:
5///
6/// ```
7/// use dyn_error::*;
8/// use std::fmt::Display;
9/// use std::error::Error;
10///
11/// #[derive(Debug, PartialEq, Eq)]
12/// struct MyErr(pub u8);
13///
14/// impl Display for MyErr {
15///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16///         write!(f, "Custom error: {}", self.0)
17///     }
18/// }
19///
20/// impl Error for MyErr {};
21///
22/// let result: Result<String, Box<dyn Error>> = Err(Box::new(
23///     MyErr(90)
24/// ));
25///
26/// assert_err_box!(
27///     result,
28///     MyErr(90)
29/// );
30/// ```
31///
32/// In case of inequality, the macro initiates a [panic]:
33///
34/// ```should_panic
35/// use dyn_error::*;
36/// use std::fmt::Display;
37/// use std::error::Error;
38///
39/// #[derive(Debug, PartialEq, Eq)]
40/// struct MyErr(pub u8);
41///
42/// impl Display for MyErr {
43///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44///         write!(f, "Custom error: {}", self.0)
45///     }
46/// }
47///
48/// impl Error for MyErr {};
49///
50/// let result: Result<String, Box<dyn Error>> = Err(Box::new(
51///     MyErr(90)
52/// ));
53///
54/// assert_err_box!(result, MyErr(7));
55/// ```
56///
57/// Of course, the macro panics if the boxed error and the expected error belong to incompatible types:
58///
59/// ```should_panic
60/// use dyn_error::*;
61/// use std::fmt::Display;
62/// use std::error::Error;
63///
64/// #[derive(Debug, PartialEq, Eq)]
65/// struct AlphaErr(pub u8);
66/// impl Display for AlphaErr {
67///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68///         write!(f, "Alpha error: {}", self.0)
69///     }
70/// }
71/// impl Error for AlphaErr {};
72///
73/// #[derive(Debug, PartialEq, Eq)]
74/// struct BetaErr(pub u8);
75/// impl Display for BetaErr {
76///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77///         write!(f, "Beta error: {}", self.0)
78///     }
79/// }
80/// impl Error for BetaErr {};
81///
82/// let result: Result<String, Box<dyn Error>> = Err(Box::new(
83///     BetaErr(90)
84/// ));
85///
86/// assert_err_box!(result, AlphaErr(90));
87/// ```
88///
89/// Finally, the macro panics also if the result is just [Ok]:
90///
91/// ```should_panic
92/// use dyn_error::*;
93/// use std::fmt::Display;
94/// use std::error::Error;
95///
96/// #[derive(Debug, PartialEq, Eq)]
97/// struct MyErr(pub u8);
98///
99/// impl Display for MyErr {
100///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101///         write!(f, "Custom error: {}", self.0)
102///     }
103/// }
104///
105/// impl Error for MyErr {};
106///
107/// let result: Result<String, Box<dyn Error>> = Ok("Dodo".to_string());
108///
109/// assert_err_box!(result, MyErr(7));
110/// ```
111#[macro_export]
112macro_rules! assert_err_box {
113    ($result: expr, $expected_err: expr) => {{
114        let result = check_err_box($result, $expected_err);
115
116        if let Err(error) = result {
117            panic!("{}() failed: {}", stringify!(check_err_box), error);
118        }
119    }};
120}