retry_macro

A library which provides macros that automatically re-execute both synchronous and asynchronous (tokio with sleep) functions upon failure.

Feature flags

Enable features = ["tokio"] if you want to use the retry_async_sleep macro.

Examples

Here are two simple examples using both the synchronous and asynchronous macros. Note that all function inputs must be bounded to an identifier (variable).

Synchronous

```rust use retrymacro::{retry, retrysleep, RetryError};

fn can_fail(input: &str) -> Result { input.parse::() }

fn main() { // Retry the canfail function 5 times with the input "notanumber" let var = "notanumber"; let res = retry!(5, canfail, var); assert!(res.iserr()); asserteq!(res.unwrap_err().retries.len(), 5);

// Retry the can_fail function 5 times with the input "not_a_number", sleep for 100 milliseconds between retries
let var = "not_a_number";
let res = retry_sleep!(5, 100, can_fail, var);
assert!(res.is_err());
assert_eq!(res.unwrap_err().retries.len(), 5);

} ```

Asynchronous

```rust use retrymacro::{retryasync, retryasyncsleep, RetryError};

async fn canfailasync(input: &str) -> Result { tokio::time::sleep(tokio::time::Duration::from_millis(1)).await; input.parse::() }

[tokio::main]

async fn main() { // Retry the canfail function 5 times with the input "notanumber" let var = "notanumber"; let res = retryasync!(5, canfailasync, var); assert!(res.iserr()); asserteq!(res.unwrap_err().retries.len(), 5);

// Retry the can_fail function 5 times with the input "not_a_number", sleep for 100 milliseconds between retries
let var = "not_a_number";
let res = retry_async_sleep!(5, 100, can_fail_async, var);
assert!(res.is_err());
assert_eq!(res.unwrap_err().retries.len(), 5);

} ```

License

retry_macro is distributed under the MIT and Apache-2.0 licenses.