Crate match_err

Source
Expand description

§match_err

Macro for quick matching and asserting errors against enum-like error types

Helps to avoid writing long and tedious structures like:

if let Err(e) = err {
    if let Some(e) = e.downcast_ref::<Error>() {
        match e {
            ...
        }
    }
}

§Examples

use match_err::*;
use anyhow::anyhow;

#[derive(thiserror::Error, Debug)]
enum Error {
    #[error("not found")]
    NotFound,
    #[error("custom: {0}")]
    Custom(String),
}

let err: Result<(), _> = Err(anyhow!(Error::NotFound));

match_if_err!(err, Error, {
    NotFound => println!("not found"),
    Custom(msg) => println!("custom message: {}", msg),
    _ => println!("unknown")
})

Macros§

assert_error
Asserts the error against an enum-like error type by hiding the usage of downcast_ref method The error is required to implement PartialEq
assert_if_error
Asserts the variable is an error and then asserts it against an enum-like error type by hiding the usage of downcast_ref method The error is required to implement PartialEq
match_err
Matches the error against an enum-like error type by hiding the usage of downcast_ref method
match_if_err
Checks if it’s an error and matches the error against an enum-like error type by hiding the usage of downcast_ref method