ddd_toolkit_core/process_manager.rs
1/// **English:**<br>
2/// Owned next state and ordered effect descriptions returned by a [`ProcessManager`].
3/// Construction and access do not execute effects or persist the state. Empty effects are valid;
4/// callers choose how to coordinate state storage and effect execution.
5///
6/// Inspect the transition before taking ownership of both components.
7///
8/// **日本語:**<br>
9/// [`ProcessManager`] が返す、所有された次状態と順序付きの副作用の記述です。
10/// 構築や参照で副作用を実行したり状態を永続化したりしません。空の副作用列も許容し、
11/// 状態保存と副作用実行の連携方法は呼び出し側が決めます。
12///
13/// 遷移を参照してから、両方の構成要素の所有権を取り出します。
14///
15/// # Example / 使用例
16///
17/// ```rust
18/// use ddd_toolkit_core::Transition;
19///
20/// let transition = Transition::new("paid", vec!["send receipt"]);
21/// assert_eq!(transition.state(), &"paid");
22/// assert_eq!(transition.effects(), &["send receipt"]);
23/// let (state, effects) = transition.into_parts();
24/// assert_eq!((state, effects), ("paid", vec!["send receipt"]));
25/// ```
26pub struct Transition<State, Effect> {
27 state: State,
28 effects: Vec<Effect>,
29}
30
31impl<State, Effect> Transition<State, Effect> {
32 /// **English:**<br>
33 /// Stores `state` and `effects` unchanged, preserving the vector's order without validation.
34 ///
35 /// **日本語:**<br>
36 /// 検証せず、ベクターの順序を保ったまま `state` と `effects` を保持します。
37 pub fn new(state: State, effects: Vec<Effect>) -> Self {
38 Self { state, effects }
39 }
40
41 /// **English:**<br>
42 /// Borrows the next state without applying it to any external store.
43 ///
44 /// **日本語:**<br>
45 /// 外部ストアへの反映を行わず、次状態を借用します。
46 pub fn state(&self) -> &State {
47 &self.state
48 }
49
50 /// **English:**<br>
51 /// Borrows the effect descriptions in their stored order without executing them.
52 ///
53 /// **日本語:**<br>
54 /// 実行せず、保持している順序で副作用の記述を借用します。
55 pub fn effects(&self) -> &[Effect] {
56 &self.effects
57 }
58
59 /// **English:**<br>
60 /// Consumes the transition to return its state and effect vector without cloning.
61 ///
62 /// **日本語:**<br>
63 /// 遷移を消費し、複製せずに状態と副作用のベクターを返します。
64 pub fn into_parts(self) -> (State, Vec<Effect>) {
65 (self.state, self.effects)
66 }
67}
68
69/// **English:**<br>
70/// Computes a next state and effect descriptions from borrowed state and event values.
71/// The caller owns persistence, event delivery, and effect execution. The signature does not enforce
72/// determinism, reentrancy, idempotency, or purity; shared access can still use interior mutability.
73/// Thread-safety bounds and panic behavior are left to implementations.
74///
75/// **日本語:**<br>
76/// 借用した状態とイベントから、次状態と副作用の記述を算出します。
77/// 永続化、イベント配信、副作用の実行は呼び出し側が担います。このシグネチャは決定性、再入可能性、
78/// 冪等性、純粋性を強制せず、共有参照でも内部可変性を使えます。
79/// スレッド安全性の境界とパニックの条件は実装が定めます。
80pub trait ProcessManager {
81 /// **English:**<br>
82 /// Process state borrowed as input and owned by the resulting transition.
83 ///
84 /// **日本語:**<br>
85 /// 入力として借用し、結果の遷移が所有するプロセス状態です。
86 type State;
87
88 /// **English:**<br>
89 /// Event type interpreted by this manager; no event trait bound is required.
90 ///
91 /// **日本語:**<br>
92 /// このマネージャーが解釈するイベント型です。イベントトレイトの境界は要求しません。
93 type Event;
94
95 /// **English:**<br>
96 /// Description of an action for the caller to execute after handling.
97 ///
98 /// **日本語:**<br>
99 /// 処理後に呼び出し側が実行する動作の記述です。
100 type Effect;
101
102 /// **English:**<br>
103 /// Handling failure returned instead of a transition.
104 ///
105 /// **日本語:**<br>
106 /// 遷移の代わりに返す処理の失敗です。
107 type Error;
108
109 /// **English:**<br>
110 /// Evaluates `event` against `state`, returning a transition or a handling error.
111 /// Neither input is consumed. On error no transition is returned, but the trait does not
112 /// undo implementation side effects or guarantee that no side effects occurred.
113 ///
114 /// **日本語:**<br>
115 /// `state` に対して `event` を評価し、遷移または処理エラーを返します。
116 /// どちらの入力も消費しません。エラー時は遷移を返しませんが、実装の副作用を取り消したり、
117 /// 副作用が発生していないと保証したりはしません。
118 fn handle(
119 &self,
120 state: &Self::State,
121 event: &Self::Event,
122 ) -> Result<Transition<Self::State, Self::Effect>, Self::Error>;
123}