Skip to main content

ddd_toolkit_core/
domain_event_metadata.rs

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