function-compose-proc-macros

Crates.iofunction-compose-proc-macros
lib.rsfunction-compose-proc-macros
version0.2.0
sourcesrc
created_at2024-06-27 10:13:03.821302
updated_at2024-08-22 07:27:05.151858
descriptionlib to allow composition of sync and async rust functions
homepage
repositoryhttps://gitlab.com/coderodent/fn-composer
max_upload_size
id1285288
size22,479
(senthil-veerabahu)

documentation

README

Composeable macro Usage

Sync function
#[composeable()]
pub fn add_10(a: i32) -> Result<i32, String> {
    Ok(a + 10)
}

Async Function

The async function should return BoxFuture.

#[composeable()]
pub fn add_async(a: i32, b: i32) -> BoxFuture<'static, Result<i32, String>> {
    async move {
        let r = a + b;
        Ok(r)
    }.boxed()
}

Retry in Fn Composer

Composeable macro supports retrying a function at specified interval in case of Error returned by the function. This could be useful when trying make a database call or connect to network endpoint. Make sure to install https://docs.rs/retry/latest/retry/ before proceeding with retry feature.

Retry mechanism is implemented as part of composeable procedureal macro. Below is example of add_10 function configured to be retried 2 times after initial failure.

use retry::delay::*;
#[composeable(retry = Fixed::from_millis(100).take(2))]
pub fn add_10(a: i32) -> Result<i32, String> {
    Ok(a + 10)
}

Retry can be applied to both sync and async functions.

for async functions, all arguments to the function must be either shared reference or exclusive reference.

Below is example of async function with retry.

#[composeable(retry = Fixed::from_millis(100))]
pub fn add_3_arg_ref__non_copy_async<'a>(
    a: &'a mut Vec<String>,
    b: &'a mut Vec<String>,
    c: &'a Vec<String>,
) -> BoxFuture<'a, Result<i32, String>> {
    async move {
        let r = a.len() + b.len() + c.len();
        Ok(r as i32)
    }
    .boxed()
}

Apart from fixed duration retries, it is possible to configure with exponential delay. Refer to retry documentation for all available delay options https://docs.rs/retry/latest/retry/all.html

Commit count: 39

cargo fmt