Macro match_err::match_if_err

source ·
macro_rules! match_if_err {
    ( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:ident),* ) )? => $arm:expr ),*, _ => $default:expr } ) => { ... };
    ( $any:expr, $ty:ident, { $( $variant:ident $( ( $($inner:ident),* ) )? => $arm:expr ),* $(,)? }) => { ... };
}
Expand description

Checks if it’s an error and matches the error against an enum-like error type by hiding the usage of downcast_ref method

§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 => assert!(true),
    Custom(msg) => assert!(false),
    _ => assert!(false)
 })