ddd_toolkit_core/domain_event_metadata.rs
1//! **English:**<br>
2//! Defines stable identification and optional metadata for a [`crate::DomainEvent`].
3//!
4//! Implement [`DomainEventMetadata`] when consumers need an event name, a schema or contract
5//! version, and optional event-specific metadata without taking ownership of the event.
6//!
7//! **日本語:**<br>
8//! [`crate::DomainEvent`] の安定した識別情報と任意のメタデータを定義します。
9//!
10//! イベントの所有権を取り上げずに、イベント名、スキーマまたは契約のバージョン、
11//! イベント固有の任意メタデータを利用者へ提供する場合に [`DomainEventMetadata`] を実装します。
12
13use crate::DomainEvent;
14
15/// **English:**<br>
16/// Provides an event name, version, and optional metadata for a domain event.
17///
18/// The associated metadata may be unsized, such as a dynamically sized string or slice. The
19/// default event version is `1`; implementations may override it to identify a later contract.
20/// Implementations choose whether metadata is present for a given event and how the returned name maps to
21/// external event contracts.
22///
23/// **日本語:**<br>
24/// ドメインイベントのイベント名、バージョン、任意のメタデータを提供します。
25///
26/// 関連付けられたメタデータは、動的サイズの文字列やスライスなど、サイズ不定でも構いません。
27/// イベントバージョンの既定値は `1` であり、実装側で上位の契約を示す値に変更できます。
28/// 個々のイベントにメタデータを用意するかどうかや、返す名前を外部イベント契約へどう対応付けるかは実装が決めます。
29pub trait DomainEventMetadata: DomainEvent {
30 /// **English:**<br>
31 /// The metadata type associated with this event.
32 ///
33 /// The `?Sized` bound permits borrowed unsized metadata such as `str` or `[u8]`.
34 ///
35 /// **日本語:**<br>
36 /// このイベントに関連付けられたメタデータの型です。
37 ///
38 /// `?Sized` 境界により、`str` や `[u8]` のような借用されたサイズ不定のメタデータを指定できます。
39 type Metadata: ?Sized;
40
41 /// **English:**<br>
42 /// Returns the stable name used to identify this event type.
43 ///
44 /// The returned string has a `'static` lifetime and is not tied to an event instance.
45 ///
46 /// **日本語:**<br>
47 /// このイベント型を識別するための安定した名前を返します。
48 ///
49 /// 返される文字列のライフタイムは `'static` であり、イベントのインスタンスには依存しません。
50 fn event_name() -> &'static str;
51
52 /// **English:**<br>
53 /// Returns the contract version for this event type.
54 ///
55 /// The default implementation returns `1`. This method does not validate compatibility with
56 /// consumers or perform migration.
57 ///
58 /// **日本語:**<br>
59 /// このイベント型の契約バージョンを返します。
60 ///
61 /// 既定の実装は `1` を返します。このメソッドは利用者との互換性検証や移行を行いません。
62 fn event_version() -> u32 {
63 1
64 }
65
66 /// **English:**<br>
67 /// Returns optional metadata borrowed from this event.
68 ///
69 /// `Some` contains a reference valid for the lifetime of the borrow; `None` indicates that
70 /// this event has no metadata. The method does not transfer ownership.
71 ///
72 /// **日本語:**<br>
73 /// このイベントから借用した任意のメタデータを返します。
74 ///
75 /// `Some` には借用期間中有効な参照が入り、`None` はメタデータがないことを示します。
76 /// このメソッドは所有権を移転しません。
77 fn metadata(&self) -> Option<&Self::Metadata>;
78}