1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Mediator traits: IMediator, IRequest, IEventRequest.
pub use Mediator;
pub use dispatch;
use crateResult;
/// 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 {}
/// ```
/// Marker trait for an event (notification) that does not produce a response.
///
/// Use `IEventRequest` for fire-and-forget notifications.
///
/// ```ignore
/// impl IEventRequest for UserCreatedEvent {}
/// ```
/// The mediator dispatches requests to their handlers and publishes events
/// to all registered handlers.
///
/// Analogous to MediatR's IMediator.
///
/// # Object safety
///
/// The trait is dyn-compatible for callers that only need type-erased dispatch:
/// the generic `send`/`publish` methods require `Self: Sized`, while
/// [`IMediator::send_boxed`] takes/returns `Box<dyn Any + Send>` and is the one
/// available on `dyn IMediator`:
///
/// ```ignore
/// let mediator: Arc<dyn IMediator> = Arc::new(Mediator::new(provider));
/// let response = mediator.send_boxed(Box::new(CreateOrderRequest { .. })).await?;
/// let order = *response.downcast::<OrderView>().expect("typed response");
/// ```