doublets/decorators/builder.rs
1//! The builder that composes decorators into a single concrete type.
2
3use std::io::Write;
4
5use data::LinkReference;
6
7use super::{
8 policy::{UniquenessPolicy, UsagesPolicy},
9 CascadeUniquenessAndUsagesResolver, CascadeUsagesResolver, InnerReferenceExistenceValidator,
10 ItselfConstantToSelfReferenceResolver, LoggingDecorator, NoExceptionsDecorator,
11 NonExistentDeletionResolver, NonExistentDependenciesCreator,
12 NonNullContentsLinkDeletionResolver, NullConstantToSelfReferenceResolver,
13 TransactionsDecorator, TransitionLog, UsagesValidator,
14};
15use crate::Doublets;
16
17/// The stack C# `ILinksExtensions.DecorateWithAutomaticUniquenessAndUsagesResolution`
18/// builds, spelled as a type.
19pub type AutomaticUniquenessAndUsagesResolution<T, L> = CascadeUniquenessAndUsagesResolver<
20 T,
21 NonNullContentsLinkDeletionResolver<T, CascadeUsagesResolver<T, L>>,
22>;
23
24/// Composes decorators around a store.
25///
26/// Every method takes the store by value and returns the *concrete* composed type, so a
27/// stack is built at compile time and every layer is a candidate for inlining:
28///
29/// ```rust
30/// use doublets::{decorators::{DecoratorsExt, Resolve}, mem, unit, Doublets};
31///
32/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
33/// let mut store = unit::Store::<usize, _>::new(mem::Global::new())?
34/// .with_uniqueness(Resolve)
35/// .with_usages_validation();
36///
37/// let a = store.create_point()?;
38/// let b = store.create_point()?;
39/// // The second `create_link` reuses the first one instead of storing a duplicate.
40/// assert_eq!(store.create_link(a, b)?, store.create_link(a, b)?);
41/// # Ok(())
42/// # }
43/// ```
44pub trait DecoratorsExt<T: LinkReference>: Doublets<T> + Sized {
45 /// Wraps the store in [`TransactionsDecorator`] and recovers its journal.
46 fn with_transactions<J: TransitionLog<T>>(
47 self,
48 journal: J,
49 ) -> Result<TransactionsDecorator<T, Self, J>, crate::Error<T>> {
50 TransactionsDecorator::new(self, journal)
51 }
52
53 /// Applies a [`UniquenessPolicy`] — [`Validate`](super::Validate),
54 /// [`Resolve`](super::Resolve) or [`CascadeResolve`](super::CascadeResolve).
55 #[inline]
56 fn with_uniqueness<P: UniquenessPolicy>(self, _policy: P) -> P::Decorator<T, Self> {
57 P::decorate(self)
58 }
59
60 /// Applies a [`UsagesPolicy`] — [`Validate`](super::Validate) or
61 /// [`CascadeResolve`](super::CascadeResolve).
62 #[inline]
63 fn with_usages<P: UsagesPolicy>(self, _policy: P) -> P::Decorator<T, Self> {
64 P::decorate(self)
65 }
66
67 /// Wraps in [`UsagesValidator`].
68 #[inline]
69 fn with_usages_validation(self) -> UsagesValidator<T, Self> {
70 UsagesValidator::new(self)
71 }
72
73 /// Wraps in [`CascadeUsagesResolver`].
74 #[inline]
75 fn with_cascade_usages_resolution(self) -> CascadeUsagesResolver<T, Self> {
76 CascadeUsagesResolver::new(self)
77 }
78
79 /// Wraps in [`InnerReferenceExistenceValidator`].
80 #[inline]
81 fn with_inner_reference_existence_validation(
82 self,
83 ) -> InnerReferenceExistenceValidator<T, Self> {
84 InnerReferenceExistenceValidator::new(self)
85 }
86
87 /// Wraps in [`NonExistentDependenciesCreator`].
88 #[inline]
89 fn with_non_existent_dependencies_creation(self) -> NonExistentDependenciesCreator<T, Self> {
90 NonExistentDependenciesCreator::new(self)
91 }
92
93 /// Wraps in [`NonExistentDeletionResolver`], ignoring deletion of missing links.
94 #[inline]
95 fn with_non_existent_deletion_resolution(self) -> NonExistentDeletionResolver<T, Self> {
96 NonExistentDeletionResolver::new(self)
97 }
98
99 /// Wraps in [`ItselfConstantToSelfReferenceResolver`].
100 #[inline]
101 fn with_itself_constant_resolution(self) -> ItselfConstantToSelfReferenceResolver<T, Self> {
102 ItselfConstantToSelfReferenceResolver::new(self)
103 }
104
105 /// Wraps in [`NullConstantToSelfReferenceResolver`].
106 #[inline]
107 fn with_null_constant_resolution(self) -> NullConstantToSelfReferenceResolver<T, Self> {
108 NullConstantToSelfReferenceResolver::new(self)
109 }
110
111 /// Wraps in [`NonNullContentsLinkDeletionResolver`].
112 #[inline]
113 fn with_non_null_contents_deletion_resolution(
114 self,
115 ) -> NonNullContentsLinkDeletionResolver<T, Self> {
116 NonNullContentsLinkDeletionResolver::new(self)
117 }
118
119 /// Wraps in [`LoggingDecorator`], logging every mutation to `writer`.
120 #[inline]
121 fn with_logging<W: Write + Send + Sync>(self, writer: W) -> LoggingDecorator<T, Self, W> {
122 LoggingDecorator::new(self, writer)
123 }
124
125 /// Wraps in [`NoExceptionsDecorator`].
126 #[inline]
127 fn with_no_exceptions(self) -> NoExceptionsDecorator<T, Self> {
128 NoExceptionsDecorator::new(self)
129 }
130
131 /// Builds the same stack as C#
132 /// `ILinksExtensions.DecorateWithAutomaticUniquenessAndUsagesResolution`:
133 /// [`CascadeUsagesResolver`] innermost, then [`NonNullContentsLinkDeletionResolver`],
134 /// then [`CascadeUniquenessAndUsagesResolver`] outermost.
135 /// The cascade explicitly resets each nested usage before descending, so handlers
136 /// observe resets before the corresponding deletions, as with C#'s facade recursion.
137 ///
138 /// The stack does not check that an updated address exists: an update of a missing
139 /// link to an existing pair reports that link and changes nothing. Add
140 /// [`with_inner_reference_existence_validation`](Self::with_inner_reference_existence_validation)
141 /// *after* this call to get `Error::NotExists` instead; see
142 /// [`CascadeUniquenessAndUsagesResolver`] and `docs/mutation.md`.
143 #[inline]
144 fn with_automatic_uniqueness_and_usages_resolution(
145 self,
146 ) -> AutomaticUniquenessAndUsagesResolution<T, Self> {
147 CascadeUniquenessAndUsagesResolver::new(NonNullContentsLinkDeletionResolver::new(
148 CascadeUsagesResolver::new(self),
149 ))
150 }
151}
152
153impl<T: LinkReference, L: Doublets<T>> DecoratorsExt<T> for L {}