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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! [`DynamicSchema`] — declarative description of a postcard-encoded
//! payload's shape.
//!
//! Postcard is **not self-describing**: a postcard payload is a flat
//! byte stream whose meaning depends entirely on the type that
//! produced it. To migrate a v1 document into a v2 type at decode
//! time the codec needs **some** description of the v1 wire shape;
//! `DynamicSchema` is that description.
//!
//! A `DynamicSchema` is pure data — a tree of enum variants, no
//! generics, no lifetimes beyond `'static`, no allocations beyond
//! the `Vec`/`Box` literal data carries. The schema describes the
//! field order + per-field type of a postcard payload; the walker
//! [`Dynamic::from_postcard_bytes`](crate::codec::Dynamic::from_postcard_bytes)
//! consumes both schema and bytes and produces a structured
//! [`Dynamic`](crate::codec::Dynamic) view.
//!
//! # Wire-format dependency
//!
//! `DynamicSchema` only describes postcard payloads. The mapping of
//! schema variants → postcard byte-stream operations is the **only**
//! postcard-specific knowledge in obj's migration path:
//!
//! | Schema variant | Postcard wire shape |
//! |----------------------|------------------------------------------------------------|
//! | `Null` | (zero bytes — `()` / unit struct) |
//! | `Bool` | 1 byte (`0` = false, `1` = true) |
//! | `U64` | unsigned LEB128 varint |
//! | `I64` | zigzag-encoded varint |
//! | `F64` | 8 little-endian bytes |
//! | `String` | varint length + UTF-8 bytes |
//! | `Bytes` | varint length + raw bytes |
//! | `Seq(elem)` | varint length, then N elements of the inner schema |
//! | `Map(fields)` | each `(name, schema)` decoded in order — *no* field names |
//! | `Enum(variants)` | varint `u32` discriminant, then the matched variant's payload |
//!
//! Postcard treats a Rust struct as a **field-ordered tuple** with
//! no names, so `Map` in this schema does NOT correspond to
//! postcard's `serialize_map`; it corresponds to a sequence of
//! per-field bytes. Field names are an obj-side convention used to
//! tag fields in the resulting `Dynamic` for `get` / `set` / `remove`
//! addressing.
//!
//! For enums, postcard writes a varint `u32` discriminant followed
//! by the matched variant's payload. Unit variants carry **only**
//! the discriminant; newtype / tuple / struct variants follow with
//! the inner type's field-ordered bytes (no length prefix — the
//! schema names every field for `Dynamic::get` addressing the same
//! way `Map` does). Tuple variants are represented by `Map` with
//! synthetic numeric field names (`"0"`, `"1"`, …) — there is no
//! `Tuple` schema variant.
//!
//! # Power-of-ten posture
//!
//! - **Rule 1.** The walker that consumes a `DynamicSchema` uses an
//! explicit stack (`Vec<Frame>`), not Rust-language recursion. Tree
//! depth is bounded by [`MAX_SCHEMA_DEPTH`].
//! - **Rule 2.** Every `Seq` / `Map` decode reads its length as a
//! varint and bounds the per-frame iteration by that length;
//! pathologically large lengths are rejected before any allocation.
//! - **Rule 5.** Schema construction is pure-data — runtime
//! correctness checks live in the walker, not in `DynamicSchema`
//! itself.
//! - **Rule 7.** Every fallible walker step propagates via `?`; no
//! `unwrap` / `expect` on byte-stream input.
/// Maximum nesting depth of a [`DynamicSchema`] tree that the walker
/// will traverse. Exceeding this bound returns
/// [`Error::SchemaDepthExceeded`](crate::error::Error::SchemaDepthExceeded).
///
/// 32 matches [`crate::codec::dynamic::MAX_DYNAMIC_DEPTH`] so the
/// schema walker cannot produce a deeper `Dynamic` than the tagged-
/// format walker can re-encode.
pub const MAX_SCHEMA_DEPTH: usize = 32;
/// Describes the byte-stream shape of a postcard-encoded payload at
/// one version. See module docs for the variant ↔ wire-format
/// mapping.
///
/// `DynamicSchema` is `'static`-friendly: literal schemas can live in
/// const-fn-adjacent contexts (e.g. a `LazyLock` registry) without
/// borrowing.
///
/// `Box`ing the inner schema in `Seq` keeps the enum's `size_of`
/// reasonable (the alternative — `Vec<DynamicSchema>` of length 1 —
/// is overkill for the single-element case and reads worse).
/// One variant of a [`DynamicSchema::Enum`] description.
///
/// `discriminant` is the varint `u32` postcard writes for this
/// variant (postcard uses the Rust enum's declaration order, so the
/// first variant has discriminant `0`, the second `1`, and so on —
/// `#[serde(other)]` / explicit discriminants change this). `name`
/// is the Rust variant identifier carried through to the decoded
/// [`Dynamic::Enum`](crate::codec::Dynamic) so a `Migrate::migrate`
/// impl can distinguish variants by name. `payload` is the variant's
/// inner shape, `Box`ed because `DynamicSchema` is self-referential.
/// A type whose postcard wire shape is describable by a
/// [`DynamicSchema`].
///
/// `Schema` is implemented for every `T: Document` (via the M9
/// derive, see `obj_derive::Document`) and **also** for hand-written
/// "historical" types that describe the wire shape of an older
/// version of a Document. Historical types never need to be
/// `Document`s themselves — they exist purely to describe bytes the
/// current reader still has on disk.
///
/// Separating `Schema` from `Document` keeps the migration registry
/// (#82) honest: a `historical_schemas()` entry refers to a
/// `Schema`-only type that owns no collection name and has no
/// `Document::VERSION` of its own (the `(u32, ...)` pair on the
/// outside carries the version).
///
/// # Power-of-ten posture
///
/// - **Rule 9.** No `dyn`; static dispatch via the associated
/// function below.
/// - **Rule 5.** Implementations return a fresh `DynamicSchema` by
/// value — pure data, no shared mutable state.