deser 0.9.1

An experimental serialization and deserialization library for Rust
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
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::sync::atomic::{self, AtomicUsize};

use deser::de::{DeserializeDriver, DeserializeOwned, Sink, SinkHandle};
use deser::{Atom, Deserialize, Event, State, Text, make_slot_wrapper};

fn deserialize<T: DeserializeOwned>(events: Vec<Event<'_>>) -> T {
    let mut out = None;
    {
        let mut driver = DeserializeDriver::new(&mut out);
        for event in events {
            driver.emit(event).unwrap();
        }
    }
    out.unwrap()
}

#[test]
fn test_floats() {
    // single precision floats are accepted by both float types
    assert_eq!(
        deserialize::<f32>(vec![Event::Atom(Atom::F32(0.1))]),
        0.1f32
    );
    assert_eq!(
        deserialize::<f64>(vec![Event::Atom(Atom::F32(0.1))]),
        f64::from(0.1f32)
    );
    assert_eq!(
        deserialize::<f32>(vec![Event::Atom(Atom::F64(0.5))]),
        0.5f32
    );
    assert_eq!(deserialize::<f32>(vec![0.25f32.into()]), 0.25f32);
}

#[test]
fn test_float_atom_paths() {
    use deser::ext::{ExtValue, Number};

    let number = Number::parse("1.25").unwrap();
    for (atom, expected) in [
        (Atom::U64(42), 42.0),
        (Atom::I64(-42), -42.0),
        (Atom::F32(0.25), 0.25),
        (Atom::F64(0.5), 0.5),
        (Atom::F64(f64::INFINITY), f64::INFINITY),
        (Atom::Lexical(Text::borrowed("1.5")), 1.5),
        (Atom::Lexical(Text::owned("-1.5")), -1.5),
        (Atom::Ext(ExtValue::owned(42u128)), 42.0),
        (Atom::Ext(ExtValue::owned(-42i128)), -42.0),
        (Atom::Ext(ExtValue::borrowed_value::<Number>(&number)), 1.25),
        (
            Atom::Ext(ExtValue::owned_value::<Number>(number.clone())),
            1.25,
        ),
    ] {
        // Exercise both the root sink and the inlined container atom path.
        assert_eq!(
            deserialize::<f32>(vec![atom.clone().into()]),
            expected as f32
        );
        assert_eq!(deserialize::<f64>(vec![atom.clone().into()]), expected);
        let events = vec![Event::seq_start(), atom.into(), Event::SeqEnd];
        assert_eq!(deserialize::<Vec<f32>>(events.clone()), [expected as f32]);
        assert_eq!(deserialize::<Vec<f64>>(events), [expected]);
    }
    for atom in [Atom::F32(-0.0), Atom::F64(-0.0)] {
        assert!(deserialize::<f32>(vec![atom.clone().into()]).is_sign_negative());
        assert!(deserialize::<f64>(vec![atom.into()]).is_sign_negative());
    }
    for atom in [Atom::F32(f32::NAN), Atom::F64(f64::NAN)] {
        assert!(deserialize::<f32>(vec![atom.clone().into()]).is_nan());
        assert!(deserialize::<f64>(vec![atom.into()]).is_nan());
    }

    for atom in [
        Atom::Lexical(Text::owned("not a float")),
        Atom::Str(Text::owned("1.5")),
        Atom::Bool(true),
        Atom::Null,
    ] {
        let mut out = None::<f32>;
        assert!(DeserializeDriver::new(&mut out).emit(atom.clone()).is_err());
        assert!(out.is_none());
        let mut out = None::<f64>;
        assert!(DeserializeDriver::new(&mut out).emit(atom).is_err());
        assert!(out.is_none());
    }
}

#[test]
fn test_f32_fallback() {
    // sinks that only know `F64` get single precision floats widened
    struct F64Only(f64);

    make_slot_wrapper!(SlotWrapper);

    impl<'de> Sink<'de> for SlotWrapper<F64Only> {
        fn atom(&mut self, atom: Atom, state: &mut deser::State) -> Result<(), deser::Error> {
            match atom {
                Atom::F64(value) => {
                    **self = Some(F64Only(value));
                    Ok(())
                }
                other => self.unexpected_atom(other, state),
            }
        }
    }

    impl<'de> Deserialize<'de> for F64Only {
        fn deserialize_into<'out>(
            out: &'out mut Option<Self>,
            _state: &mut State,
        ) -> SinkHandle<'out, 'de> {
            SlotWrapper::make_handle(out)
        }
    }

    let value = deserialize::<F64Only>(vec![Event::Atom(Atom::F32(1.5))]);
    assert_eq!(value.0, 1.5);

    // integers do not accept floats of either precision
    let mut out = None::<u32>;
    let mut driver = DeserializeDriver::new(&mut out);
    let err = driver.emit(Event::Atom(Atom::F32(1.0))).unwrap_err();
    assert_eq!(
        err.to_string(),
        "Unexpected: unexpected float, expected u32"
    );
}

#[test]
fn test_optional() {
    let mut out = None::<Option<usize>>;
    {
        let mut driver = DeserializeDriver::new(&mut out);
        driver.emit(Event::Atom(Atom::U64(42))).unwrap();
    }
    assert_eq!(out, Some(Some(42)));

    let mut out = None::<Option<usize>>;
    {
        let mut driver = DeserializeDriver::new(&mut out);
        driver.emit(Event::Atom(Atom::Null)).unwrap();
    }
    assert_eq!(out, Some(None));
}

#[test]
fn test_tuples() {
    let s: (u32, u32) = deserialize(vec![
        Event::seq_start(),
        1u64.into(),
        2u64.into(),
        Event::SeqEnd,
    ]);
    assert_eq!(s.0, 1);
    assert_eq!(s.1, 2);
}

#[test]
#[should_panic = "too many elements in tuple"]
fn test_tuples_too_many_elements() {
    let _: (u32, u32) = deserialize(vec![
        Event::seq_start(),
        1u64.into(),
        2u64.into(),
        "extra".into(),
        Event::SeqEnd,
    ]);
}

#[test]
#[should_panic = "not enough elements in tuple"]
fn test_tuples_not_enough_elements() {
    let _: (u32, u32) = deserialize(vec![Event::seq_start(), 1u64.into(), Event::SeqEnd]);
}

#[test]
fn test_array_basic() {
    let arr: [u16; 4] = deserialize(vec![
        Event::seq_start(),
        1u64.into(),
        2u64.into(),
        3u64.into(),
        4u64.into(),
        Event::SeqEnd,
    ]);
    assert_eq!(arr, [1, 2, 3, 4]);
}

#[test]
#[should_panic = "too many elements in array"]
fn test_array_too_many_elements() {
    let _: [u16; 4] = deserialize(vec![
        Event::seq_start(),
        1u64.into(),
        2u64.into(),
        3u64.into(),
        4u64.into(),
        5u64.into(),
        Event::SeqEnd,
    ]);
}

#[test]
#[should_panic = "not enough elements in array"]
fn test_array_not_enough_elements() {
    let _: [u16; 4] = deserialize(vec![
        Event::seq_start(),
        1u64.into(),
        2u64.into(),
        3u64.into(),
        Event::SeqEnd,
    ]);
}

#[test]
fn test_array_dropping_on_error() {
    // this is important since we're doing unsafe shit in the array serializer.  If stuff gets
    // wrong it needs to make sure drop is called.
    static DROP_COUNTER: AtomicUsize = AtomicUsize::new(0);

    struct X;

    impl Drop for X {
        fn drop(&mut self) {
            DROP_COUNTER.fetch_add(1, atomic::Ordering::Relaxed);
        }
    }

    make_slot_wrapper!(SlotWrapper);

    impl<'de> Deserialize<'de> for X {
        fn deserialize_into<'out>(
            out: &'out mut Option<Self>,
            _state: &mut State,
        ) -> SinkHandle<'out, 'de> {
            SlotWrapper::make_handle(out)
        }
    }

    impl<'de> Sink<'de> for SlotWrapper<X> {
        fn atom(&mut self, _atom: Atom, _state: &mut deser::State) -> Result<(), deser::Error> {
            **self = Some(X);
            Ok(())
        }
    }

    std::panic::catch_unwind(|| {
        let _: [X; 4] = deserialize(vec![
            Event::seq_start(),
            1u64.into(),
            2u64.into(),
            3u64.into(),
            Event::SeqEnd,
        ]);
    })
    .ok();

    assert_eq!(DROP_COUNTER.load(atomic::Ordering::Relaxed), 3);
}

#[test]
fn test_byte_array() {
    let x: [u8; 4] = deserialize(vec![
        Event::seq_start(),
        0u64.into(),
        1u64.into(),
        2u64.into(),
        3u64.into(),
        Event::SeqEnd,
    ]);
    assert_eq!(x, [0, 1, 2, 3]);

    let x: [u8; 4] = deserialize(vec![Event::Atom(Atom::Bytes(deser::Bytes::new(
        Cow::Borrowed(&b"\x00\x01\x02\x03"[..]),
    )))]);
    assert_eq!(x, [0, 1, 2, 3]);
}

#[test]
#[should_panic = "byte array of wrong length"]
fn test_byte_array_wrong_length() {
    let _: [u8; 4] = deserialize(vec![Event::Atom(Atom::Bytes(deser::Bytes::new(
        Cow::Borrowed(&b"012"[..]),
    )))]);
}

#[test]
fn test_chars() {
    let x: char = deserialize(vec!['x'.into()]);
    assert_eq!(x, 'x');
    let x: char = deserialize(vec!["x".into()]);
    assert_eq!(x, 'x');
}

#[test]
#[should_panic = "unexpected string, expected char"]
fn test_chars_long_string() {
    let _: char = deserialize(vec!["Harry".into()]);
}

#[test]
fn test_box() {
    let x: Box<u64> = deserialize(vec![0u64.into()]);
    assert_eq!(*x, 0);
}

#[test]
fn test_set() {
    let x: BTreeSet<String> = deserialize(vec![
        Event::seq_start(),
        "foo".into(),
        "bar".into(),
        Event::SeqEnd,
    ]);
    let mut set = BTreeSet::new();
    set.insert("foo".into());
    set.insert("bar".into());
    assert_eq!(x, set);
}

#[test]
fn test_string_lengths() {
    for len in 0..40 {
        for base in ["abcdefghij", "äöü日本"] {
            let s: String = base.chars().cycle().take(len).collect();
            let borrowed: String = deserialize(vec![Event::from(s.as_str())]);
            assert_eq!(borrowed, s);
            let owned: String = deserialize(vec![Event::from(s.clone())]);
            assert_eq!(owned, s);
        }
    }
}

/// Runs events through a driver, stops at the first error.
///
/// If `plain` is set, the root sink is wrapped in a sink that only forwards
/// the public methods, the elements of the sequence are then not built
/// inline.
fn run_events<T: DeserializeOwned + std::fmt::Debug>(
    events: &[Event<'static>],
    plain: bool,
    collect: bool,
) -> String {
    struct Plain<'a, 'de>(SinkHandle<'a, 'de>);

    impl<'de> Sink<'de> for Plain<'_, 'de> {
        fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), deser::Error> {
            self.0.atom(atom, state)
        }
        fn seq(&mut self, state: &mut State) -> Result<(), deser::Error> {
            self.0.seq(state)
        }
        fn map(&mut self, state: &mut State) -> Result<(), deser::Error> {
            self.0.map(state)
        }
        fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, deser::Error> {
            self.0.next_value(state)
        }
        fn finish(&mut self, state: &mut State) -> Result<(), deser::Error> {
            self.0.finish(state)
        }
        fn recover(&mut self, err: deser::Error, state: &mut State) -> Result<(), deser::Error> {
            self.0.recover(err, state)
        }
        fn expecting(&self) -> Cow<'_, str> {
            self.0.expecting()
        }
    }

    let mut out = None::<T>;
    let mut log = Vec::new();
    {
        let mut driver = DeserializeDriver::new(&mut out);
        driver.state_mut().set_collect_errors(collect);
        if plain {
            driver.wrap_sink(|sink, _| SinkHandle::heap(Plain(sink)));
        }
        for (idx, event) in events.iter().enumerate() {
            driver.state_mut().set_input_range(idx, idx + 1);
            if let Err(err) = driver.emit(event.clone()) {
                log.push(format!("{idx}: {:?} {err:#}", err.kind()));
                break;
            }
        }
    }
    log.push(format!("{out:?}"));
    log.join("\n")
}

#[test]
fn test_inline_elements() {
    fn seq(items: Vec<Event<'static>>) -> Vec<Event<'static>> {
        let mut rv = vec![Event::seq_start()];
        rv.extend(items);
        rv.push(Event::SeqEnd);
        rv
    }
    let pair = |a: Event<'static>, b: Event<'static>| seq(vec![a, b]);
    let ok = pair(1u64.into(), 2u64.into());
    let cases: Vec<Vec<Event<'static>>> = vec![
        vec![],
        ok.clone(),
        [ok.clone(), ok.clone()].concat(),
        // too many and not enough elements
        seq(vec![1u64.into(), 2u64.into(), 3u64.into()]),
        seq(vec![1u64.into()]),
        seq(vec![]),
        // atoms of the wrong type or out of range
        pair("x".into(), 2u64.into()),
        pair(1u64.into(), 300u64.into()),
        pair(Atom::Null.into(), 2u64.into()),
        pair(Atom::Lexical(Text::borrowed("7")).into(), 2u64.into()),
        // containers as items
        pair(seq(vec![1u64.into()])[0].clone(), 2u64.into()),
        seq(vec![
            Event::seq_start(),
            1u64.into(),
            Event::SeqEnd,
            2u64.into(),
        ]),
        seq(vec![
            Event::map_start(),
            "a".into(),
            1u64.into(),
            Event::MapEnd,
            2u64.into(),
        ]),
        seq(vec![
            1u64.into(),
            2u64.into(),
            Event::seq_start(),
            Event::SeqEnd,
        ]),
        // elements that are not sequences
        vec![Event::map_start(), Event::MapEnd],
        vec![Event::from(42u64)],
        vec![Event::from("x")],
        vec![Event::Atom(Atom::Null)],
    ];
    for case in cases {
        // the element is followed by a valid one, miri (which is slow) only
        // checks it between valid ones
        let wrapped = [
            seq([ok.clone(), case.clone(), ok.clone()].concat()),
            seq(case.clone()),
            seq([case.clone(), ok.clone()].concat()),
        ];
        for events in &wrapped[..if cfg!(miri) { 1 } else { 3 }] {
            for collect in [false, true] {
                let inline = run_events::<Vec<[u32; 2]>>(events, false, collect);
                let plain = run_events::<Vec<[u32; 2]>>(events, true, collect);
                assert_eq!(inline, plain, "{events:?}");
                let inline = run_events::<Vec<(u32, u8)>>(events, false, collect);
                let plain = run_events::<Vec<(u32, u8)>>(events, true, collect);
                assert_eq!(inline, plain, "{events:?}");
            }
        }
    }
    assert_eq!(
        run_events::<Vec<(u32, u8)>>(&seq([ok.clone(), ok.clone()].concat()), false, false),
        "Some([(1, 2), (1, 2)])"
    );
}