ddd_toolkit_core/event_delivery_metadata.rs
1use crate::DomainEvent;
2
3/// **English:**<br>
4/// Owned delivery identifiers, independent of the domain event payload.
5///
6/// Strings are stored without format or emptiness validation. Providing an event identifier
7/// does not enforce uniqueness or deduplication; correlation and causation are optional labels.
8///
9/// Attach tracing labels without changing the event identifier.
10///
11/// **日本語:**<br>
12/// ドメインイベントのペイロードから独立した、所有された配信識別子です。
13///
14/// 文字列の形式や空文字列は検証しません。イベント識別子の指定は一意性や重複排除を
15/// 強制しません。相関識別子と原因識別子は任意のラベルです。
16///
17/// イベント識別子を変えずにトレース用のラベルを付けます。
18///
19/// # Example / 使用例
20///
21/// ```rust
22/// use ddd_toolkit_core::DeliveryMetadata;
23///
24/// let metadata = DeliveryMetadata::new("event-1")
25/// .with_correlation_id("request-7")
26/// .with_causation_id("command-2");
27/// assert_eq!(metadata.event_id(), "event-1");
28/// assert_eq!(metadata.correlation_id(), Some("request-7"));
29/// assert_eq!(metadata.causation_id(), Some("command-2"));
30/// ```
31#[derive(Clone, Debug, Eq, Hash, PartialEq)]
32pub struct DeliveryMetadata {
33 event_id: String,
34 correlation_id: Option<String>,
35 causation_id: Option<String>,
36}
37
38impl DeliveryMetadata {
39 /// **English:**<br>
40 /// Stores `event_id` after conversion to `String`, leaving both optional IDs unset.
41 /// No identifier validation or generation is performed.
42 ///
43 /// **日本語:**<br>
44 /// `event_id` を `String` に変換して保存し、二つの任意識別子は未設定にします。
45 /// 識別子の検証や生成は行いません。
46 pub fn new(event_id: impl Into<String>) -> Self {
47 Self {
48 event_id: event_id.into(),
49 correlation_id: None,
50 causation_id: None,
51 }
52 }
53
54 /// **English:**<br>
55 /// Consumes this value and returns it with the correlation ID set to `id`.
56 /// A previously set correlation ID is replaced.
57 ///
58 /// **日本語:**<br>
59 /// 自身を消費し、相関識別子を `id` に設定して返します。
60 /// 設定済みの相関識別子は置き換えます。
61 pub fn with_correlation_id(mut self, id: impl Into<String>) -> Self {
62 self.correlation_id = Some(id.into());
63 self
64 }
65
66 /// **English:**<br>
67 /// Consumes this value and sets the causation ID to `id`, replacing any prior value.
68 ///
69 /// **日本語:**<br>
70 /// 自身を消費し、原因識別子を `id` に設定します。以前の値は置き換えます。
71 pub fn with_causation_id(mut self, id: impl Into<String>) -> Self {
72 self.causation_id = Some(id.into());
73 self
74 }
75
76 /// **English:**<br>
77 /// Borrows the event identifier as stored, without allocating.
78 ///
79 /// **日本語:**<br>
80 /// 保存されたイベント識別子を、割り当てなしで借用します。
81 pub fn event_id(&self) -> &str {
82 &self.event_id
83 }
84
85 /// **English:**<br>
86 /// Borrows the correlation ID, or returns `None` if it has not been set.
87 ///
88 /// **日本語:**<br>
89 /// 相関識別子を借用し、未設定なら `None` を返します。
90 pub fn correlation_id(&self) -> Option<&str> {
91 self.correlation_id.as_deref()
92 }
93
94 /// **English:**<br>
95 /// Borrows the causation ID, or returns `None` when no cause label is stored.
96 ///
97 /// **日本語:**<br>
98 /// 原因識別子を借用し、原因のラベルが保存されていなければ `None` を返します。
99 pub fn causation_id(&self) -> Option<&str> {
100 self.causation_id.as_deref()
101 }
102}
103
104/// **English:**<br>
105/// Opt-in access to delivery labels on a [`DomainEvent`].
106///
107/// Implementing [`DomainEvent`] alone does not require these labels. This trait does not
108/// assign identifiers or prescribe how metadata is stored or transported.
109///
110/// **日本語:**<br>
111/// [`DomainEvent`] に配信ラベルへのアクセスを任意で追加します。
112///
113/// [`DomainEvent`] の実装だけではラベルは必須ではありません。このトレイトは
114/// 識別子の割り当てやメタデータの保存・転送方法を規定しません。
115pub trait DomainEventDeliveryMetadata: DomainEvent {
116 /// **English:**<br>
117 /// Borrows this event's delivery metadata, or returns `None` if unavailable.
118 /// The returned reference cannot outlive the event borrow.
119 ///
120 /// **日本語:**<br>
121 /// このイベントの配信メタデータを借用し、存在しなければ `None` を返します。
122 /// 返される参照の有効期間はイベントの借用を超えません。
123 fn delivery_metadata(&self) -> Option<&DeliveryMetadata>;
124}