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