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