1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//!
//! # Macros
//!
//! Useful macros for chained error managements.
//!

/// print infomation only
#[macro_export]
macro_rules! info {
    ($ops: expr, $fmt: expr, $($arg:tt)*) => {{
        $ops.c($crate::d!($fmt, $($arg)*)).map_err(|e| {
            if "INFO" == $crate::LOG_LEVEL.as_str() {
                e.print(Some("INFO"));
            }
            e
        })
    }};
    ($ops: expr, $msg: expr) => {{
        $crate::info!($ops, "{}", $msg)
    }};
    ($ops: expr) => {{
        $crate::info!($ops, "")
    }};
}

/// omit the result without printing any message
#[macro_export]
macro_rules! omit {
    ($ops: expr) => {{
        let _ = $ops;
    }};
}

/// drop the result afeter printing the message
#[macro_export]
macro_rules! info_omit {
    ($ops: expr, $fmt: expr, $($arg:tt)*) => {{
        $crate::omit!($crate::info!($ops, $fmt, $($arg)*));
    }};
    ($ops: expr, $msg: expr) => {{
        $crate::info_omit!($ops, "{}", $msg)
    }};
    ($ops: expr) => {{
        $crate::info_omit!($ops, "")
    }};
}

/// print debug-info, eg: modular and file path, line number ...
#[macro_export]
macro_rules! d {
    ($fmt: expr, $($arg:tt)*) => {{
        let err = format!("{}", format_args!($fmt, $($arg)*));
        $crate::err::SimpleMsg::new(err, file!(), line!(), column!())
    }};
    ($err: expr) => {{
        $crate::d!("{}", $err)
    }};
    () => {{
        $crate::d!("")
    }};
}

/// print custom msg
#[macro_export]
macro_rules! print_msg {
    ($fmt: expr, $($arg:tt)*) => {{
        println!("\n{}", $crate::d!($fmt, $($arg)*));
    }};
    ($msg: expr) => {{
        $crate::print_msg!("{}", $msg)
    }};
}

/// Just a panic
#[macro_export]
macro_rules! die {
    ($fmt: expr, $($arg:tt)*) => {{
        $crate::print_msg!($fmt, $($arg)*);
        panic!();
    }};
    ($msg:expr) => {{
        $crate::die!("{}", $msg);
    }};
    () => {
        $crate::die!("");
    };
}

/// Print log, and panic
#[macro_export]
macro_rules! pnk {
    ($ops: expr, $fmt: expr, $($arg:tt)*) => {{
        $ops.c($crate::d!($fmt, $($arg)*)).unwrap_or_else(|e| e.print_die())
    }};
    ($ops: expr, $msg: expr) => {{
        $crate::pnk!($ops, "{}", $msg)
    }};
    ($ops: expr) => {{
        $crate::pnk!($ops, "")
    }};
}

/// Generate error with debug info
#[macro_export]
macro_rules! eg {
    ($fmt: expr, $($arg:tt)*) => {{
        Box::new($crate::err::SimpleError::new($crate::d!($fmt, $($arg)*), None))
            as Box<dyn $crate::err::RucError>
    }};
    ($err: expr) => {{
        $crate::eg!("{}", $err)
    }};
    () => {
        $crate::eg!("")
    };
}

#[cfg(test)]
mod tests {
    use crate::*;

    fn display_style_inner() -> Result<()> {
        #[derive(Debug, Eq, PartialEq)]
        struct CustomErr(i32);

        let l1 = || -> Result<()> { Err(eg!("The final error message!")) };
        let l2 = || -> Result<()> { l1().c(d!()).or_else(|e| l1().c(d!(e))) };
        let l3 = || -> Result<()> { l2().c(d!("A custom message!")) };
        let l4 = || -> Result<()> { l3().c(d!("ERR_UNKNOWN")) };
        let l5 = || -> Result<()> { l4().c(d!("{:?}", CustomErr(-1))) };

        l5().c(d!())
    }

    #[test]
    #[should_panic]
    fn t_display_style() {
        pnk!(display_style_inner());
    }

    #[test]
    fn t_macros() {
        let s1 = map! {1 => 2, 2 => 4};
        let s2 = map! {B 1 => 2, 2 => 4};
        assert_eq!(s1.len(), s2.len());
        for (idx, (k, v)) in s2.into_iter().enumerate() {
            assert_eq!(1 + idx, k);
            assert_eq!(2 * k, v);
        }

        let _ = info!(Err::<u8, _>(eg!()));
        omit!(Err::<u8, _>(eg!()));
        info_omit!(Err::<u8, _>(eg!()));
        print_msg!("{:?}", ts!());
    }
}