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
/// A macro for building `Report<AnyErr>` objects with string information easily.
///
/// `anyerr!()` is equivalent to `Report::new(AnyErr)`
///
/// `anyerr!("foo")` is equivalent to `Report::new(AnyErr).attach_printable("foo")`
///
/// `anyerr!("foo: {}", "bar")` is equivalent to `Report::new(AnyErr).attach_printable(format!("foo: {}", "bar"))`
#[macro_export]
macro_rules! anyerr {
    () => {{
        use error_stack::Report;
        use $crate::errors::AnyErr;

        Report::new(AnyErr)
    }};

    ($str:expr) => {{
        use error_stack::Report;
        use $crate::errors::AnyErr;

        Report::new(AnyErr).attach_printable($str)
    }};

    ($str:expr, $($arg:expr),*) => {{
        use error_stack::Report;
        use $crate::errors::AnyErr;

        Report::new(AnyErr).attach_printable(format!($str, $($arg),*))
    }};
}

/// A macro for building `Report<ArbitraryErrorStackErr>` objects with string context easily.
///
/// `err!(Err)` is equivalent to `Report::new(Err)`
///
/// `err!(Err, "foo")` is equivalent to `Report::new(Err).attach_printable("foo")`
///
/// `err!(Err, "foo: {}", "bar")` is equivalent to `Report::new(Err).attach_printable(format!("foo: {}", "bar"))`///
#[macro_export]
macro_rules! err {
    ($err_variant:expr) => {{
        use error_stack::Report;

        Report::new($err_variant)
    }};

    ($err_variant:expr, $str:expr) => {{
        use error_stack::Report;

        Report::new($err_variant).attach_printable($str)
    }};

    ($err_variant:expr, $str:expr, $($arg:expr),*) => {{
        use error_stack::Report;

        Report::new($err_variant).attach_printable(format!($str, $($arg),*))
    }};
}

/// When working in a function that cannot return a result, use this to auto panic with the formatted error if something goes wrong.
///
/// Allows use of e.g. `?` in the block.
#[macro_export]
macro_rules! panic_on_err {
    ($closure:block) => {{
        use error_stack::{Result, ResultExt};
        use $crate::errors::AnyErr;

        match (|| -> Result<_, AnyErr> { $closure })() {
            Ok(s) => s,
            Err(e) => {
                panic!("{:?}", e);
            }
        }
    }};
}