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
//! `Projection` — consumer-implemented live read-model contract (D-06).
//!
//! **Not to be confused with `ferro-projections` (plural).** That crate
//! is the Service Projection abstraction (`ServiceDef → IntentGraph →
//! JsonUiRenderer`). This trait is the live-read-model contract: fold
//! domain events into a per-key state, return a delta per apply, let
//! the runtime persist and broadcast.
//!
//! ## Authoring a Projection
//!
//! ```rust,ignore
//! use ferro_projection::{Projection, ProjectionKey, ProjectionRuntime};
//! use ferro_events::Event;
//! use ferro_broadcast::Broadcaster;
//! use serde::{Deserialize, Serialize};
//! use std::sync::Arc;
//!
//! // Consumer event (already implements ferro_events::Event).
//! #[derive(Clone, Serialize, Deserialize)]
//! struct InventoryAdjusted { warehouse: String, sku: String, delta: i32 }
//!
//! impl Event for InventoryAdjusted {
//! fn name(&self) -> &'static str { "InventoryAdjusted" }
//! }
//!
//! // Consumer projection state + delta.
//! #[derive(Clone, Default, Serialize, Deserialize)]
//! struct WarehouseDashboard {
//! totals: std::collections::HashMap<String, i64>,
//! }
//!
//! #[derive(Clone, Serialize)]
//! struct WarehouseDelta { sku: String, new_total: i64 }
//!
//! // Consumer projection impl.
//! struct WarehouseProjection;
//!
//! impl Projection for WarehouseProjection {
//! type Event = InventoryAdjusted;
//! type State = WarehouseDashboard;
//! type Delta = WarehouseDelta;
//!
//! const NAME: &'static str = "inventory.dashboard";
//!
//! fn key(&self, event: &Self::Event) -> ProjectionKey {
//! ProjectionKey::new(event.warehouse.clone())
//! }
//!
//! fn apply(&self, state: &mut Self::State, event: &Self::Event) -> Self::Delta {
//! let new_total = state.totals.entry(event.sku.clone()).or_insert(0);
//! *new_total += event.delta as i64;
//! WarehouseDelta { sku: event.sku.clone(), new_total: *new_total }
//! }
//! }
//!
//! // Application setup (one-line wiring):
//! let runtime = Arc::new(ProjectionRuntime::new(
//! db.clone(),
//! broadcaster.clone(),
//! WarehouseProjection,
//! ));
//! runtime.clone().register();
//!
//! // Anywhere in the app:
//! InventoryAdjusted { warehouse: "a".into(), sku: "sku-1".into(), delta: 5 }
//! .dispatch()
//! .await?;
//!
//! // Frontend subscribes to `projection.inventory.dashboard.a` and
//! // receives event `"delta"` with payload `{ "sku": "sku-1", "new_total": 5 }`.
//! ```
//!
//! ## Naming conventions
//!
//! - `NAME`: dotted namespace — `"inventory.dashboard"`,
//! `"checkout.cart"`, `"orders.recent"`. Same convention as
//! `ferro_audit`'s action namespace and `ferro_reservation::Resource::KIND`.
//! - `key()`: stringify any compound key — multi-tenancy lives in the
//! key string (`"tenant-7:warehouse-a"`).
use DeserializeOwned;
use Serialize;
use crateProjectionKey;
/// Consumer-implemented live read-model (D-06).
///
/// Implementations are usually unit structs because the projection's
/// behaviour is encoded in `apply`. Carry state only if `apply` needs
/// configuration (e.g., a tenant-id allowlist).