ddd_toolkit_core/domain_event_publisher.rs
1use crate::DomainEvent;
2
3/// **English:** Publishes events that implement [`DomainEvent`].
4///
5/// A publisher may keep mutable state, such as a connection or an in-memory queue. Each call
6/// receives a shared reference to one event and returns the publisher-defined error type when
7/// publishing fails. Implementations decide whether events are delivered synchronously and
8/// whether a successful return means acceptance, delivery, or queuing.
9///
10/// **日本語:** [`DomainEvent`] を実装するイベントを発行します。
11///
12/// パブリッシャーは、接続やメモリ内キューなどの可変状態を保持できます。各呼び出しは
13/// 1 つのイベントへの共有参照を受け取り、発行に失敗した場合はパブリッシャー固有の
14/// エラー型を返します。イベントを同期的に配送するか、成功が受理・配送・キュー投入の
15/// どれを意味するかは実装が決定します。
16pub trait DomainEventPublisher {
17 /// **English:** The event type accepted by [`Self::publish`].
18 ///
19 /// **日本語:** [`Self::publish`] が受け付けるイベント型です。
20 type Event: DomainEvent;
21
22 /// **English:** The error returned when [`Self::publish`] cannot publish an event.
23 ///
24 /// **日本語:** [`Self::publish`] がイベントを発行できない場合に返すエラー型です。
25 type Error;
26
27 /// **English:** Attempts to publish `event` using this publisher.
28 ///
29 /// The event is borrowed for the duration of the call, so the implementation must not retain
30 /// the reference after returning. A successful result has publisher-specific delivery
31 /// semantics; an error indicates that the implementation could not complete its publish
32 /// operation.
33 ///
34 /// **日本語:** このパブリッシャーを使って `event` の発行を試みます。
35 ///
36 /// イベントは呼び出し中だけ借用されるため、実装は戻り値を返した後にその参照を保持
37 /// してはいけません。成功時の配送の意味はパブリッシャーごとに異なり、エラーは実装が
38 /// 発行処理を完了できなかったことを示します。
39 fn publish(&mut self, event: &Self::Event) -> Result<(), Self::Error>;
40}