#error #anyhow #match #downcast #macro

match_err

匹配枚举错误类型的宏

8个版本

0.1.9 2024年8月6日
0.1.8 2024年8月5日

#191 in 测试

Download history 309/week @ 2024-07-29 267/week @ 2024-08-05

每月下载量 576

MIT许可协议

9KB
50

match_err

用于快速匹配枚举错误类型的宏

帮助避免编写长而繁琐的结构,例如

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

lib.rs:

match_err

用于快速匹配和断言枚举错误类型的宏

帮助避免编写长而繁琐的结构,例如

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

示例

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

无运行时依赖