rust-webx-core 0.4.0

rust-webx core traits: IRequest, IRequestHandler, IMiddleware, IHttpContext, IMediator, and more
Documentation
//! Mediator traits: IMediator, IRequest, IEventRequest.

pub mod default;
pub mod dispatch;
pub mod pipeline;
pub use default::Mediator;
pub use dispatch::dispatch;
pub use pipeline::build_chain as build_pipeline_chain;

use crate::error::Result;

/// Marker trait for a request (command or query) carrying a structured response `TResponse`.
///
/// The HTTP adapter serializes `TResponse` as JSON, so it must implement
/// [`serde::Serialize`] — unless it is [`crate::route::scan::ResponseData`],
/// the raw-response envelope used for file downloads and other responses that
/// control their own status, headers and body. That is why this trait itself
/// only requires `Send`: the `Serialize` bound is enforced where the value is
/// actually serialized.
///
/// - `TResponse: Serialize` → framework writes JSON and sets status 200
/// - `TResponse = ()`        → framework writes no body and sets status 204
/// - `TResponse = ResponseData` → the handler builds the whole response
///
/// ```ignore
/// impl IRequest<UserModel> for GetUserRequest {}
/// impl IRequest<()> for DeleteUserRequest {}
/// impl IRequest<ResponseData> for DownloadFileRequest {}
/// ```
pub trait IRequest<TResponse>: Send + 'static
where
    TResponse: Send + 'static,
{
}

/// Marker trait for an event (notification) that does not produce a response.
///
/// Use `IEventRequest` for fire-and-forget notifications.
///
/// ```ignore
/// impl IEventRequest for UserCreatedEvent {}
/// ```
pub trait IEventRequest: Clone + Send + 'static {}

/// The mediator dispatches requests to their handlers and publishes events
/// to all registered handlers.
///
/// Analogous to MediatR's IMediator.
#[async_trait::async_trait]
pub trait IMediator: Send + Sync {
    /// Send a request and return its structured response.
    async fn send<T, R>(&self, req: T) -> Result<R>
    where
        T: IRequest<R> + Send + 'static,
        R: Send + 'static;

    /// Publish an event to all registered handlers.
    async fn publish<T: IEventRequest>(&self, event: T) -> Result<()>;
}