xmachines-codec-msgpack 1.1.0

MessagePack events and snapshots for xmachines-rs machines (an xmachines-codec 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
//! A small MessagePack writer for snapshots: a serde `Serializer` into a
//! `Vec<u8>` that cannot fail, for JSON-shaped data (maps with string keys,
//! sequences, strings, numbers, booleans, null). Records are read back with
//! `rmp_serde`; the encoding is the one `rmp_serde` would choose for the
//! same data (smallest integer and length markers, `f64` floats, maps with
//! string keys), so either can read the other's output.
//!
//! It exists because snapshots are written once per changed actor per
//! transaction, and `rmp_serde`'s general-purpose writer (fallible writes,
//! configuration, unknown-length buffering) cost several times the encoding
//! itself.

use std::fmt;

use serde::ser::{self, Serialize};

/// Writes `value` as MessagePack, appending to `out`.
pub fn write<T: Serialize + ?Sized>(out: &mut Vec<u8>, value: &T) -> Result<(), Error> {
    value.serialize(&mut Writer { out })
}

/// The only failure: a sequence or map whose length was not given up front,
/// or data that has no MessagePack form here.
#[derive(Debug)]
pub struct Error(String);

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

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

impl ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error(msg.to_string())
    }
}

struct Writer<'a> {
    out: &'a mut Vec<u8>,
}

impl Writer<'_> {
    fn uint(&mut self, n: u64) {
        let out = &mut *self.out;
        if n < 0x80 {
            out.push(n as u8);
        } else if n <= u8::MAX as u64 {
            out.extend_from_slice(&[0xcc, n as u8]);
        } else if n <= u16::MAX as u64 {
            out.push(0xcd);
            out.extend_from_slice(&(n as u16).to_be_bytes());
        } else if n <= u32::MAX as u64 {
            out.push(0xce);
            out.extend_from_slice(&(n as u32).to_be_bytes());
        } else {
            out.push(0xcf);
            out.extend_from_slice(&n.to_be_bytes());
        }
    }

    fn int(&mut self, n: i64) {
        if n >= 0 {
            return self.uint(n as u64);
        }
        let out = &mut *self.out;
        if n >= -32 {
            out.push(n as i8 as u8);
        } else if n >= i8::MIN as i64 {
            out.extend_from_slice(&[0xd0, n as i8 as u8]);
        } else if n >= i16::MIN as i64 {
            out.push(0xd1);
            out.extend_from_slice(&(n as i16).to_be_bytes());
        } else if n >= i32::MIN as i64 {
            out.push(0xd2);
            out.extend_from_slice(&(n as i32).to_be_bytes());
        } else {
            out.push(0xd3);
            out.extend_from_slice(&n.to_be_bytes());
        }
    }

    fn str(&mut self, s: &str) {
        let len = s.len();
        let out = &mut *self.out;
        if len < 32 {
            out.push(0xa0 | len as u8);
        } else if len <= u8::MAX as usize {
            out.extend_from_slice(&[0xd9, len as u8]);
        } else if len <= u16::MAX as usize {
            out.push(0xda);
            out.extend_from_slice(&(len as u16).to_be_bytes());
        } else {
            out.push(0xdb);
            out.extend_from_slice(&(len as u32).to_be_bytes());
        }
        out.extend_from_slice(s.as_bytes());
    }

    fn bin(&mut self, b: &[u8]) {
        let len = b.len();
        let out = &mut *self.out;
        if len <= u8::MAX as usize {
            out.extend_from_slice(&[0xc4, len as u8]);
        } else if len <= u16::MAX as usize {
            out.push(0xc5);
            out.extend_from_slice(&(len as u16).to_be_bytes());
        } else {
            out.push(0xc6);
            out.extend_from_slice(&(len as u32).to_be_bytes());
        }
        out.extend_from_slice(b);
    }

    fn array_len(&mut self, len: usize) {
        let out = &mut *self.out;
        if len < 16 {
            out.push(0x90 | len as u8);
        } else if len <= u16::MAX as usize {
            out.push(0xdc);
            out.extend_from_slice(&(len as u16).to_be_bytes());
        } else {
            out.push(0xdd);
            out.extend_from_slice(&(len as u32).to_be_bytes());
        }
    }

    fn map_len(&mut self, len: usize) {
        let out = &mut *self.out;
        if len < 16 {
            out.push(0x80 | len as u8);
        } else if len <= u16::MAX as usize {
            out.push(0xde);
            out.extend_from_slice(&(len as u16).to_be_bytes());
        } else {
            out.push(0xdf);
            out.extend_from_slice(&(len as u32).to_be_bytes());
        }
    }
}

fn unknown_length() -> Error {
    Error("a sequence or map of unknown length".into())
}

impl<'a, 'b> ser::Serializer for &'b mut Writer<'a> {
    type Ok = ();
    type Error = Error;
    type SerializeSeq = Self;
    type SerializeTuple = Self;
    type SerializeTupleStruct = Self;
    type SerializeTupleVariant = Self;
    type SerializeMap = Self;
    type SerializeStruct = Self;
    type SerializeStructVariant = Self;

    fn serialize_bool(self, v: bool) -> Result<(), Error> {
        self.out.push(if v { 0xc3 } else { 0xc2 });
        Ok(())
    }
    fn serialize_i8(self, v: i8) -> Result<(), Error> {
        self.int(v.into());
        Ok(())
    }
    fn serialize_i16(self, v: i16) -> Result<(), Error> {
        self.int(v.into());
        Ok(())
    }
    fn serialize_i32(self, v: i32) -> Result<(), Error> {
        self.int(v.into());
        Ok(())
    }
    fn serialize_i64(self, v: i64) -> Result<(), Error> {
        self.int(v);
        Ok(())
    }
    fn serialize_u8(self, v: u8) -> Result<(), Error> {
        self.uint(v.into());
        Ok(())
    }
    fn serialize_u16(self, v: u16) -> Result<(), Error> {
        self.uint(v.into());
        Ok(())
    }
    fn serialize_u32(self, v: u32) -> Result<(), Error> {
        self.uint(v.into());
        Ok(())
    }
    fn serialize_u64(self, v: u64) -> Result<(), Error> {
        self.uint(v);
        Ok(())
    }
    fn serialize_f32(self, v: f32) -> Result<(), Error> {
        self.out.push(0xca);
        self.out.extend_from_slice(&v.to_be_bytes());
        Ok(())
    }
    fn serialize_f64(self, v: f64) -> Result<(), Error> {
        self.out.push(0xcb);
        self.out.extend_from_slice(&v.to_be_bytes());
        Ok(())
    }
    fn serialize_char(self, v: char) -> Result<(), Error> {
        self.str(v.encode_utf8(&mut [0; 4]));
        Ok(())
    }
    fn serialize_str(self, v: &str) -> Result<(), Error> {
        self.str(v);
        Ok(())
    }
    fn serialize_bytes(self, v: &[u8]) -> Result<(), Error> {
        self.bin(v);
        Ok(())
    }
    fn serialize_none(self) -> Result<(), Error> {
        self.out.push(0xc0);
        Ok(())
    }
    fn serialize_some<T: Serialize + ?Sized>(self, value: &T) -> Result<(), Error> {
        value.serialize(self)
    }
    fn serialize_unit(self) -> Result<(), Error> {
        self.out.push(0xc0);
        Ok(())
    }
    fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Error> {
        self.serialize_unit()
    }
    fn serialize_unit_variant(self, _name: &'static str, _index: u32, variant: &'static str) -> Result<(), Error> {
        self.str(variant);
        Ok(())
    }
    fn serialize_newtype_struct<T: Serialize + ?Sized>(self, _name: &'static str, value: &T) -> Result<(), Error> {
        value.serialize(self)
    }
    fn serialize_newtype_variant<T: Serialize + ?Sized>(
        self,
        _name: &'static str,
        _index: u32,
        variant: &'static str,
        value: &T,
    ) -> Result<(), Error> {
        self.map_len(1);
        self.str(variant);
        value.serialize(self)
    }
    fn serialize_seq(self, len: Option<usize>) -> Result<Self, Error> {
        self.array_len(len.ok_or_else(unknown_length)?);
        Ok(self)
    }
    fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
        self.array_len(len);
        Ok(self)
    }
    fn serialize_tuple_struct(self, _name: &'static str, len: usize) -> Result<Self, Error> {
        self.array_len(len);
        Ok(self)
    }
    fn serialize_tuple_variant(
        self,
        _name: &'static str,
        _index: u32,
        variant: &'static str,
        len: usize,
    ) -> Result<Self, Error> {
        self.map_len(1);
        self.str(variant);
        self.array_len(len);
        Ok(self)
    }
    fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
        self.map_len(len.ok_or_else(unknown_length)?);
        Ok(self)
    }
    fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self, Error> {
        self.map_len(len);
        Ok(self)
    }
    fn serialize_struct_variant(
        self,
        _name: &'static str,
        _index: u32,
        variant: &'static str,
        len: usize,
    ) -> Result<Self, Error> {
        self.map_len(1);
        self.str(variant);
        self.map_len(len);
        Ok(self)
    }
    fn collect_str<T: fmt::Display + ?Sized>(self, value: &T) -> Result<(), Error> {
        self.str(&value.to_string());
        Ok(())
    }
}

impl ser::SerializeSeq for &mut Writer<'_> {
    type Ok = ();
    type Error = Error;
    fn serialize_element<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
        value.serialize(&mut **self)
    }
    fn end(self) -> Result<(), Error> {
        Ok(())
    }
}

impl ser::SerializeTuple for &mut Writer<'_> {
    type Ok = ();
    type Error = Error;
    fn serialize_element<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
        value.serialize(&mut **self)
    }
    fn end(self) -> Result<(), Error> {
        Ok(())
    }
}

impl ser::SerializeTupleStruct for &mut Writer<'_> {
    type Ok = ();
    type Error = Error;
    fn serialize_field<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
        value.serialize(&mut **self)
    }
    fn end(self) -> Result<(), Error> {
        Ok(())
    }
}

impl ser::SerializeTupleVariant for &mut Writer<'_> {
    type Ok = ();
    type Error = Error;
    fn serialize_field<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
        value.serialize(&mut **self)
    }
    fn end(self) -> Result<(), Error> {
        Ok(())
    }
}

impl ser::SerializeMap for &mut Writer<'_> {
    type Ok = ();
    type Error = Error;
    fn serialize_key<T: Serialize + ?Sized>(&mut self, key: &T) -> Result<(), Error> {
        key.serialize(&mut **self)
    }
    fn serialize_value<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
        value.serialize(&mut **self)
    }
    fn end(self) -> Result<(), Error> {
        Ok(())
    }
}

impl ser::SerializeStruct for &mut Writer<'_> {
    type Ok = ();
    type Error = Error;
    fn serialize_field<T: Serialize + ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error> {
        self.str(key);
        value.serialize(&mut **self)
    }
    fn end(self) -> Result<(), Error> {
        Ok(())
    }
}

impl ser::SerializeStructVariant for &mut Writer<'_> {
    type Ok = ();
    type Error = Error;
    fn serialize_field<T: Serialize + ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error> {
        self.str(key);
        value.serialize(&mut **self)
    }
    fn end(self) -> Result<(), Error> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{json, Value};

    /// The same bytes as `rmp_serde` (named maps), and they decode back.
    fn check(v: &Value) {
        let mut ours = Vec::new();
        super::write(&mut ours, v).unwrap();
        let mut theirs = Vec::new();
        rmp_serde::encode::write_named(&mut theirs, v).unwrap();
        assert_eq!(ours, theirs, "{v}");
        let back: Value = rmp_serde::from_slice(&ours).unwrap();
        assert_eq!(back.to_string(), v.to_string());
    }

    #[test]
    fn matches_rmp_serde() {
        let long = |n: usize| "x".repeat(n);
        let ints: Vec<Value> = [
            0i64,
            1,
            127,
            128,
            255,
            256,
            65_535,
            65_536,
            4_294_967_295,
            4_294_967_296,
            -1,
            -32,
            -33,
            -128,
            -129,
            -32_768,
            -32_769,
            -2_147_483_648,
            -2_147_483_649,
            i64::MIN,
            i64::MAX,
        ]
        .iter()
        .map(|&n| json!(n))
        .collect();
        for v in ints {
            check(&v);
        }
        check(&json!(u64::MAX));
        for v in [json!(1.5), json!(-0.25), json!(1e300), json!(null), json!(true), json!(false), json!("ü€")] {
            check(&v);
        }
        for n in [0, 31, 32, 255, 256, 65_535, 65_536] {
            check(&json!(long(n)));
        }
        for n in [0, 15, 16, 65_535, 65_536] {
            check(&Value::Array((0..n).map(|i| json!(i % 3)).collect()));
            check(&Value::Object((0..n).map(|i| (format!("k{i}"), json!(i))).collect()));
        }
        check(&json!({ "z": [1, { "a": null, "b": [true, 2.5, "s"] }], "a": { "": {} } }));
    }
}