Crates.io | sena |
lib.rs | sena |
version | 0.2.0 |
source | src |
created_at | 2024-05-12 23:23:57.486255 |
updated_at | 2024-05-13 00:55:43.858833 |
description | Library for composable event handlers |
homepage | https://github.com/nerodono/sena-rs |
repository | https://github.com/nerodono/sena-rs |
max_upload_size | |
id | 1237818 |
size | 46,938 |
Library for composable handlers. Many things can be expressed in the following way:
sena
is a library that simplifies writing code in that pattern, for example:
use std::num::ParseIntError;
use sena::handling::Handler;
fn increment<E>() -> impl Handler<i32, E, Output = i32> {
|req: i32| async move {
Ok(req + 1)
}
}
fn parse_int<E: From<ParseIntError>>() -> impl Handler<String, E, Output = i32> {
|req: String| async move { req.parse().map_err(Into::into) }
}
async fn entrypoint() -> Result<(), ParseIntError> {
let chain = parse_int::<ParseIntError>() // parses input string
.pipe(increment()) // pipes output from parse_int to increment
.pipe(increment()); // one more time
let result = chain.handle("100".to_owned()).await?;
assert_eq!(result, 102);
Ok(())
}
Let's break that code into parts:
handling::Handler
] traitparse_int
is a handler that converts String
to a i32
.pipe
method on the handler pipes input from left-hand side handler (before the dot) to right-hand side handler (increment()
in ourcase)chain.handle
on resulting handlerThere's more handlers for composability, like [handling::seq::Seq
] handler that allows running "middlewares" (functions with continuations) on the handler
or [handling::map::MapAsync
]/[handling::map::Map
] that allows converting handler's input to another type.
Also take a look at [csp
] module and at [handling::server::Server
], this can be used to turn your handler into a server or "actor".
sena
doesn't provide batteries for dependency injection, however, it provides [handling::Handler::provide
] and [dependent::Dependent
] for basic dependency injection
needs, these can be sufficient for great amount of use-cases.
It is a good solution, since it allows usage of handler in more flexible way: handler can be used with any error type as long as its specific errors can be converted to that type.
Take a look at github: https://github.com/nerodono/sena-rs