Skip to main content

ddd_toolkit_core/
lib.rs

1//! **English:**<br>
2//! Core traits for modeling Domain-Driven Design entities and value objects.
3//!
4//! [`Entity`] exposes a stable identifier by reference, while [`ValueObject`] exposes
5//! a wrapped value by reference or by value. Implement these traits on domain types to keep their
6//! domain-facing APIs independent from persistence details.
7//!
8//! For event-sourced aggregates, [`EventStore`] loads complete [`StreamHistory`] values
9//! and appends events using [`ExpectedRevision`]. [`AggregateReplay`] supplies reconstruction hooks
10//! for [`replay_aggregate`] and [`replay_with_optional_snapshot`]. [`SnapshotStore`] persists
11//! [`AggregateSnapshot`] values separately; adapters define storage and concurrency behavior.
12//!
13//! [`Secret`] wraps a value implementing `zeroize::Zeroize`, redacts its formatted
14//! output, and zeroizes it on drop. Access to the unredacted value is explicit; extracting it
15//! transfers responsibility for zeroization to the caller. [`SecretMetadata`] is a zero-sized
16//! marker and does not itself protect data.
17//!
18//! [`AsyncEventStore`] and [`AsyncSnapshotStore`] expose boxed `Send` futures for
19//! event and snapshot persistence. [`EventConsumer`] and [`Projection`] separate event handling
20//! from read-model updates, with asynchronous counterparts [`AsyncEventConsumer`] and
21//! [`AsyncProjection`]. [`DomainEventDeliveryMetadata`] optionally exposes [`DeliveryMetadata`].
22//! [`TransactionalOutbox`] separates save-and-enqueue, claim, and acknowledgement operations;
23//! adapters supply transactions and delivery policies, not this crate.
24//!
25//! [`TransactionalOutboxLifecycleContext`] tracks a single save-and-enqueue attempt.
26//! It pulls pending events without restoring them on failure, rejects repeated attempts, and
27//! reports missing or unsuccessful enqueueing at completion. It does not implement a transaction.
28//!
29//! [`AsyncTransactionalOutbox`] provides future-based outbox operations.
30//! [`AsyncTransactionalOutboxLifecycleContext`] consumes an attempt and pulls events before polling;
31//! dropping its future does not restore events. [`ProjectionCheckpoint`] carries application-defined
32//! progress for [`ProjectionCheckpointStore`], while [`IdempotentProjection`] reports applied or
33//! duplicate input via [`ProjectionApplication`]. Implementations supply duplicate detection and
34//! coordination between checkpoint persistence and read-model changes.
35//!
36//! [`AsyncProjectionCheckpointStore`] persists progress through boxed `Send` futures,
37//! and [`AsyncIdempotentProjection`] asynchronously distinguishes new application from duplicates.
38//! These independent ports do not coordinate transactions; dropping a future does not guarantee
39//! rollback or absence of side effects. Implementations define cancellation and retry semantics.
40//!
41//! [`Specification`] composes fallible predicates with short-circuiting [`And`],
42//! [`Or`], and [`Not`]. [`DomainService`], [`Policy`], and [`DomainFactory`] define typed operation,
43//! rule-checking, and construction boundaries without prescribing persistence or thread safety.
44//! [`ProcessManager`] returns a [`Transition`] of state and effect descriptions for callers to
45//! store and execute. [`UnitOfWork`] exposes explicit registration, commit, and rollback ports;
46//! implementations define their transaction guarantees. [`EventIdentity`] and [`VersionedEvent`]
47//! describe event schemas, while [`UpcasterChain`] applies [`EventUpcaster`] stages in insertion
48//! order without automatically selecting versions.
49//!
50//! **日本語:**<br>
51//! ドメイン駆動設計のエンティティと値オブジェクトを表現するためのコアトレイトを提供します。
52//!
53//! [`Entity`] は安定した識別子を参照で公開し、[`ValueObject`] は内包値を参照または
54//! 値で公開します。永続化の詳細から独立したドメイン型の API を保つために、これらのトレイトを実装します。
55//!
56//! イベントソーシングを使う集約では、[`EventStore`] が完全な [`StreamHistory`] を
57//! 読み込み、[`ExpectedRevision`] を条件にイベントを追加します。[`AggregateReplay`] は
58//! [`replay_aggregate`] と [`replay_with_optional_snapshot`] の復元フックを定めます。
59//! [`SnapshotStore`] は [`AggregateSnapshot`] を別途永続化し、保存や並行処理の挙動は
60//! アダプターが定めます。
61//!
62//! [`Secret`] は `zeroize::Zeroize` を実装する値を包み、書式出力を秘匿化し、
63//! 破棄時にゼロ化します。秘匿化していない値には明示的にアクセスし、値を取り出した後の
64//! ゼロ化は呼び出し側が管理します。[`SecretMetadata`] はサイズがゼロのマーカーであり、
65//! それ自体はデータを保護しません。
66//!
67//! [`AsyncEventStore`] と [`AsyncSnapshotStore`] はイベントとスナップショットの
68//! 永続化を、ボックス化された `Send` Future で公開します。[`EventConsumer`] と [`Projection`]
69//! はイベント処理と読み取りモデル更新を分け、非同期版の [`AsyncEventConsumer`] と
70//! [`AsyncProjection`] も提供します。[`DomainEventDeliveryMetadata`] は任意で
71//! [`DeliveryMetadata`] を公開します。[`TransactionalOutbox`] は保存・登録、取得、確認応答を
72//! 分離します。トランザクションと配信方針はこのクレートではなくアダプターが提供します。
73//!
74//! [`TransactionalOutboxLifecycleContext`] は保存・登録の一回の試行を追跡します。
75//! 保留イベントを取り出して失敗時も復元せず、繰り返しの試行を拒否し、完了確認時に
76//! 未試行や未成功を報告します。トランザクション自体は実装しません。
77//!
78//! [`AsyncTransactionalOutbox`] は Future によるアウトボックス操作を提供します。
79//! [`AsyncTransactionalOutboxLifecycleContext`] はポーリング前に試行を消費してイベントを取り出し、
80//! Future の破棄でも復元しません。[`ProjectionCheckpoint`] はアプリケーション定義の進捗を
81//! [`ProjectionCheckpointStore`] に渡し、[`IdempotentProjection`] は適用または重複を
82//! [`ProjectionApplication`] で報告します。重複検出と、チェックポイント永続化・読み取りモデル変更の
83//! 連携は実装が提供します。
84//!
85//! [`AsyncProjectionCheckpointStore`] はボックス化された `Send` Future で進捗を永続化し、
86//! [`AsyncIdempotentProjection`] は新規適用と重複を非同期に区別します。これらの独立したポートは
87//! トランザクションを調整せず、Future の破棄はロールバックや副作用の不在を保証しません。
88//! キャンセルと再試行の意味は実装が定めます。
89//!
90//! [`Specification`] は失敗し得る述語を、短絡評価する [`And`]、[`Or`]、[`Not`] で
91//! 結合します。[`DomainService`]、[`Policy`]、[`DomainFactory`] は永続化やスレッド安全性を
92//! 規定せず、型付きの操作、規則確認、構築の境界を定めます。[`ProcessManager`] は状態と
93//! 副作用の記述を [`Transition`] で返し、保存と実行は呼び出し側が担います。[`UnitOfWork`] は
94//! 明示的な登録、コミット、ロールバックのポートであり、トランザクションの保証は実装が定めます。
95//! [`EventIdentity`] と [`VersionedEvent`] はイベントのスキーマを表し、[`UpcasterChain`] は
96//! バージョンを自動選択せず、[`EventUpcaster`] の段階を追加順に適用します。
97
98mod aggregate_root;
99mod async_dispatch;
100mod async_domain_event_publisher;
101mod async_event_consumption;
102mod async_event_store;
103mod async_outbox_lifecycle;
104mod async_repository;
105mod async_snapshot;
106mod async_transactional_outbox;
107mod cqrs;
108mod dispatch;
109mod domain_event;
110mod domain_event_conversion;
111mod domain_event_metadata;
112mod domain_event_publisher;
113mod domain_factory;
114mod domain_service;
115mod entity;
116mod event_consumption;
117mod event_delivery_metadata;
118mod event_store;
119mod event_versioning;
120mod outbox_lifecycle;
121mod process_manager;
122mod projection_reliability;
123mod replay;
124mod repository;
125mod secret;
126mod secret_metadata;
127mod snapshot;
128mod specification;
129mod transactional_outbox;
130mod unit_of_work;
131mod value_object;
132
133pub use aggregate_root::AggregateRoot;
134
135pub use async_dispatch::dispatch_events_async;
136
137pub use async_domain_event_publisher::AsyncDomainEventPublisher;
138
139pub use async_event_consumption::{AsyncEventConsumer, AsyncProjection};
140
141pub use async_event_store::AsyncEventStore;
142
143pub use async_outbox_lifecycle::{
144    AsyncOutboxLifecycleCompletionError, AsyncOutboxLifecycleContext,
145    AsyncOutboxLifecycleOperationError, AsyncTransactionalOutboxCompletionError,
146    AsyncTransactionalOutboxLifecycleContext, AsyncTransactionalOutboxOperationError,
147};
148
149pub use async_repository::AsyncRepository;
150
151pub use async_snapshot::AsyncSnapshotStore;
152
153pub use async_transactional_outbox::AsyncTransactionalOutbox;
154
155pub use cqrs::{
156    AggregateCreator, AsyncAggregateCreator, AsyncCommandHandler, AsyncLifecycleCallback,
157    AsyncLifecycleContext, AsyncQueryHandler, CommandHandler, LifecycleCallback,
158    LifecycleCompletionError, LifecycleContext, LifecycleOperationError, QueryHandler,
159};
160
161pub use dispatch::dispatch_events;
162
163pub use domain_event::DomainEvent;
164
165pub use domain_event_conversion::DomainEventConversion;
166
167pub use domain_event_metadata::DomainEventMetadata;
168
169pub use domain_event_publisher::DomainEventPublisher;
170
171pub use domain_factory::DomainFactory;
172
173pub use domain_service::{DomainService, Policy};
174
175pub use entity::Entity;
176
177pub use event_consumption::{EventConsumer, Projection};
178
179pub use event_delivery_metadata::{DeliveryMetadata, DomainEventDeliveryMetadata};
180
181pub use event_store::{
182    AppendError, AppendResult, CurrentRevision, EventStore, ExpectedRevision, LoadResult, NoStream,
183    RevisionConflict, StreamHistory, StreamHistoryError, StreamRevision,
184};
185
186pub use event_versioning::{EventIdentity, EventUpcaster, UpcasterChain, VersionedEvent};
187
188pub use outbox_lifecycle::{
189    OutboxLifecycleCompletionError, OutboxLifecycleContext, OutboxLifecycleOperationError,
190    TransactionalOutboxCompletionError, TransactionalOutboxLifecycleContext,
191    TransactionalOutboxOperationError,
192};
193
194pub use process_manager::{ProcessManager, Transition};
195
196pub use projection_reliability::{
197    AsyncIdempotentProjection, AsyncProjectionCheckpointStore, IdempotentProjection,
198    ProjectionApplication, ProjectionCheckpoint, ProjectionCheckpointStore,
199};
200
201pub use replay::{
202    AggregateReplay, RehydratedAggregate, ReplayError, SnapshotReplayError, replay_aggregate,
203    replay_with_optional_snapshot,
204};
205
206pub use repository::Repository;
207
208pub use secret::Secret;
209
210pub use secret_metadata::SecretMetadata;
211
212pub use snapshot::{
213    AggregateSnapshot, SnapshotLoadError, SnapshotSaveError, SnapshotStore, SnapshotUse,
214};
215
216pub use specification::{And, Not, Or, Specification};
217
218pub use transactional_outbox::{OutboxClaim, OutboxMessageId, TransactionalOutbox};
219
220pub use unit_of_work::UnitOfWork;
221
222pub use value_object::ValueObject;
223
224#[cfg(feature = "serde")]
225#[doc(hidden)]
226/// **English:**<br>
227/// Internal access to the optional `serde` dependency for generated implementations.
228///
229/// **日本語:**<br>
230/// 生成された実装から任意依存の `serde` にアクセスするための内部モジュールです。
231pub mod __private {
232    /// **English:**<br>
233    /// Re-exports the optional `serde` crate for macro-generated code.
234    ///
235    /// **日本語:**<br>
236    /// マクロが生成したコードで使うために、任意依存の `serde` クレートを再エクスポートします。
237    pub use serde;
238}