dyn_error/err_box/
errors.rs

1use std::{error::Error, fmt::Display};
2
3/// Unsuccessful outcome of [check_err_box](crate::check_err_box).
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub enum ErrBoxCheckFailure {
6    /// If the tested [Result] is [Ok] instead of [Err].
7    ResultIsNotErr,
8
9    /// If the wrapped [Error] cannot be downcast
10    /// to the expected error type.
11    DowncastFailed,
12
13    /// If the wrapped [Error] is not equal (via [PartialEq])
14    /// to the expected error.
15    NotEqual { expected: String, actual: String },
16}
17
18/// ```
19/// use dyn_error::*;
20///
21/// assert_eq!(
22///     ErrBoxCheckFailure::ResultIsNotErr.to_string(),
23///     "The result is not Err."
24/// );
25///
26/// assert_eq!(
27///     ErrBoxCheckFailure::DowncastFailed.to_string(),
28///     "The boxed error cannot be downcast to the expected type."
29/// );
30///
31/// assert_eq!(
32///     ErrBoxCheckFailure::NotEqual {
33///         expected: "EXP".to_string(),
34///         actual: "ACT".to_string()
35///     }.to_string(),
36///     "Expected error: 'EXP', actual error: 'ACT'."
37/// );
38/// ```
39impl Display for ErrBoxCheckFailure {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            ErrBoxCheckFailure::ResultIsNotErr => write!(f, "The result is not Err."),
43
44            ErrBoxCheckFailure::DowncastFailed => {
45                write!(
46                    f,
47                    "The boxed error cannot be downcast to the expected type."
48                )
49            }
50
51            ErrBoxCheckFailure::NotEqual { expected, actual } => {
52                write!(
53                    f,
54                    "Expected error: '{}', actual error: '{}'.",
55                    expected, actual
56                )
57            }
58        }
59    }
60}
61
62impl Error for ErrBoxCheckFailure {}