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
use std::fmt;

use vox_schema::{
    ChannelDirection, FieldSchema, PrimitiveType, Schema, SchemaHash, SchemaKind, SchemaRegistry,
    TypeRef, VariantSchema,
};

#[derive(Debug)]
pub enum SerializeError {
    UnsupportedType(String),
    ReflectError(String),
}

impl fmt::Display for SerializeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnsupportedType(ty) => write!(f, "unsupported type: {ty}"),
            Self::ReflectError(msg) => write!(f, "reflect error: {msg}"),
        }
    }
}

impl std::error::Error for SerializeError {}

#[derive(Debug)]
pub enum DeserializeError {
    UnexpectedEof {
        pos: usize,
    },
    VarintOverflow {
        pos: usize,
    },
    InvalidBool {
        pos: usize,
        got: u8,
    },
    InvalidUtf8 {
        pos: usize,
    },
    InvalidOptionTag {
        pos: usize,
        got: u8,
    },
    InvalidEnumDiscriminant {
        pos: usize,
        index: u64,
        variant_count: usize,
    },
    UnsupportedType(String),
    ReflectError(String),
    UnknownVariant {
        remote_index: usize,
    },
    TrailingBytes {
        pos: usize,
        len: usize,
    },
    Custom(String),
    /// A protocol-level error: missing schemas, missing tracker, etc.
    // r[impl schema.exchange.required]
    Protocol(String),
}

impl fmt::Display for DeserializeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnexpectedEof { pos } => write!(f, "unexpected EOF at byte {pos}"),
            Self::VarintOverflow { pos } => write!(f, "varint overflow at byte {pos}"),
            Self::InvalidBool { pos, got } => write!(f, "invalid bool 0x{got:02x} at byte {pos}"),
            Self::InvalidUtf8 { pos } => write!(f, "invalid UTF-8 at byte {pos}"),
            Self::InvalidOptionTag { pos, got } => {
                write!(f, "invalid option tag 0x{got:02x} at byte {pos}")
            }
            Self::InvalidEnumDiscriminant {
                pos,
                index,
                variant_count,
            } => {
                write!(
                    f,
                    "enum discriminant {index} out of range (0..{variant_count}) at byte {pos}"
                )
            }
            Self::UnsupportedType(ty) => write!(f, "unsupported type: {ty}"),
            Self::ReflectError(msg) => write!(f, "reflect error: {msg}"),
            Self::UnknownVariant { remote_index } => {
                write!(f, "unknown remote enum variant index {remote_index}")
            }
            Self::TrailingBytes { pos, len } => {
                write!(
                    f,
                    "trailing bytes: {remaining} at byte {pos}",
                    remaining = len - pos
                )
            }
            Self::Custom(msg) => write!(f, "{msg}"),
            Self::Protocol(msg) => write!(f, "protocol error: {msg}"),
        }
    }
}

impl DeserializeError {
    pub fn protocol(msg: &str) -> Self {
        Self::Protocol(msg.to_string())
    }
}

impl std::error::Error for DeserializeError {}

/// Path from a root type to a specific location in the schema tree.
///
/// Formatted as `RootType.field.nested_field` or `RootType::Variant.field`.
#[derive(Debug, Clone, Default)]
pub struct SchemaPath {
    segments: Vec<PathSegment>,
}

/// One segment in a schema path.
#[derive(Debug, Clone)]
pub enum PathSegment {
    /// A struct field: `.field_name`
    Field(String),
    /// An enum variant: `::VariantName`
    Variant(String),
    /// A tuple element: `.0`, `.1`, etc.
    Index(usize),
}

impl SchemaPath {
    pub fn new() -> Self {
        Self {
            segments: Vec::new(),
        }
    }

    /// Prepend a segment to the front of the path.
    pub fn push_front(&mut self, segment: PathSegment) {
        self.segments.insert(0, segment);
    }

    pub fn with_front(mut self, segment: PathSegment) -> Self {
        self.push_front(segment);
        self
    }
}

impl fmt::Display for SchemaPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for segment in &self.segments {
            match segment {
                PathSegment::Field(name) => write!(f, ".{name}")?,
                PathSegment::Variant(name) => write!(f, "::{name}")?,
                PathSegment::Index(i) => write!(f, ".{i}")?,
            }
        }
        Ok(())
    }
}

// r[impl schema.errors.content]
// r[impl schema.errors.early-detection]
#[derive(Debug)]
pub struct TranslationError {
    /// Path from the root type to the error site.
    pub path: SchemaPath,
    /// The specific incompatibility.
    pub kind: Box<TranslationErrorKind>,
}

#[derive(Debug)]
pub enum TranslationErrorKind {
    /// The root type names don't match.
    NameMismatch {
        remote: Schema,
        local: Schema,
        remote_rust: String,
        local_rust: String,
    },
    /// The structural kinds don't match (e.g. remote is enum, local is struct).
    KindMismatch {
        remote: Schema,
        local: Schema,
        remote_rust: String,
        local_rust: String,
    },
    // r[impl schema.errors.missing-required]
    /// A required local field has no corresponding remote field and no default.
    MissingRequiredField {
        /// The local field that's missing from the remote schema.
        field: FieldSchema,
        /// The remote struct schema (so you can see what fields it does have).
        remote_struct: Schema,
    },
    // r[impl schema.errors.type-mismatch]
    /// A field exists in both types but the nested types are incompatible.
    FieldTypeMismatch {
        field_name: String,
        remote_field_type: Schema,
        local_field_type: Schema,
        /// The nested error that explains what exactly is incompatible.
        source: Box<TranslationError>,
    },
    /// Enum variant payloads are incompatible (e.g. unit vs struct).
    IncompatibleVariantPayload {
        remote_variant: VariantSchema,
        local_variant: VariantSchema,
    },
    /// A type ID referenced by the remote schema was not found in the registry.
    SchemaNotFound {
        type_id: SchemaHash,
        /// Which side was missing it.
        side: SchemaSide,
    },
    /// Tuple lengths don't match.
    TupleLengthMismatch {
        remote: Schema,
        local: Schema,
        remote_rust: String,
        local_rust: String,
        remote_len: usize,
        local_len: usize,
    },
    /// A type variable (Var) appeared where a concrete type was expected.
    /// This means Var substitution didn't happen — a bug in the extraction
    /// or plan building pipeline.
    UnresolvedVar { name: String, side: SchemaSide },
}

/// Which side of the schema comparison a missing schema was on.
#[derive(Debug, Clone, Copy)]
pub enum SchemaSide {
    Remote,
    Local,
}

impl fmt::Display for SchemaSide {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SchemaSide::Remote => write!(f, "remote"),
            SchemaSide::Local => write!(f, "local"),
        }
    }
}

impl TranslationError {
    pub fn new(kind: TranslationErrorKind) -> Self {
        Self {
            path: SchemaPath::new(),
            kind: Box::new(kind),
        }
    }

    /// Prepend a path segment when propagating errors up from nested plan building.
    pub fn with_path_prefix(mut self, segment: PathSegment) -> Self {
        self.path.push_front(segment);
        self
    }
}

pub(crate) fn format_schema_rust(schema: &Schema, registry: &SchemaRegistry) -> String {
    match &schema.kind {
        SchemaKind::Struct { name, .. } | SchemaKind::Enum { name, .. } => name.clone(),
        kind => format_schema_kind_rust(kind, registry),
    }
}

pub(crate) fn format_type_ref_rust(type_ref: &TypeRef, registry: &SchemaRegistry) -> String {
    match type_ref {
        TypeRef::Var { name } => name.as_str().to_string(),
        TypeRef::Concrete { type_id, args } => {
            let Some(schema) = registry.get(type_id) else {
                return format!("<missing:{type_id:?}>");
            };
            match &schema.kind {
                SchemaKind::Struct { name, .. } | SchemaKind::Enum { name, .. } => {
                    if args.is_empty() {
                        name.clone()
                    } else {
                        let args = args
                            .iter()
                            .map(|arg| format_type_ref_rust(arg, registry))
                            .collect::<Vec<_>>()
                            .join(", ");
                        format!("{name}<{args}>")
                    }
                }
                kind => format_schema_kind_rust(kind, registry),
            }
        }
    }
}

fn format_schema_kind_rust(kind: &SchemaKind, registry: &SchemaRegistry) -> String {
    match kind {
        SchemaKind::Struct { name, .. } | SchemaKind::Enum { name, .. } => name.clone(),
        SchemaKind::Tuple { elements } => {
            let elements = elements
                .iter()
                .map(|element| format_type_ref_rust(element, registry))
                .collect::<Vec<_>>();
            match elements.len() {
                0 => "()".to_string(),
                1 => format!("({},)", elements[0]),
                _ => format!("({})", elements.join(", ")),
            }
        }
        SchemaKind::List { element } => format!("Vec<{}>", format_type_ref_rust(element, registry)),
        SchemaKind::Map { key, value } => format!(
            "HashMap<{}, {}>",
            format_type_ref_rust(key, registry),
            format_type_ref_rust(value, registry)
        ),
        SchemaKind::Array { element, length } => {
            format!("[{}; {length}]", format_type_ref_rust(element, registry))
        }
        SchemaKind::Option { element } => {
            format!("Option<{}>", format_type_ref_rust(element, registry))
        }
        SchemaKind::Channel { direction, element } => format!(
            "{}<{}>",
            match direction {
                ChannelDirection::Tx => "Tx",
                ChannelDirection::Rx => "Rx",
            },
            format_type_ref_rust(element, registry)
        ),
        SchemaKind::Primitive { primitive_type } => match primitive_type {
            PrimitiveType::Bool => "bool".to_string(),
            PrimitiveType::U8 => "u8".to_string(),
            PrimitiveType::U16 => "u16".to_string(),
            PrimitiveType::U32 => "u32".to_string(),
            PrimitiveType::U64 => "u64".to_string(),
            PrimitiveType::U128 => "u128".to_string(),
            PrimitiveType::I8 => "i8".to_string(),
            PrimitiveType::I16 => "i16".to_string(),
            PrimitiveType::I32 => "i32".to_string(),
            PrimitiveType::I64 => "i64".to_string(),
            PrimitiveType::I128 => "i128".to_string(),
            PrimitiveType::F32 => "f32".to_string(),
            PrimitiveType::F64 => "f64".to_string(),
            PrimitiveType::Char => "char".to_string(),
            PrimitiveType::String => "String".to_string(),
            PrimitiveType::Unit => "()".to_string(),
            PrimitiveType::Never => "never".to_string(),
            PrimitiveType::Bytes => "Vec<u8>".to_string(),
            PrimitiveType::Payload => "Payload".to_string(),
        },
    }
}

impl fmt::Display for TranslationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.path.segments.is_empty() {
            write!(f, "at {}: ", self.path)?;
        }

        match &*self.kind {
            TranslationErrorKind::NameMismatch {
                remote,
                local,
                remote_rust,
                local_rust,
            } => {
                write!(
                    f,
                    "type name mismatch: remote is `{remote_rust}`, local is `{local_rust}` (remote name `{}`, local name `{}`)",
                    remote.name().unwrap_or("<anonymous>"),
                    local.name().unwrap_or("<anonymous>"),
                )
            }
            TranslationErrorKind::KindMismatch {
                remote_rust,
                local_rust,
                ..
            } => {
                write!(
                    f,
                    "structural mismatch: remote is `{remote_rust}`, local is `{local_rust}`",
                )
            }
            TranslationErrorKind::MissingRequiredField {
                field,
                remote_struct,
            } => {
                write!(
                    f,
                    "required field '{}' (type {:?}) missing from remote '{}'",
                    field.name,
                    field.type_ref,
                    format_schema_rust(remote_struct, &SchemaRegistry::new()),
                )?;
                if let SchemaKind::Struct { fields, .. } = &remote_struct.kind {
                    write!(f, " (remote has fields: ")?;
                    for (i, rf) in fields.iter().enumerate() {
                        if i > 0 {
                            write!(f, ", ")?;
                        }
                        write!(f, "{}", rf.name)?;
                    }
                    write!(f, ")")?;
                }
                Ok(())
            }
            TranslationErrorKind::FieldTypeMismatch {
                field_name,
                remote_field_type,
                local_field_type,
                source,
            } => {
                write!(
                    f,
                    "field '{field_name}' type mismatch: remote is '{}', local is '{}': {source}",
                    format_schema_rust(remote_field_type, &SchemaRegistry::new()),
                    format_schema_rust(local_field_type, &SchemaRegistry::new()),
                )
            }
            TranslationErrorKind::IncompatibleVariantPayload {
                remote_variant,
                local_variant,
            } => {
                write!(
                    f,
                    "variant '{}' payload mismatch: remote is {}, local is {}",
                    remote_variant.name,
                    variant_payload_str(&remote_variant.payload),
                    variant_payload_str(&local_variant.payload),
                )
            }
            TranslationErrorKind::SchemaNotFound { type_id, side } => {
                write!(f, "{side} schema not found for type ID {type_id:?}")
            }
            TranslationErrorKind::TupleLengthMismatch {
                remote_rust,
                local_rust,
                remote_len,
                local_len,
                ..
            } => {
                write!(
                    f,
                    "tuple length mismatch: remote `{remote_rust}` has {remote_len} elements, local `{local_rust}` has {local_len} elements",
                )
            }
            TranslationErrorKind::UnresolvedVar { name, side } => {
                write!(
                    f,
                    "unresolved type variable {name} on {side} side — Var substitution failed"
                )
            }
        }
    }
}

fn variant_payload_str(payload: &vox_schema::VariantPayload) -> &'static str {
    match payload {
        vox_schema::VariantPayload::Unit => "unit",
        vox_schema::VariantPayload::Newtype { .. } => "newtype",
        vox_schema::VariantPayload::Tuple { .. } => "tuple",
        vox_schema::VariantPayload::Struct { .. } => "struct",
    }
}

impl std::error::Error for TranslationError {}