Skip to main content

ddd_toolkit_core/
domain_event_conversion.rs

1//! **English:** Defines conversion behavior for a [`crate::DomainEvent`] value.
2//!
3//! Implement [`DomainEventConversion`] when an event can be transformed into another representation
4//! and the transformation may fail.
5//!
6//! **日本語:** [`crate::DomainEvent`] の値を変換する動作を定義します。
7//!
8//! イベントを別の表現へ変換でき、その変換が失敗する可能性がある場合に
9//! [`DomainEventConversion`] を実装します。
10
11use crate::DomainEvent;
12
13/// **English:** Converts a domain event into an associated output type.
14///
15/// The conversion borrows the event, so implementations can inspect it without taking ownership.
16/// A failed conversion returns the associated [`Self::Error`] value and does not produce output.
17/// The trait does not prescribe serialization, allocation, or whether the conversion is deterministic.
18///
19/// **日本語:** ドメインイベントを関連付けられた出力型へ変換します。
20///
21/// 変換はイベントを借用して行うため、所有権を取り上げずにイベントを検査できます。
22/// 変換に失敗した場合は関連付けられた [`Self::Error`] の値を返し、出力は生成しません。
23/// シリアライズ、メモリアロケーション、変換結果の決定性はこのトレイトでは規定しません。
24pub trait DomainEventConversion: DomainEvent {
25    /// **English:** The value produced when conversion succeeds.
26    ///
27    /// **日本語:** 変換に成功したときに生成される値です。
28    type Output;
29
30    /// **English:** The error returned when conversion fails.
31    ///
32    /// **日本語:** 変換に失敗したときに返されるエラーです。
33    type Error;
34
35    /// **English:** Converts this event without consuming it.
36    ///
37    /// Returns [`Self::Output`] on success or [`Self::Error`] when the event cannot be converted.
38    /// Panics only if the implementation itself panics.
39    ///
40    /// **日本語:** このイベントを消費せずに変換します。
41    ///
42    /// 成功時は [`Self::Output`] を返し、イベントを変換できない場合は [`Self::Error`] を返します。
43    /// パニックするのは実装自体がパニックする場合だけです。
44    fn convert(&self) -> Result<Self::Output, Self::Error>;
45}