tokenspeed-wire 0.1.0

TokenSpeed engine IPC schema and msgpack codec
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
// Copyright (c) 2026 LightSeek Foundation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! The [`Wire`] trait and the struct layouts msgspec uses.
//!
//! msgspec encodes a `Struct` in one of two layouts, both mirrored here by
//! [`wire_struct!`]:
//!
//! * **array** (`array_like=True`): `[tag?, field, ...]` in declaration
//!   order. Declaration order is the wire contract, so fields are only ever
//!   appended. A shorter array takes the remaining fields' defaults; a
//!   longer one has its extra trailing elements ignored. A tagged struct
//!   (`tag=True`) starts with its class name.
//! * **map**: `{name: value, ...}` with every field present, in declaration
//!   order. Unknown keys are ignored and missing keys take their defaults.
//!
//! Both layouts fail when a field without a default is absent.

use bytes::Bytes;

use crate::{
    error::{Error, Result},
    msgpack::{Decoder, Encoder, Kind},
};

/// A type with a msgpack encoding shared with the Python side.
pub trait Wire: Sized {
    /// Append this value to a message.
    fn encode(&self, encoder: &mut Encoder);

    /// Read one value of this type.
    fn decode(decoder: &mut Decoder<'_>) -> Result<Self>;
}

/// Encode a value as a message: the msgpack buffer, then its out-of-band
/// frames.
pub fn encode_frames<T: Wire>(value: &T) -> Vec<Bytes> {
    let mut encoder = Encoder::new();
    value.encode(&mut encoder);
    encoder.into_frames()
}

/// Decode a message from its frames, failing on bytes after the value.
pub fn decode_frames<T: Wire>(frames: &[Bytes]) -> Result<T> {
    let mut decoder = Decoder::new(frames);
    let value = T::decode(&mut decoder)?;
    decoder.finish()?;
    Ok(value)
}

/// Decode a message that is a single msgpack buffer.
pub fn decode_bytes<T: Wire>(bytes: &[u8]) -> Result<T> {
    decode_frames(&[Bytes::copy_from_slice(bytes)])
}

impl Wire for bool {
    fn encode(&self, encoder: &mut Encoder) {
        encoder.bool(*self);
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        decoder.bool()
    }
}

macro_rules! wire_int {
    ($($ty:ty => $write:ident),* $(,)?) => {$(
        impl Wire for $ty {
            fn encode(&self, encoder: &mut Encoder) {
                encoder.$write((*self).into());
            }

            fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
                decoder.integer()?.narrow(stringify!($ty))
            }
        }
    )*};
}

wire_int!(i32 => int, i64 => int, u16 => uint, u32 => uint, u64 => uint);

impl Wire for f64 {
    fn encode(&self, encoder: &mut Encoder) {
        encoder.float(*self);
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        decoder.float()
    }
}

impl Wire for String {
    fn encode(&self, encoder: &mut Encoder) {
        encoder.str(self);
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        Ok(decoder.str()?.to_owned())
    }
}

/// `None` is `nil` on the wire.
impl<T: Wire> Wire for Option<T> {
    fn encode(&self, encoder: &mut Encoder) {
        match self {
            Some(value) => value.encode(encoder),
            None => encoder.nil(),
        }
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        if decoder.take_nil()? {
            Ok(None)
        } else {
            T::decode(decoder).map(Some)
        }
    }
}

/// Lists, tuples of any length and sets are all arrays on the wire.
impl<T: Wire> Wire for Vec<T> {
    fn encode(&self, encoder: &mut Encoder) {
        encoder.array_len(self.len());
        for item in self {
            item.encode(encoder);
        }
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        let len = decoder.array_len()?;
        // Bound the up-front reservation: the length is untrusted input.
        let mut items = Vec::with_capacity(len.min(1 << 16));
        for _ in 0..len {
            items.push(T::decode(decoder)?);
        }
        Ok(items)
    }
}

/// A two-element tuple, an array of length two on the wire.
impl<A: Wire, B: Wire> Wire for (A, B) {
    fn encode(&self, encoder: &mut Encoder) {
        encoder.array_len(2);
        self.0.encode(encoder);
        self.1.encode(encoder);
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        let len = decoder.array_len()?;
        if len != 2 {
            return Err(Error::Invalid(format!(
                "expected a 2-element array, found {len} elements"
            )));
        }
        Ok((A::decode(decoder)?, B::decode(decoder)?))
    }
}

/// A Python `dict`: a msgpack map whose entries keep their wire order.
#[derive(Debug, Clone, PartialEq)]
pub struct Dict<K, V>(pub Vec<(K, V)>);

impl<K, V> Default for Dict<K, V> {
    fn default() -> Self {
        Dict(Vec::new())
    }
}

impl<K: PartialEq, V> Dict<K, V> {
    /// The value for `key`, if present.
    pub fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: PartialEq<Q>,
        Q: ?Sized,
    {
        self.0.iter().find(|(k, _)| k == key).map(|(_, v)| v)
    }
}

impl<K: Wire, V: Wire> Wire for Dict<K, V> {
    fn encode(&self, encoder: &mut Encoder) {
        encoder.map_len(self.0.len());
        for (key, value) in &self.0 {
            key.encode(encoder);
            value.encode(encoder);
        }
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        let len = decoder.map_len()?;
        let mut entries = Vec::with_capacity(len.min(1 << 16));
        for _ in 0..len {
            let key = K::decode(decoder)?;
            entries.push((key, V::decode(decoder)?));
        }
        Ok(Dict(entries))
    }
}

/// A Python `str | list[str]` field.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StrOrList {
    /// A single string.
    One(String),
    /// A list of strings.
    Many(Vec<String>),
}

impl Wire for StrOrList {
    fn encode(&self, encoder: &mut Encoder) {
        match self {
            StrOrList::One(value) => value.encode(encoder),
            StrOrList::Many(values) => values.encode(encoder),
        }
    }

    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
        match decoder.peek_kind()? {
            Kind::Str => Ok(StrOrList::One(String::decode(decoder)?)),
            Kind::Array => Ok(StrOrList::Many(Vec::decode(decoder)?)),
            _ => Err(Error::Invalid(
                "expected a string or a list of strings".to_owned(),
            )),
        }
    }
}

/// Reads the fields of an array-layout struct in declaration order.
#[doc(hidden)]
pub struct ArrayFields<'d, 'f> {
    decoder: &'d mut Decoder<'f>,
    remaining: usize,
}

impl<'d, 'f> ArrayFields<'d, 'f> {
    /// Read the array header.
    pub fn begin(decoder: &'d mut Decoder<'f>) -> Result<Self> {
        let remaining = decoder.array_len()?;
        Ok(Self { decoder, remaining })
    }

    /// Check the tag a tagged struct starts with.
    pub fn tag(&mut self, expected: &'static str) -> Result<()> {
        if self.remaining == 0 {
            return Err(Error::MissingField("_tag"));
        }
        self.remaining -= 1;
        let found = self.decoder.str()?;
        if found != expected {
            return Err(Error::Tag {
                expected,
                found: found.to_owned(),
            });
        }
        Ok(())
    }

    /// The next field, or `None` once the array is exhausted.
    pub fn next<T: Wire>(&mut self, name: &'static str) -> Result<Option<T>> {
        if self.remaining == 0 {
            return Ok(None);
        }
        self.remaining -= 1;
        T::decode(self.decoder)
            .map(Some)
            .map_err(|error| error.in_field(name))
    }

    /// Skip trailing elements a newer peer appended.
    pub fn finish(self) -> Result<()> {
        for _ in 0..self.remaining {
            self.decoder.skip()?;
        }
        Ok(())
    }
}

/// Define a struct and its [`Wire`] codec in one of msgspec's layouts.
///
/// ```ignore
/// wire_struct! {
///     /// Doc.
///     pub struct Example: array("Example") {
///         /// A required field.
///         pub name: String,
///         /// A field with a default, used when an older peer omits it.
///         pub count: i64 = 0,
///     }
/// }
/// ```
///
/// The layout is `array` (optionally with a tag) or `map`. Writing
/// `+ Default` after the layout also derives `Default` from the field
/// defaults, which then must be given for every field.
macro_rules! wire_struct {
    (
        $(#[$meta:meta])*
        $vis:vis struct $name:ident: $layout:ident $(($tag:expr))? + Default {
            $( $(#[$fmeta:meta])* $fvis:vis $field:ident: $fty:ty = $fdefault:expr ),* $(,)?
        }
    ) => {
        wire_struct! {
            $(#[$meta])*
            $vis struct $name: $layout $(($tag))? {
                $( $(#[$fmeta])* $fvis $field: $fty = $fdefault ),*
            }
        }

        impl Default for $name {
            fn default() -> Self {
                Self { $( $field: $fdefault ),* }
            }
        }
    };
    (
        $(#[$meta:meta])*
        $vis:vis struct $name:ident: $layout:ident $(($tag:expr))? {
            $( $(#[$fmeta:meta])* $fvis:vis $field:ident: $fty:ty $(= $fdefault:expr)? ),* $(,)?
        }
    ) => {
        $(#[$meta])*
        $vis struct $name {
            $( $(#[$fmeta])* $fvis $field: $fty ),*
        }

        wire_struct!(@$layout $name $(($tag))? { $( $field: $fty $(= $fdefault)? ),* });
    };
    (@array $name:ident $(($tag:expr))? { $( $field:ident: $fty:ty $(= $fdefault:expr)? ),* }) => {
        impl $crate::codec::Wire for $name {
            fn encode(&self, encoder: &mut $crate::msgpack::Encoder) {
                let tag_len: usize = 0 $( + { let _ = $tag; 1 } )?;
                encoder.array_len(tag_len $( + { let _ = stringify!($field); 1 } )*);
                $( encoder.str($tag); )?
                $( $crate::codec::Wire::encode(&self.$field, encoder); )*
            }

            fn decode(
                decoder: &mut $crate::msgpack::Decoder<'_>,
            ) -> $crate::error::Result<Self> {
                let mut fields = $crate::codec::ArrayFields::begin(decoder)?;
                $( fields.tag($tag)?; )?
                $(
                    let $field: $fty = match fields.next(stringify!($field))? {
                        Some(value) => value,
                        None => wire_struct!(@default $field $($fdefault)?),
                    };
                )*
                fields.finish()?;
                Ok(Self { $( $field ),* })
            }
        }
    };
    (@map $name:ident { $( $field:ident: $fty:ty $(= $fdefault:expr)? ),* }) => {
        impl $crate::codec::Wire for $name {
            fn encode(&self, encoder: &mut $crate::msgpack::Encoder) {
                encoder.map_len(0 $( + { let _ = stringify!($field); 1 } )*);
                $(
                    encoder.str(stringify!($field));
                    $crate::codec::Wire::encode(&self.$field, encoder);
                )*
            }

            fn decode(
                decoder: &mut $crate::msgpack::Decoder<'_>,
            ) -> $crate::error::Result<Self> {
                $( let mut $field: Option<$fty> = None; )*
                for _ in 0..decoder.map_len()? {
                    match decoder.str()? {
                        $(
                            stringify!($field) => {
                                $field = Some(
                                    $crate::codec::Wire::decode(decoder)
                                        .map_err(|error| error.in_field(stringify!($field)))?,
                                );
                            }
                        )*
                        _ => decoder.skip()?,
                    }
                }
                Ok(Self {
                    $(
                        $field: match $field {
                            Some(value) => value,
                            None => wire_struct!(@default $field $($fdefault)?),
                        },
                    )*
                })
            }
        }
    };
    (@default $field:ident) => {
        return Err($crate::error::Error::MissingField(stringify!($field)))
    };
    (@default $field:ident $default:expr) => {
        $default
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    wire_struct! {
        /// A tagged array struct with an appended defaulted field.
        #[derive(Debug, PartialEq)]
        struct Tagged: array("Tagged") {
            name: String,
            count: i64 = 5,
        }
    }

    wire_struct! {
        /// A map struct.
        #[derive(Debug, PartialEq)]
        struct Named: map + Default {
            x: i64 = 1,
            y: Option<String> = None,
        }
    }

    fn encode_value(write: impl FnOnce(&mut Encoder)) -> Vec<u8> {
        let mut encoder = Encoder::new();
        write(&mut encoder);
        encoder.into_single_frame()
    }

    #[test]
    fn short_arrays_take_defaults_and_long_arrays_drop_extras() {
        let short = encode_value(|e| {
            e.array_len(2);
            e.str("Tagged");
            e.str("a");
        });
        let decoded: Tagged = decode_bytes(&short).unwrap();
        assert_eq!(
            decoded,
            Tagged {
                name: "a".into(),
                count: 5
            }
        );

        let long = encode_value(|e| {
            e.array_len(4);
            e.str("Tagged");
            e.str("a");
            e.int(9);
            e.str("from a newer peer");
        });
        let decoded: Tagged = decode_bytes(&long).unwrap();
        assert_eq!(
            decoded,
            Tagged {
                name: "a".into(),
                count: 9
            }
        );
    }

    #[test]
    fn required_fields_and_tags_are_enforced() {
        let missing = encode_value(|e| {
            e.array_len(1);
            e.str("Tagged");
        });
        assert!(matches!(
            decode_bytes::<Tagged>(&missing),
            Err(Error::MissingField("name"))
        ));

        let wrong_tag = encode_value(|e| {
            e.array_len(2);
            e.str("Other");
            e.str("a");
        });
        assert!(matches!(
            decode_bytes::<Tagged>(&wrong_tag),
            Err(Error::Tag { .. })
        ));
    }

    #[test]
    fn map_structs_ignore_unknown_keys_and_default_missing_ones() {
        let bytes = encode_value(|e| {
            e.map_len(2);
            e.str("unknown");
            e.array_len(1);
            e.nil();
            e.str("y");
            e.str("v");
        });
        let decoded: Named = decode_bytes(&bytes).unwrap();
        assert_eq!(
            decoded,
            Named {
                x: 1,
                y: Some("v".into())
            }
        );

        let encoded = encode_frames(&Named::default());
        assert_eq!(&encoded[0][..], b"\x82\xa1x\x01\xa1y\xc0");
    }

    #[test]
    fn errors_name_the_field() {
        let bytes = encode_value(|e| {
            e.array_len(3);
            e.str("Tagged");
            e.str("a");
            e.str("not an int");
        });
        let error = decode_bytes::<Tagged>(&bytes).unwrap_err();
        assert!(error.to_string().starts_with("field `count`:"), "{error}");
    }
}