Crates.io | match_err |
lib.rs | match_err |
version | 0.1.9 |
source | src |
created_at | 2024-08-01 20:54:10.58098 |
updated_at | 2024-08-06 06:30:40.993751 |
description | Macro for matching enum-like errors |
homepage | https://github.com/blkmlk/match_err |
repository | https://github.com/blkmlk/match_err |
max_upload_size | |
id | 1322520 |
size | 9,424 |
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 {
...
}
}
}
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")
})