rust_webx_core/mediator.rs
1//! Mediator traits: IMediator, IRequest, IEventRequest.
2
3pub mod default;
4pub mod dispatch;
5pub mod pipeline;
6pub use default::Mediator;
7pub use dispatch::dispatch;
8pub use pipeline::build_chain as build_pipeline_chain;
9
10use crate::error::Result;
11
12/// Marker trait for a request (command or query) carrying a structured response `TResponse`.
13///
14/// - `TResponse: Serialize` → framework writes JSON and sets status 200
15/// - `TResponse = ()` → framework writes no body and sets status 204
16///
17/// ```ignore
18/// impl IRequest<UserModel> for GetUserRequest {}
19/// impl IRequest<()> for DeleteUserRequest {}
20/// ```
21pub trait IRequest<TResponse>: Send + 'static
22where
23 TResponse: serde::Serialize + Send + 'static,
24{
25}
26
27/// Marker trait for an event (notification) that does not produce a response.
28///
29/// Use `IEventRequest` for fire-and-forget notifications.
30///
31/// ```ignore
32/// impl IEventRequest for UserCreatedEvent {}
33/// ```
34pub trait IEventRequest: Clone + Send + 'static {}
35
36/// The mediator dispatches requests to their handlers and publishes events
37/// to all registered handlers.
38///
39/// Analogous to MediatR's IMediator.
40#[async_trait::async_trait]
41pub trait IMediator: Send + Sync {
42 /// Send a request and return its structured response.
43 async fn send<T, R>(&self, req: T) -> Result<R>
44 where
45 T: IRequest<R> + Send + 'static,
46 R: serde::Serialize + Send + 'static;
47
48 /// Publish an event to all registered handlers.
49 async fn publish<T: IEventRequest>(&self, event: T) -> Result<()>;
50}