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