doublets/decorators/mod.rs
1//! Decorators that add policy and transactions to any [`Doublets`](crate::Doublets) store.
2//!
3//! This module is a port of the decorator layer of `Platform.Data.Doublets`. Each C#
4//! decorator has a counterpart here with the same logic, but composition is static: a
5//! decorator is a generic struct that owns the store it wraps, every forwarding method is
6//! `#[inline]`, and the policy markers are zero-sized. A chosen stack is one concrete
7//! type, so the optimiser can fuse the whole stack into a single `create` / `update` /
8//! `delete` / `each` with no per-layer call.
9//!
10//! # Building a stack
11//!
12//! [`DecoratorsExt`] takes the store by value and returns the concrete composed type:
13//!
14//! ```rust
15//! use doublets::{decorators::{DecoratorsExt, Resolve}, mem, unit, Doublets};
16//!
17//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
18//! let mut store = unit::Store::<usize, _>::new(mem::Global::new())?
19//! .with_uniqueness(Resolve)
20//! .with_usages_validation();
21//!
22//! let a = store.create_point()?;
23//! let b = store.create_point()?;
24//! assert_eq!(store.create_link(a, b)?, store.create_link(a, b)?);
25//! # Ok(())
26//! # }
27//! ```
28//!
29//! # The decorators
30//!
31//! | C# decorator | This module | Create | Update | Delete | Each |
32//! |---|---|:--:|:--:|:--:|:--:|
33//! | `LinksUniquenessValidator` | [`UniquenessValidator`] | | ● | | |
34//! | `LinksUniquenessResolver` | [`UniquenessResolver`] | | ● | | |
35//! | `LinksCascadeUniquenessAndUsagesResolver` | [`CascadeUniquenessAndUsagesResolver`] | | ● | ● | |
36//! | `LinksUsagesValidator` | [`UsagesValidator`] | | ● | ● | |
37//! | `LinksCascadeUsagesResolver` | [`CascadeUsagesResolver`] | | | ● | |
38//! | `LinksInnerReferenceExistenceValidator` | [`InnerReferenceExistenceValidator`] | | ● | ● | ● |
39//! | `LinksItselfConstantToSelfReferenceResolver` | [`ItselfConstantToSelfReferenceResolver`] | | ● | | ● |
40//! | `LinksNullConstantToSelfReferenceResolver` | [`NullConstantToSelfReferenceResolver`] | ● | ● | | |
41//! | `LinksNonExistentDependenciesCreator` | [`NonExistentDependenciesCreator`] | | ● | | |
42//! | `LinksNonExistentDeletionResolver` | [`NonExistentDeletionResolver`] | | | ● | |
43//! | `NonNullContentsLinkDeletionResolver` | [`NonNullContentsLinkDeletionResolver`] | | | ● | |
44//! | `LoggingDecorator` | [`LoggingDecorator`] | ● | ● | ● | |
45//! | `NoExceptionsDecorator` | [`NoExceptionsDecorator`] | ● | ● | ● | ● |
46//! | `UInt64LinksTransactionsLayer` | [`TransactionsDecorator`] | ● | ● | ● | |
47//!
48//! Unlike the stateless policy wrappers, transactions keep a journal and graph
49//! snapshots. See [`transactions`] for their durability, ordering and cost.
50//!
51//! # Ordering
52//!
53//! C# propagates a `_facade` reference down the stack so that a decorator's recursive
54//! calls — a cascading delete, a usage merge — re-enter at the *top* of the stack. A
55//! statically composed stack has no such back-reference: a decorator only knows the
56//! layers below it, so recursive calls re-enter at the layer that made them.
57//!
58//! Put a decorator whose behaviour the cascade must observe *below* the decorator that
59//! cascades. The automatic uniqueness and usages resolution stack keeps C#'s order,
60//! with [`CascadeUsagesResolver`] below [`NonNullContentsLinkDeletionResolver`]. To
61//! preserve its callbacks without a facade back-reference, the cascade explicitly resets
62//! each usage to `(null, null)` before recursing into its usages, then deletes it after
63//! those usages are gone. The outer reset layer handles the originally requested link.
64//!
65//! # Deviations from C\#
66//!
67//! Each is documented on the item it affects; in summary:
68//!
69//! - [`UniquenessResolver`] reports the surviving link to the handler, so
70//! [`create_link`](crate::Doublets::create_link) returns a valid address instead of the
71//! address of the link it just deleted.
72//! - [`CascadeUsagesResolver`] tracks the links it has already visited, so a reference
73//! cycle terminates instead of overflowing the stack.
74//! - `MergeUsages` re-points sources and targets correctly; the C# version writes null
75//! targets because of a `params` constructor mix-up.
76//! - `EnsureCreated` actually calls the creator in its loop; the C# loop never terminates.
77//! - [`InnerReferenceExistenceValidator::try_each_links`] exists because
78//! [`Links::each_links`](crate::Links::each_links) has no error channel.
79//! - [`NoExceptionsDecorator`] maps `Err(_)` to [`Flow::Break`](data::Flow::Break), the
80//! closest thing Rust's [`Flow`](data::Flow) has to C#'s `Error` constant. It does not
81//! catch panics.
82//! - [`LoggingDecorator`] surfaces the first I/O error from the log writer instead of
83//! discarding it.
84
85mod macros;
86
87mod builder;
88mod constants;
89mod deletion;
90mod existence;
91mod helpers;
92mod logging;
93mod mutation;
94mod no_exceptions;
95mod policy;
96pub mod transactions;
97mod uniqueness;
98mod usages;
99
100pub use builder::{AutomaticUniquenessAndUsagesResolution, DecoratorsExt};
101pub use constants::{ItselfConstantToSelfReferenceResolver, NullConstantToSelfReferenceResolver};
102pub use deletion::{CheckedRelease, NonNullContentsLinkDeletionResolver};
103pub use existence::{
104 InnerReferenceExistenceValidator, NonExistentDeletionResolver, NonExistentDependenciesCreator,
105};
106pub use logging::LoggingDecorator;
107pub use mutation::CheckedMutation;
108pub use no_exceptions::NoExceptionsDecorator;
109pub use policy::{CascadeResolve, Resolve, UniquenessPolicy, UsagesPolicy, Validate};
110pub use transactions::{
111 FileTransitionLog, JournalEntry, MemoryTransitionLog, Transaction, TransactionsDecorator,
112 Transition, TransitionKind, TransitionLog,
113};
114pub use uniqueness::{CascadeUniquenessAndUsagesResolver, UniquenessResolver, UniquenessValidator};
115pub use usages::{CascadeUsagesResolver, UsagesValidator};