error_log/
macros.rs

1#[cfg(doc)]
2use crate::ErrorLog;
3
4#[cfg(feature = "errors")]
5#[macro_export]
6/**
7Macro to [`push_result()`][crate::ErrorLog::push_result] and return given [`ErrorLog`][crate::ErrorLog] if given [`Result`] is an [`Err`]
8
9Same as
10#[cfg_attr(tarpaulin, ignore)]
11```
12# use error_log::ErrorLog;
13
14# fn run() -> ErrorLog<i32, std::num::ParseIntError> {
15    let mut err_log = ErrorLog::<i32, std::num::ParseIntError>::new();
16    match "a1".parse::<i32>() {
17        Ok(o) => err_log.set_ok(o),
18        Err(e) => {
19            err_log.push_err(e);
20            return err_log;
21        }
22    };
23    # err_log
24# }
25*/
26macro_rules! try_add {
27    ($res: expr, $errlog: ident) => {
28        match $errlog.push_result($res) {
29            Some(o) => o,
30            None => return $errlog,
31        }
32    };
33}
34
35#[cfg(feature = "errors")]
36#[macro_export]
37/**
38Macro to [`merge_result()`][crate::ErrorLog::merge_result] and return given [`ErrorLog`][crate::ErrorLog] if given [`Result`] is an [`Err`]
39
40Same as
41```
42# use error_log::ErrorLog;
43
44# fn run() -> ErrorLog<i32, std::num::ParseIntError> {
45    let mut err_log = ErrorLog::<i32, std::num::ParseIntError>::new();
46    match "a1".parse::<i32>() {
47        Ok(o) => err_log.set_ok(o),
48        Err(e) => {
49            err_log.push_err(e);
50            return err_log;
51        }
52    };
53    # err_log
54# }
55```
56
57Arguments:
581. [`Result`]
592. [`ErrorLog`]
60*/
61macro_rules! try_merge {
62    ($res: expr, $errlog: ident) => {
63        if !$errlog.merge_result($res) {
64            return $errlog;
65        }
66    };
67}
68
69#[cfg(feature = "errors")]
70#[macro_export]
71/**
72Attach error to given [`ErrorLog`][crate::ErrorLog] and return it.
73
74Arguments:
751. err value
762. [`ErrorLog`]
77*/
78macro_rules! return_err {
79    ($err: expr, $errlog: ident) => {
80        $errlog.push_err(e);
81        return $errlog;
82    };
83}
84
85#[macro_export]
86/**
87Set `ok` value of given [`ErrorLog`][crate::ErrorLog] and return it
88
89Arguments:
901. err value
912. [`ErrorLog`]
92*/
93macro_rules! return_ok {
94    ($err: expr, $errlog: ident) => {
95        $errlog.set_ok(e);
96        return $errlog;
97    };
98}