error_handling/lib.rs
1#![allow(clippy::crate_in_macro_def)]
2/// # Description
3/// This macro handles errors inserting error handling logic for a given `Result<T, crate::error::Error<'a>>`
4/// it is used by passing in a `Sender<crate::status::Status>` as the first parameter
5/// and a `Result<T, crate::error::Error<'a>>` as the second parameter.
6///
7/// NOTE: There are 3 caveats to using this macro:
8/// 1. can only be used within async functions since status needs to be send over async channel
9/// 2. The macro must be used within a loop since it calls `continue` on error. If `unwraps/expects` are used within a function
10/// without a loop, you should make the function return a result and handle the result within a main loop
11/// 3. The macro must be able to reference the user defined function `crate::status::handle_error(T, U) -> ErrorBranch;` where U is the output
12/// of `e.into()`
13///
14/// # Example
15/// ``` ignore
16/// let (tx_status: Sender<Status>, rx_status) = async_channel::unbounded();
17/// let variable = handle_result!(
18/// tx_status,
19/// type_.try_into()
20/// );
21/// ```
22#[macro_export]
23macro_rules! handle_result {
24 ($sender:expr, $res:expr) => {
25 match $res {
26 Ok(val) => val,
27 Err(e) => {
28 // handle error
29 let res = crate::status::handle_error(&$sender, e.into()).await;
30 match res {
31 error_handling::ErrorBranch::Break => break,
32 error_handling::ErrorBranch::Continue => continue,
33 }
34 }
35 }
36 };
37}
38
39pub enum ErrorBranch {
40 Break,
41 Continue,
42}