vox-postcard 0.4.0

Postcard serialization for vox with translation plan support
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::collections::HashMap;

use facet_core::{Shape, Type, UserType};
use vox_schema::{
    FieldSchema, PrimitiveType, Schema, SchemaHash, SchemaKind, SchemaRegistry, TypeRef,
    VariantPayload, VariantSchema,
};

use crate::error::{PathSegment, SchemaSide, TranslationError, TranslationErrorKind};

/// A precomputed plan for deserializing postcard bytes into a local type.
///
/// This is a recursive enum that mirrors the shape of data. Each variant
/// carries sub-plans for its children, so translation works through
/// arbitrarily nested containers (Vec, Option, Map, etc.).
#[derive(Debug)]
pub enum TranslationPlan {
    /// Identity — no translation needed. Used for leaves (scalars, etc.)
    /// and for container elements when remote and local types match.
    Identity,

    /// Struct or tuple-struct: field reordering, skipping unknown, filling defaults.
    Struct {
        /// One op per remote field, in remote wire order.
        field_ops: Vec<FieldOp>,
        /// Nested plans for fields that need translation, keyed by local field index.
        nested: HashMap<usize, TranslationPlan>,
    },

    /// Enum: variant remapping + per-variant field plans.
    Enum {
        /// For each remote variant index, the local variant index (or None if unknown).
        variant_map: Vec<Option<usize>>,
        /// Per-variant field plans, keyed by remote variant index.
        variant_plans: HashMap<usize, TranslationPlan>,
        /// Nested plans for newtype/tuple variant inner types, keyed by local variant index.
        nested: HashMap<usize, TranslationPlan>,
    },

    /// Tuple: positional elements with nested plans.
    Tuple {
        field_ops: Vec<FieldOp>,
        nested: HashMap<usize, TranslationPlan>,
    },

    /// List/Vec/Slice: element type needs translation.
    List { element: Box<TranslationPlan> },

    /// Option: inner type needs translation.
    Option { inner: Box<TranslationPlan> },

    /// Map: key and/or value types need translation.
    Map {
        key: Box<TranslationPlan>,
        value: Box<TranslationPlan>,
    },

    /// Fixed-size array: element type needs translation.
    Array { element: Box<TranslationPlan> },

    /// Pointer (Box, Arc, etc.): pointee needs translation.
    Pointer { pointee: Box<TranslationPlan> },
}

#[derive(Debug)]
pub enum FieldOp {
    /// Read this remote field into local field at `local_index`.
    Read { local_index: usize },
    /// Skip this remote field (not present in local type).
    Skip { type_ref: TypeRef },
}

/// A schema set: the root schema (with Vars resolved) + the registry.
#[derive(Debug)]
pub struct SchemaSet {
    pub root: Schema,
    pub registry: SchemaRegistry,
}

impl SchemaSet {
    /// Build a SchemaSet from a raw list of schemas (e.g. received from the wire).
    /// The root is the last schema. Its kind is used as-is (no Var resolution).
    pub fn from_schemas(schemas: Vec<Schema>) -> Self {
        let root = schemas.last().cloned().expect("empty schema list");
        let registry = vox_schema::build_registry(&schemas);
        SchemaSet { root, registry }
    }

    /// Build a SchemaSet from extracted root/schemas data.
    /// The root TypeRef is used to resolve any Var references in the root schema.
    pub fn from_root_and_schemas(root: TypeRef, schemas: Vec<Schema>) -> Self {
        let registry = vox_schema::build_registry(&schemas);
        let root_kind = root
            .resolve_kind(&registry)
            .expect("root schema must be in registry");
        let root_id = match &root {
            TypeRef::Concrete { type_id, .. } => *type_id,
            TypeRef::Var { .. } => unreachable!("root type ref is never a Var"),
        };
        let root = Schema {
            id: root_id,
            type_params: vec![],
            kind: root_kind,
        };
        SchemaSet { root, registry }
    }
}

/// Input to `build_plan`: identifies which side is remote and which is local.
pub struct PlanInput<'a> {
    pub remote: &'a SchemaSet,
    pub local: &'a SchemaSet,
}

/// Build the trivial identity plan from a local Shape alone.
/// Every field maps 1:1, no skips, no defaults. Used when no remote schema
/// is available (same types on both sides).
pub fn build_identity_plan(shape: &'static Shape) -> TranslationPlan {
    match shape.ty {
        Type::User(UserType::Struct(struct_type)) => {
            let field_ops = (0..struct_type.fields.len())
                .map(|i| FieldOp::Read { local_index: i })
                .collect();
            TranslationPlan::Struct {
                field_ops,
                nested: HashMap::new(),
            }
        }
        Type::User(UserType::Enum(enum_type)) => {
            let variant_map = (0..enum_type.variants.len()).map(Some).collect();
            let mut variant_plans = HashMap::new();
            for (i, variant) in enum_type.variants.iter().enumerate() {
                let field_ops = (0..variant.data.fields.len())
                    .map(|j| FieldOp::Read { local_index: j })
                    .collect();
                variant_plans.insert(
                    i,
                    TranslationPlan::Struct {
                        field_ops,
                        nested: HashMap::new(),
                    },
                );
            }
            TranslationPlan::Enum {
                variant_map,
                variant_plans,
                nested: HashMap::new(),
            }
        }
        _ => TranslationPlan::Identity,
    }
}

/// Build a translation plan by comparing remote and local schemas.
///
/// Both sides are represented as schemas — the same extraction logic
/// (channel unwrapping, transparent wrappers, etc.) has already run on
/// both. This avoids mismatches between schema representation and raw
/// Shape inspection.
// r[impl schema.translation.field-matching]
// r[impl schema.translation.skip-unknown]
// r[impl schema.translation.fill-defaults]
// r[impl schema.translation.reorder]
// r[impl schema.errors.early-detection]
pub fn build_plan(input: &PlanInput) -> Result<TranslationPlan, TranslationError> {
    let remote = &input.remote.root;
    let local = &input.local.root;

    // Validate type names match for nominal types (struct/enum).
    if let (Some(remote_name), Some(local_name)) = (remote.name(), local.name())
        && remote_name != local_name
    {
        return Err(TranslationError::new(TranslationErrorKind::NameMismatch {
            remote: remote.clone(),
            local: local.clone(),
            remote_rust: crate::error::format_schema_rust(remote, &input.remote.registry),
            local_rust: crate::error::format_schema_rust(local, &input.local.registry),
        }));
    }

    if is_byte_buffer_kind(&remote.kind, &input.remote.registry)
        && is_byte_buffer_kind(&local.kind, &input.local.registry)
    {
        return Ok(TranslationPlan::Identity);
    }

    match (&remote.kind, &local.kind) {
        (
            SchemaKind::Struct {
                fields: remote_fields,
                ..
            },
            SchemaKind::Struct {
                fields: local_fields,
                ..
            },
        ) => build_struct_plan(remote_fields, local_fields, remote, local, input),
        (
            SchemaKind::Enum {
                variants: remote_variants,
                ..
            },
            SchemaKind::Enum {
                variants: local_variants,
                ..
            },
        ) => build_enum_plan(remote_variants, local_variants, remote, local, input),
        (
            SchemaKind::Tuple {
                elements: remote_elements,
            },
            SchemaKind::Tuple {
                elements: local_elements,
            },
        ) => build_tuple_plan(remote_elements, local_elements, remote, local, input),
        // Container types — recurse into element/value types
        (
            SchemaKind::List {
                element: remote_elem,
            },
            SchemaKind::List {
                element: local_elem,
            },
        ) => {
            let element_plan = nested_plan(remote_elem, local_elem, input)?;
            Ok(TranslationPlan::List {
                element: Box::new(element_plan.unwrap_or(TranslationPlan::Identity)),
            })
        }
        (
            SchemaKind::Option {
                element: remote_elem,
            },
            SchemaKind::Option {
                element: local_elem,
            },
        ) => {
            let inner_plan = nested_plan(remote_elem, local_elem, input)?;
            Ok(TranslationPlan::Option {
                inner: Box::new(inner_plan.unwrap_or(TranslationPlan::Identity)),
            })
        }
        (
            SchemaKind::Map {
                key: remote_key,
                value: remote_val,
            },
            SchemaKind::Map {
                key: local_key,
                value: local_val,
            },
        ) => {
            let key_plan = nested_plan(remote_key, local_key, input)?;
            let val_plan = nested_plan(remote_val, local_val, input)?;
            Ok(TranslationPlan::Map {
                key: Box::new(key_plan.unwrap_or(TranslationPlan::Identity)),
                value: Box::new(val_plan.unwrap_or(TranslationPlan::Identity)),
            })
        }
        (
            SchemaKind::Array {
                element: remote_elem,
                ..
            },
            SchemaKind::Array {
                element: local_elem,
                ..
            },
        ) => {
            let element_plan = nested_plan(remote_elem, local_elem, input)?;
            Ok(TranslationPlan::Array {
                element: Box::new(element_plan.unwrap_or(TranslationPlan::Identity)),
            })
        }
        (
            SchemaKind::Channel {
                direction: remote_direction,
                ..
            },
            SchemaKind::Channel {
                direction: local_direction,
                ..
            },
        ) if remote_direction == local_direction => Ok(TranslationPlan::Identity),
        (
            SchemaKind::Primitive {
                primitive_type: remote_primitive,
            },
            SchemaKind::Primitive {
                primitive_type: local_primitive,
            },
        ) if remote_primitive == local_primitive => Ok(TranslationPlan::Identity),
        // Kind mismatch
        _ => Err(TranslationError::new(TranslationErrorKind::KindMismatch {
            remote: remote.clone(),
            local: local.clone(),
            remote_rust: crate::error::format_schema_rust(remote, &input.remote.registry),
            local_rust: crate::error::format_schema_rust(local, &input.local.registry),
        })),
    }
}

/// Build a nested plan for two TypeRefs looked up in their respective registries.
/// Handles generic types by resolving Var references using the TypeRef's args.
fn nested_plan(
    remote_type_ref: &TypeRef,
    local_type_ref: &TypeRef,
    input: &PlanInput,
) -> Result<Option<TranslationPlan>, TranslationError> {
    let resolve_schema = |type_ref: &TypeRef, registry: &SchemaRegistry, side: SchemaSide| {
        let type_id = match type_ref {
            TypeRef::Concrete { type_id, .. } => *type_id,
            TypeRef::Var { name } => {
                return Err(TranslationError::new(TranslationErrorKind::UnresolvedVar {
                    name: format!("{name:?}"),
                    side,
                }));
            }
        };
        let kind = type_ref.resolve_kind(registry).ok_or_else(|| {
            TranslationError::new(TranslationErrorKind::SchemaNotFound { type_id, side })
        })?;
        let base = registry.get(&type_id).ok_or_else(|| {
            TranslationError::new(TranslationErrorKind::SchemaNotFound { type_id, side })
        })?;
        Ok(Schema {
            id: base.id,
            type_params: vec![],
            kind,
        })
    };

    let remote_schema =
        resolve_schema(remote_type_ref, &input.remote.registry, SchemaSide::Remote)?;
    let local_schema = resolve_schema(local_type_ref, &input.local.registry, SchemaSide::Local)?;

    let sub_input = PlanInput {
        remote: &SchemaSet {
            root: remote_schema,
            registry: input.remote.registry.clone(),
        },
        local: &SchemaSet {
            root: local_schema,
            registry: input.local.registry.clone(),
        },
    };
    build_plan(&sub_input).map(Some)
}

fn is_byte_buffer_kind(kind: &SchemaKind, registry: &SchemaRegistry) -> bool {
    match kind {
        SchemaKind::Primitive {
            primitive_type: PrimitiveType::Bytes,
        } => true,
        SchemaKind::List { element } => matches!(
            element.resolve_kind(registry),
            Some(SchemaKind::Primitive {
                primitive_type: PrimitiveType::U8,
            })
        ),
        _ => false,
    }
}

fn build_struct_plan(
    remote_fields: &[FieldSchema],
    local_fields: &[FieldSchema],
    remote_schema: &Schema,
    _local_schema: &Schema,
    input: &PlanInput,
) -> Result<TranslationPlan, TranslationError> {
    let mut field_ops = Vec::with_capacity(remote_fields.len());
    let mut nested = HashMap::new();
    let mut matched_local = vec![false; local_fields.len()];

    for remote_field in remote_fields {
        if let Some((local_idx, local_field)) = local_fields
            .iter()
            .enumerate()
            .find(|(_, f)| f.name == remote_field.name)
        {
            matched_local[local_idx] = true;
            field_ops.push(FieldOp::Read {
                local_index: local_idx,
            });

            // r[impl schema.translation.type-compat]
            let nested_plan = nested_plan(&remote_field.type_ref, &local_field.type_ref, input)
                .map_err(|e| e.with_path_prefix(PathSegment::Field(remote_field.name.clone())))?;
            if let Some(plan) = nested_plan {
                nested.insert(local_idx, plan);
            }
        } else {
            field_ops.push(FieldOp::Skip {
                type_ref: remote_field.type_ref.clone(),
            });
        }
    }

    // r[impl schema.errors.missing-required]
    for (i, matched) in matched_local.iter().enumerate() {
        if !matched && local_fields[i].required {
            return Err(TranslationError::new(
                TranslationErrorKind::MissingRequiredField {
                    field: local_fields[i].clone(),
                    remote_struct: remote_schema.clone(),
                },
            ));
        }
    }

    Ok(TranslationPlan::Struct { field_ops, nested })
}

fn build_tuple_plan(
    remote_elements: &[TypeRef<SchemaHash>],
    local_elements: &[TypeRef<SchemaHash>],
    remote_schema: &Schema,
    local_schema: &Schema,
    input: &PlanInput,
) -> Result<TranslationPlan, TranslationError> {
    if remote_elements.len() != local_elements.len() {
        return Err(TranslationError::new(
            TranslationErrorKind::TupleLengthMismatch {
                remote: remote_schema.clone(),
                local: local_schema.clone(),
                remote_rust: crate::error::format_schema_rust(
                    remote_schema,
                    &input.remote.registry,
                ),
                local_rust: crate::error::format_schema_rust(local_schema, &input.local.registry),
                remote_len: remote_elements.len(),
                local_len: local_elements.len(),
            },
        ));
    }

    let mut field_ops = Vec::with_capacity(remote_elements.len());
    let mut nested = HashMap::new();

    for (i, (remote_elem, local_elem)) in remote_elements
        .iter()
        .zip(local_elements.iter())
        .enumerate()
    {
        field_ops.push(FieldOp::Read { local_index: i });

        let nested_plan = nested_plan(remote_elem, local_elem, input)
            .map_err(|e| e.with_path_prefix(PathSegment::Index(i)))?;
        if let Some(plan) = nested_plan {
            nested.insert(i, plan);
        }
    }

    Ok(TranslationPlan::Tuple { field_ops, nested })
}

// r[impl schema.translation.enum]
// r[impl schema.translation.enum.missing-variant]
// r[impl schema.translation.enum.payload-compat]
fn build_enum_plan(
    remote_variants: &[VariantSchema],
    local_variants: &[VariantSchema],
    _remote_schema: &Schema,
    _local_schema: &Schema,
    input: &PlanInput,
) -> Result<TranslationPlan, TranslationError> {
    let mut variant_map = Vec::with_capacity(remote_variants.len());
    let mut variant_plans = HashMap::new();
    let mut nested = HashMap::new();

    for (remote_idx, remote_variant) in remote_variants.iter().enumerate() {
        if let Some((local_idx, local_variant)) = local_variants
            .iter()
            .enumerate()
            .find(|(_, v)| v.name == remote_variant.name)
        {
            variant_map.push(Some(local_idx));

            match (&remote_variant.payload, &local_variant.payload) {
                // Both struct variants — build a per-variant field plan
                (
                    VariantPayload::Struct {
                        fields: remote_fields,
                    },
                    VariantPayload::Struct {
                        fields: local_fields,
                    },
                ) => {
                    let variant_field_ops: Vec<FieldOp> = remote_fields
                        .iter()
                        .map(|rf| {
                            if let Some((local_field_idx, _)) = local_fields
                                .iter()
                                .enumerate()
                                .find(|(_, f)| f.name == rf.name)
                            {
                                FieldOp::Read {
                                    local_index: local_field_idx,
                                }
                            } else {
                                FieldOp::Skip {
                                    type_ref: rf.type_ref.clone(),
                                }
                            }
                        })
                        .collect();
                    variant_plans.insert(
                        remote_idx,
                        TranslationPlan::Struct {
                            field_ops: variant_field_ops,
                            nested: HashMap::new(),
                        },
                    );
                }
                // Both newtype — build a nested plan for the inner type
                (
                    VariantPayload::Newtype {
                        type_ref: remote_inner_ref,
                    },
                    VariantPayload::Newtype {
                        type_ref: local_inner_ref,
                    },
                ) => {
                    let inner_plan = nested_plan(remote_inner_ref, local_inner_ref, input)
                        .map_err(|e| {
                            e.with_path_prefix(PathSegment::Variant(remote_variant.name.clone()))
                        })?;
                    if let Some(plan) = inner_plan {
                        nested.insert(local_idx, plan);
                    }
                }
                // Both tuple — check arity matches and build nested plans
                (
                    VariantPayload::Tuple {
                        types: remote_types,
                    },
                    VariantPayload::Tuple { types: local_types },
                ) => {
                    let tuple_plan = build_tuple_plan(
                        remote_types,
                        local_types,
                        _remote_schema,
                        _local_schema,
                        input,
                    )
                    .map_err(|e| {
                        e.with_path_prefix(PathSegment::Variant(remote_variant.name.clone()))
                    })?;
                    variant_plans.insert(remote_idx, tuple_plan);
                }
                (VariantPayload::Unit, VariantPayload::Unit) => {}
                // Payload kind mismatch within a variant
                _ => {
                    return Err(TranslationError::new(
                        TranslationErrorKind::IncompatibleVariantPayload {
                            remote_variant: remote_variant.clone(),
                            local_variant: local_variant.clone(),
                        },
                    )
                    .with_path_prefix(PathSegment::Variant(remote_variant.name.clone())));
                }
            }
        } else {
            // r[impl schema.translation.enum.unknown-variant]
            variant_map.push(None);
        }
    }

    Ok(TranslationPlan::Enum {
        variant_map,
        variant_plans,
        nested,
    })
}