Skip to main content

ddd_toolkit_core/
process_manager.rs

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