1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Phantom-typed phase markers for the directive-processing pipeline.
//!
//! The loader runs directives through a strict sequence of phases:
//!
//! ```text
//! Raw → Sorted → Synthed → EarlyValidated → Booked
//! → RegularPluginsApplied → LateValidated → Finalized
//! ```
//!
//! Phase ordering was previously enforced by code organization and
//! inline comments in `process.rs`. This module makes the ordering
//! a property of the type system: each phase transition consumes a
//! [`Directives<P>`] of one phase and produces one of the next phase
//! only. A refactor that drops a phase, swaps two phases, or calls a
//! later phase on raw input produces a type error rather than silent
//! misbehavior. See issue #1166.
//!
//! ## Phase definitions
//!
//! | Phase | Invariant after this phase |
//! |---|---|
//! | [`Raw`] | Straight from the parser. No ordering / synth / booking guarantees. |
//! | [`Sorted`] | Sorted into canonical display order `(date, priority, file_id, span.start)`. |
//! | [`Synthed`] | Synth-only plugins (`auto_accounts`, `document_discovery`) applied. |
//! | [`EarlyValidated`] | Early-phase validators ran. Account presence / lifecycle / structural errors collected. |
//! | [`Booked`] | Cost-spec interpolation done. Failed transactions partitioned out. |
//! | [`RegularPluginsApplied`] | Post-booking plugins (cost-reading) applied to the successfully-booked directives. |
//! | [`LateValidated`] | Late-phase validators ran on booked + plugin-processed directives. |
//! | [`Finalized`] | Failed transactions re-merged + re-sorted into the final display order. |
//!
//! ## Why a phantom rather than separate Vec types?
//!
//! The underlying payload is `Vec<Spanned<Directive>>` in every phase
//! — only the *invariants* differ, not the layout. Phantom-data
//! markers carry the phase at the type level without changing the
//! runtime representation. rkyv cache compatibility is preserved
//! (the wrapper is zero-sized in memory).
//!
//! ## Booking partition note
//!
//! `Directives<Booked>` carries only the successfully-booked
//! transactions. Failed ones are returned in a `FailedBookings`
//! newtype (an internal `pub(crate)` wrapper around
//! `Vec<Spanned<Directive>>`) and re-merged at [`Finalized`] (see
//! the `book` and `finalize` transitions in `process.rs`). The
//! newtype gives the out-of-band channel a name and a type — the
//! `finalize` call can't accidentally receive an arbitrary
//! `Vec<Spanned<Directive>>` (e.g. a freshly-parsed one). The
//! phantom-typed `Directives<P>` can't express "this Vec holds a
//! mix of stages," which is why the failed branch travels alongside
//! the main pipeline rather than as another phase.
//!
//! ## Open design choices documented in #1166
//!
//! - **Error state is NOT carried in the phase type.** Phase tracks
//! ordering; errors accumulate in a separate `Vec<LedgerError>`
//! passed through the pipeline. Including the error state in the
//! phase would explode the variant count and impede the chain.
//! - **Only [`Finalized`] is exposed publicly.** Pipeline methods
//! are `pub(crate)`; downstream consumers can't accidentally hold
//! a partially-processed `Directives` because the only escape
//! hatch is `Directives<Finalized>::into_inner()`.
//! - **Plugin trait is NOT stage-parameterized in this PR.** Plugins
//! continue to use the `PluginPass` enum to discriminate
//! synth-vs-regular. Making the trait phase-aware is a follow-up;
//! the current approach catches the call-site error (calling the
//! wrong phase function) without restructuring the plugin API.
use PhantomData;
use Directive;
use Spanned;
/// Marker trait for pipeline phases. Sealed: only the markers in
/// this module implement it, so downstream crates can't invent new
/// phases (which would defeat the type-driven ordering).
define_phase!;
define_phase!;
define_phase!;
define_phase!;
define_phase!;
define_phase!;
define_phase!;
define_phase!;
/// A directive collection at a specific pipeline phase.
///
/// The phase is a phantom marker — the runtime representation is the
/// same `Vec<Spanned<Directive>>` regardless of `P`. Transitions
/// between phases are the only way to advance: see the `impl`
/// blocks in `process.rs` for each phase's allowed next step.
///
/// Constructed only via [`Directives::from_parser`] (which produces
/// [`Directives<Raw>`]). Subsequent phases are reached by calling
/// the relevant transition methods in order.
/// Transactions that failed booking, partitioned out of the main
/// pipeline by the `book` transition on [`Directives<EarlyValidated>`]
/// and re-merged at the `finalize` transition on
/// [`Directives<LateValidated>`].
///
/// Effectively `pub(crate)`: the only producer (`book`) and consumer
/// (`finalize`) are both crate-internal, and the type lives in the
/// private `mod phase` of `rustledger-loader`, so external callers
/// can't get a value of this type. Kept as a named newtype rather
/// than a bare `Vec` so `finalize` can't accidentally receive an
/// arbitrary directive list at the call site. The contents are
/// pre-booking shape: unresolved cost specs, unfilled elided slots,
/// possibly unbalanced.