Crates.io | medi-rs |
lib.rs | medi-rs |
version | 1.0.0 |
source | src |
created_at | 2024-10-30 15:11:25.942363 |
updated_at | 2024-10-30 20:22:19.470724 |
description | A yet another mediator library for Rust |
homepage | https://github.com/klab365/medi-rs |
repository | https://github.com/klab365/medi-rs |
max_upload_size | |
id | 1428652 |
size | 35,184 |
A flexible and lightweight mediator library for Rust, inspired by Axum’s handler function pattern. medi-rs
provides a clean and effective approach to command and event handling in Rust applications, with dependency injection support.
medi-rs
helps organize complex Rust applications by defining a clear separation between request-response operations and event-driven tasks. Usage examples can be found in the tests
folder.
medi-rs
supports two primary handler types:
Request-Response Handler: Designed for commands and queries. This handler receives a request and returns a response, ensuring the caller waits for the operation to complete.
Event Handler: Tailored for events. It receives a request but does not return a response or make the caller wait. Instead, it publishes the event to all designated handlers, allowing for efficient, non-blocking event processing.
Handlers in medi-rs
can be equipped with dependencies, simplifying access to shared resources. Use the FromResource
trait to declare a struct as a dependency that can then be injected into handler functions. The maximum numerber of dependencies is 7.
#[derive(Debug)]
struct MyResource;
impl FromResource for MyResource {}
async fn handler(dep1: MyResource, req: Request) -> HandlerResult<()> {
// Handler logic here
}
All commands for building, testing, and running the project are defined in the Justfile
and can be executed with the just command.
You can quickly set up the development environment using the provided DevContainer configuration. Prerequisites are Docker and Visual Studio Code with the Remote - Containers extension.
To build the project, run the following command:
just build
To run the test suite, execute the following command:
just test