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