ddd_toolkit_core/async_snapshot.rs
1#![allow(clippy::type_complexity)]
2
3use crate::{AggregateRoot, AggregateSnapshot, Entity, SnapshotLoadError, SnapshotSaveError};
4use std::future::Future;
5use std::pin::Pin;
6
7/// **English:** Asynchronous storage for snapshots, separate from event-stream appends.
8///
9/// Futures are boxed and `Send`, borrowing the store and identifier for `'a`.
10/// Adapters define replacement, durability, concurrency, panic conditions, and cancellation
11/// outcomes. This port does not verify snapshots against an event stream or provide an executor.
12///
13/// **日本語:** イベントストリームへの追加とは別にスナップショットを非同期保存します。
14///
15/// Future はボックス化された `Send` で、ストアと識別子を `'a` の間借用します。
16/// 置換、永続性、並行処理、パニック条件、キャンセルの結果はアダプターが定めます。
17/// このポートはストリームに対するスナップショット検証や実行器を提供しません。
18pub trait AsyncSnapshotStore
19where
20 Self::State: Send,
21 Self::Error: Send,
22 <Self::Aggregate as Entity>::Id: Sync,
23{
24 /// **English:** Aggregate defining the snapshot identifier, whose type must be `Sync`.
25 ///
26 /// **日本語:** スナップショット識別子を定める集約です。識別子の型には `Sync` が必要です。
27 type Aggregate: AggregateRoot + Send;
28
29 /// **English:** Owned snapshot state transferable across threads.
30 ///
31 /// **日本語:** スレッド間で受け渡せる所有されたスナップショット状態です。
32 type State: Send;
33
34 /// **English:** Adapter failure carried by snapshot load and save errors.
35 ///
36 /// **日本語:** スナップショットの読み込み・保存エラーが保持するアダプター障害です。
37 type Error: Send;
38
39 /// **English:** Returns a future loading the snapshot for `id`; `Ok(None)` means absence.
40 /// Storage failures are reported through [`SnapshotLoadError::Adapter`].
41 ///
42 /// **日本語:** `id` のスナップショットを読み込む Future を返し、`Ok(None)` は不在を表します。
43 /// ストレージ障害は [`SnapshotLoadError::Adapter`] で報告します。
44 fn load<'a>(
45 &'a self,
46 id: &'a <Self::Aggregate as Entity>::Id,
47 ) -> Pin<
48 Box<
49 dyn Future<
50 Output = Result<
51 Option<AggregateSnapshot<Self::State>>,
52 SnapshotLoadError<Self::Error>,
53 >,
54 > + Send
55 + 'a,
56 >,
57 >;
58
59 /// **English:** Takes ownership of `snapshot` and returns a future saving it for `id`.
60 /// Completion is `Ok(())` or [`SnapshotSaveError::Adapter`]; the snapshot is not returned
61 /// on failure. Neither an error nor cancellation specifies whether storage changed.
62 ///
63 /// **日本語:** `snapshot` の所有権を受け取り、`id` に対して保存する Future を返します。
64 /// 結果は `Ok(())` または [`SnapshotSaveError::Adapter`] で、失敗時に値は返されません。
65 /// エラーやキャンセルだけではストレージが変更されたかどうかは決まりません。
66 fn save<'a>(
67 &'a mut self,
68 id: &'a <Self::Aggregate as Entity>::Id,
69 snapshot: AggregateSnapshot<Self::State>,
70 ) -> Pin<Box<dyn Future<Output = Result<(), SnapshotSaveError<Self::Error>>> + Send + 'a>>;
71}