Skip to main content

Crate hexeract_mediator

Crate hexeract_mediator 

Source
Expand description

In-process mediator for the Hexeract messaging framework.

The mediator dispatches a Command, a Query or a Notification to the handlers registered with a MediatorBuilder at startup. Dispatch is type-safe and reflection-free: each call to Mediator::send, Mediator::query or Mediator::publish resolves to the matching handler through a compile-time generic, while the internal registry erases the handler types behind a TypeId lookup table.

Commands and queries are single-handler: registering a second handler for the same type is a build-time error. Notifications are multi-handler and fan out concurrently; every handler runs regardless of its siblings, and failures are aggregated so one handler’s error never hides another.

To propagate a CorrelationId across in-process dispatches (for example forwarding ctx.correlation_id from a command handler to a follow-up notification), use Mediator::send_with_correlation_id, Mediator::query_with_correlation_id or Mediator::publish_with_correlation_id. The plain send / query / publish methods mint a fresh id each time.

The hexeract-middleware crate ships two built-in middlewares: TracingMiddleware and TimeoutMiddleware. Wire them through MediatorBuilder::with_middleware, or supply your own Middleware implementations for other cross-cutting concerns.

§Example

use hexeract_core::{Command, CommandHandler, HandlerContext, HexeractError};
use hexeract_mediator::MediatorBuilder;

struct Greet { name: String }

impl Command for Greet {
    type Output = String;
}

struct GreetHandler;

impl CommandHandler<Greet> for GreetHandler {
    type Error = HexeractError;
    async fn handle(&self, cmd: Greet, _ctx: &HandlerContext)
        -> Result<String, Self::Error>
    {
        Ok(format!("hello {}", cmd.name))
    }
}

let mediator = MediatorBuilder::new()
    .register_command_handler::<Greet, _>(GreetHandler)
    .build()?;

let greeting = mediator.send(Greet { name: "world".into() }).await?;
assert_eq!(greeting, "hello world");

Structs§

Mediator
In-process dispatcher for commands, queries and notifications.
MediatorBuilder
Fluent builder that wires handlers and middlewares into a Mediator.
MissingHandler
One handler that was declared via the #[handler] attribute macro but never registered through MediatorBuilder.

Enums§

HandlersVerificationError
Errors raised by MediatorBuilder::verify_handlers.
MediatorBuildError
Errors raised by MediatorBuilder::build when the requested configuration is inconsistent. Handler failures at dispatch time keep flowing through HexeractError.