Skip to main content

rs_eda/error/
mod.rs

1use std::{
2    error::Error,
3    fmt::{self, Display},
4    future::Future,
5};
6
7pub type MyError<T> = Result<T, BError>;
8
9#[derive(Debug)]
10pub struct BError {
11    msg: String,
12}
13impl BError {
14    pub fn with_msg(msg: &str) -> Self {
15        Self {
16            msg: msg.to_string(),
17        }
18    }
19    pub fn get_msg(&self) -> &str {
20        &self.msg
21    }
22}
23impl Error for BError {}
24impl Display for BError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "{}", self.msg)
27    }
28}
29
30// 执行MyError相关的方法与函数
31pub async fn my_err_fn<F, FUT, T>(f: F) -> MyError<T>
32where
33    F: FnOnce() -> FUT,
34    FUT: Future<Output = MyError<T>>,
35{
36    let res = f().await?;
37    Ok(res)
38}
39
40#[macro_export]
41macro_rules! my_error {
42    ($res:expr) => {
43        match $res {
44            core::result::Result::Ok(x) => x,
45            core::result::Result::Err(e) => {
46                let err_msg = &e.to_string();
47                return core::result::Result::Err(rs_units::error::BError::with_msg(err_msg));
48            }
49        }
50    };
51}