8 releases

0.1.9 Aug 6, 2024
0.1.8 Aug 5, 2024

#199 in Testing

Download history 63/week @ 2024-07-26 495/week @ 2024-08-02 21/week @ 2024-08-09 1/week @ 2024-08-16

580 downloads per month

MIT license

9KB
50 lines

match_err

Macro for quick matching 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::*;

#[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")
})

lib.rs:

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")
})

No runtime deps