Expand description
mediator-rs
An implementation of the Mediator pattern in Rust inspired in C# MediatR.
Mediator Pattern
https://en.wikipedia.org/wiki/Mediator_pattern
Usage
[dependencies]
mediator = "0.1"
Example
use mediator::{DefaultMediator, Mediator, Request, Event, RequestHandler, EventHandler};
#[derive(Clone, Debug)]
enum Op {
Add(f32, f32),
Sub(f32, f32),
Mul(f32, f32),
Div(f32, f32),
}
struct MathRequest(Op);
impl Request<Option<f32>> for MathRequest {}
#[derive(Clone, Debug)]
struct MathEvent(Op, Option<f32>);
impl Event for MathEvent {}
struct MathRequestHandler(DefaultMediator);
impl RequestHandler<MathRequest, Option<f32>> for MathRequestHandler {
fn handle(&mut self, req: MathRequest) -> Option<f32> {
let result = match req.0 {
Op::Add(a, b) => Some(a + b),
Op::Sub(a, b) => Some(a - b),
Op::Mul(a, b) => Some(a * b),
Op::Div(a, b) => {
if b == 0.0 { None } else { Some(a / b) }
}
};
self.0.publish(MathEvent(req.0, result));
result
}
}
fn main() {
let mut mediator = DefaultMediator::builder()
.add_handler_deferred(|m| MathRequestHandler(m))
.subscribe_fn(|event: MathEvent| {
println!("{:?}", event);
})
.build();
}
Modules
Module for the errors.
Structs
A default implementation for the Mediator trait.
A builder for the DefaultMediator.
Traits
Represents an application event.
A handler for application events.
A mediator is a central hub for communication between components.
Represents a request to the mediator.
Handles a request to the mediator.
Type Definitions
A convenient result type.