tbon 0.9.0

TinyChain Binary Object Notation is a compact and versatile stream-friendly binary serialization format
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Library for encoding Rust program data into a binary stream, and decoding that stream.
//!
//! Example:
//! ```
//! # use futures::executor::block_on;
//! let expected = ("one".to_string(), 2.0, vec![3, 4], vec![5u8]);
//! let stream = tbon::en::encode(&expected).unwrap();
//! let actual = block_on(tbon::de::try_decode((), stream)).unwrap();
//! assert_eq!(expected, actual);
//! ```
//!
//! Compatibility notes:
//!  - Decoding enforces a maximum nesting depth of 1024 by default; use
//!    `tbon::de::decode_with_max_depth`/`tbon::de::try_decode_with_max_depth` to override.

// `tbon` implements `destream`'s `async fn` trait APIs on stable Rust, so we keep this `allow`
// until `async_fn_in_trait` is stabilized.
#![allow(async_fn_in_trait)]

use element::Element;

mod constants;
mod element;

pub mod de;
pub mod en;

#[cfg(test)]
mod tests {
    #![allow(clippy::approx_constant)] // tests use explicit literals to validate encoding/decoding

    use std::collections::{
        BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque,
    };
    use std::fmt;
    use std::iter::FromIterator;
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
    use std::num::{NonZeroI128, NonZeroU128};
    use std::time::Duration;

    use bytes::Bytes;
    use destream::{FromStream, IntoStream};
    use futures::{future, stream, StreamExt, TryStreamExt};

    use rand::Rng;

    use super::constants::{
        Type, ARRAY_DELIMIT, ESCAPE, LIST_BEGIN, LIST_END, MAP_BEGIN, MAP_END, STRING_DELIMIT, TRUE,
    };
    use super::de::*;
    use super::en::*;
    use num_traits::Signed;
    use uuid::Uuid;

    async fn run_test<'en, T>(value: T)
    where
        T: FromStream<Context = ()> + IntoStream<'en> + fmt::Debug + PartialEq + Clone + 'en,
    {
        let encoded = encode(value.clone()).unwrap();
        let decoded: T = try_decode((), encoded).await.unwrap();
        assert_eq!(decoded, value);
    }

    async fn encode_to_vec<'en, T>(value: T) -> Vec<u8>
    where
        T: IntoStream<'en> + Clone + 'en,
    {
        encode(value)
            .unwrap()
            .try_fold(Vec::new(), |mut buffer, chunk| {
                buffer.extend_from_slice(&chunk);
                future::ready(Ok(buffer))
            })
            .await
            .unwrap()
    }

    async fn decode_from_chunks<T: FromStream<Context = ()>>(
        bytes: &[u8],
        chunk_size: usize,
    ) -> Result<T, super::de::Error> {
        let source = stream::iter(bytes.iter().copied())
            .chunks(chunk_size.max(1))
            .map(Bytes::from)
            .map(Result::<Bytes, super::en::Error>::Ok);

        try_decode((), source).await
    }

    async fn assert_decode_fails<'en, T, V>(value: V)
    where
        T: FromStream<Context = ()>,
        V: IntoStream<'en> + Clone + 'en,
    {
        let encoded = encode(value).unwrap();
        let result: Result<T, _> = try_decode((), encoded).await;
        assert!(result.is_err(), "expected decode to fail, but succeeded");
    }

    async fn assert_decode_bytes_fails<T: FromStream<Context = ()>>(bytes: &[u8]) {
        for chunk_size in 1..=bytes.len().clamp(1, 16) {
            let result: Result<T, _> = decode_from_chunks(bytes, chunk_size).await;
            assert!(result.is_err(), "expected decode to fail, but succeeded");
        }
    }

    #[tokio::test]
    async fn test_encode_buffered_equivalent() {
        let value = (true, -1i16, 3.14f64, "hello".to_string(), vec![1u8, 2, 3]);

        let baseline = encode_to_vec(value.clone()).await;
        let buffered_stream = super::en::encode_buffered(value, 1024).unwrap();
        let buffered: Vec<u8> = buffered_stream
            .try_fold(Vec::new(), |mut buffer, chunk| {
                buffer.extend_from_slice(&chunk);
                future::ready(Ok(buffer))
            })
            .await
            .unwrap();

        assert_eq!(baseline, buffered);
    }

    #[tokio::test]
    async fn test_encode_large_seq_no_stack_overflow() {
        let value: Vec<u64> = (0..100_000).map(|i| i as u64).collect();

        let encoded = encode_to_vec(value.clone()).await;
        assert!(!encoded.is_empty());
        assert_eq!(encoded[0], LIST_BEGIN[0]);
        assert_eq!(encoded[encoded.len() - 1], LIST_END[0]);
    }

    #[tokio::test]
    async fn test_encode_large_map_no_stack_overflow() {
        let value: BTreeMap<u64, u64> = (0..50_000_u64).map(|i| (i, i + 1)).collect();

        let encoded = encode_to_vec(value).await;
        assert!(!encoded.is_empty());
        assert_eq!(encoded[0], MAP_BEGIN[0]);
        assert_eq!(encoded[encoded.len() - 1], MAP_END[0]);
    }

    #[tokio::test]
    async fn test_decode_chunk_boundaries() {
        let value = (true, -1i16, 3.14f64, "hello".to_string(), vec![1u8, 2, 3]);

        let bytes = encode_to_vec(value.clone()).await;
        for chunk_size in 1..=bytes.len().clamp(1, 16) {
            let decoded: (bool, i16, f64, String, Vec<u8>) =
                decode_from_chunks(&bytes, chunk_size).await.unwrap();
            assert_eq!(decoded, value);
        }
    }

    #[tokio::test]
    async fn test_decode_chunk_boundaries_with_escapes() {
        // Ensure escapes spanning chunk boundaries are handled:
        // - Strings escape `"` and `\\`
        // - Byte arrays escape `=` (array delimiter) and `\\`
        let value = (
            "this is a \"string\" within a \\ string".to_string(),
            Bytes::from(vec![b'=', b'\\', 1u8, 2u8, b'=', b'\\']),
        );

        let bytes = encode_to_vec(value.clone()).await;
        for chunk_size in 1..=bytes.len().clamp(1, 16) {
            let decoded: (String, Bytes) = decode_from_chunks(&bytes, chunk_size).await.unwrap();
            assert_eq!(decoded, value);
        }
    }

    #[tokio::test]
    async fn test_truncated_escape_fails() {
        // string: '"' '\' EOF
        assert_decode_bytes_fails::<String>(&[STRING_DELIMIT[0], ESCAPE[0]]).await;

        // bytes: '=' <dtype> '\' EOF
        assert_decode_bytes_fails::<Vec<u8>>(&[ARRAY_DELIMIT[0], Type::U8 as u8, ESCAPE[0]]).await;
    }

    #[tokio::test]
    async fn test_unterminated_string_fails() {
        assert_decode_bytes_fails::<String>(&[STRING_DELIMIT[0], b'a', b'b']).await;
    }

    #[tokio::test]
    async fn test_invalid_utf8_string_fails() {
        assert_decode_bytes_fails::<String>(&[STRING_DELIMIT[0], 0xFF, STRING_DELIMIT[0]]).await;
    }

    #[tokio::test]
    async fn test_malformed_arrays_fail() {
        // unknown array dtype
        struct Any;
        struct AnyVisitor;

        impl destream::de::Visitor for AnyVisitor {
            type Value = Any;

            fn expecting() -> &'static str {
                "any TBON value"
            }

            fn visit_unit<E: destream::de::Error>(self) -> Result<Self::Value, E> {
                Ok(Any)
            }
        }

        impl FromStream for Any {
            type Context = ();

            async fn from_stream<D: destream::de::Decoder>(
                _: (),
                decoder: &mut D,
            ) -> Result<Self, D::Error> {
                decoder.decode_any(AnyVisitor).await
            }
        }

        assert_decode_bytes_fails::<Any>(&[ARRAY_DELIMIT[0], 0xFF]).await;

        // missing array end delimiter
        assert_decode_bytes_fails::<Vec<u8>>(&[ARRAY_DELIMIT[0], Type::U8 as u8, 1, 2, 3]).await;
    }

    #[tokio::test]
    async fn test_trailing_bytes_fail() {
        let bytes = [Type::Bool as u8, TRUE[0], 0xFF, 0xFF];
        let result: Result<bool, _> = decode_from_chunks(&bytes, 1).await;
        assert!(result.is_err(), "expected trailing bytes to cause an error");
    }

    #[tokio::test]
    async fn test_ignored_any_consumes_nested_values() {
        // IgnoredAny must be able to consume nested values, including arrays (Bytes) and strings.
        let value = (
            HashMap::<String, Vec<u8>>::from_iter([(
                "a".to_string(),
                vec![1u8, 2, 3, b'=', b'\\'],
            )]),
            vec![
                "hello".to_string(),
                "this is a \"string\" within a \\ string".to_string(),
            ],
            Bytes::from(vec![b'=', b'\\', 0, 1, 2, b'=', b'\\']),
            HashMap::<String, HashMap<String, Vec<u8>>>::from_iter([(
                "nested".to_string(),
                HashMap::from_iter([("k".to_string(), vec![9u8, 8, 7])]),
            )]),
        );

        let encoded = encode(&value).unwrap();
        let _: destream::IgnoredAny = try_decode((), encoded).await.unwrap();
    }

    #[tokio::test]
    async fn test_decode_ignored_any_deep_nesting() {
        // IgnoredAny must be able to consume deep nesting without recursion.
        const DEPTH: usize = 2048;
        let mut bytes = Vec::with_capacity(DEPTH * 2);
        bytes.extend(std::iter::repeat_n(LIST_BEGIN[0], DEPTH));
        bytes.extend(std::iter::repeat_n(LIST_END[0], DEPTH));

        let source = stream::iter(bytes.iter().copied())
            .chunks(1)
            .map(Bytes::from)
            .map(Result::<Bytes, super::en::Error>::Ok);

        let _: destream::IgnoredAny = super::de::try_decode_with_max_depth((), source, DEPTH + 1)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_decode_reject_too_deep_nesting() {
        const DEPTH: usize = 1025;
        let mut bytes = Vec::with_capacity(DEPTH * 2);
        bytes.extend(std::iter::repeat_n(LIST_BEGIN[0], DEPTH));
        bytes.extend(std::iter::repeat_n(LIST_END[0], DEPTH));

        let result: Result<destream::IgnoredAny, _> = decode_from_chunks(&bytes, 1).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_truncated_streams_fail() {
        let value = (HashMap::<String, u64>::from_iter([
            ("a".into(), 1),
            ("b".into(), 2),
        ]),);
        let bytes = encode_to_vec(value.clone()).await;

        // Determine the minimum prefix length required to decode successfully (TBON decoders are
        // allowed to stop after reading a single value).
        let mut min_success = None;
        for i in 0..=bytes.len() {
            let result: Result<(HashMap<String, u64>,), _> =
                decode_from_chunks(&bytes[..i], 1).await;
            if result.as_ref().is_ok_and(|decoded| decoded == &value) {
                min_success = Some(i);
                break;
            }
        }

        let min_success = min_success.expect("expected at least one successful decode");
        for i in 0..min_success {
            for chunk_size in [1usize, 2, 3, 7] {
                let result: Result<(HashMap<String, u64>,), _> =
                    decode_from_chunks(&bytes[..i], chunk_size).await;
                assert!(result.is_err(), "expected truncation to fail at {i}");
            }
        }
    }

    #[tokio::test]
    async fn test_corrupt_stream_fails() {
        let value = ("hello".to_string(), vec![1u8, 2, 3]);
        let mut bytes = encode_to_vec(value).await;
        if let Some(first) = bytes.first_mut() {
            *first = 0xFF;
        }

        let result: Result<(String, Vec<u8>), _> = decode_from_chunks(&bytes, 3).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_default_impl_roundtrips() {
        run_test(()).await;

        run_test(Some("hello".to_string())).await;
        run_test::<Option<String>>(None).await;

        run_test(VecDeque::from([1u16, 2, 3, 4])).await;
        run_test(LinkedList::from([1i8, 2, 3, 4])).await;

        run_test(BTreeSet::from([1u8, 2, 3])).await;
        run_test(HashSet::from(["a".to_string(), "b".to_string()])).await;

        run_test(BTreeMap::from_iter([
            (1u64, "one".to_string()),
            (2u64, "two".to_string()),
        ]))
        .await;
        run_test(HashMap::<i32, String>::from_iter([
            (-1i32, "one".to_string()),
            (2i32, "two".to_string()),
        ]))
        .await;

        let array = [1u8, 2, 3, 4, 5, 6, 7, 8];
        let array_ref: &[u8; 8] = &array;
        let encoded = encode(&array_ref).unwrap();
        let decoded: [u8; 8] = try_decode((), encoded).await.unwrap();
        assert_eq!(decoded, array);
        run_test((1u8, 2u16, 3u32, 4u64)).await;

        // BinaryHeap doesn't implement PartialEq; compare its sorted contents.
        let heap: BinaryHeap<i32> = BinaryHeap::from([3, 1, 2, 5, 4]);
        let encoded = encode(&heap).unwrap();
        let decoded: BinaryHeap<i32> = try_decode((), encoded).await.unwrap();
        assert_eq!(heap.clone().into_sorted_vec(), decoded.into_sorted_vec());

        // IgnoredAny must be able to consume any TBON value.
        let map: HashMap<String, Vec<u8>> =
            HashMap::from_iter([("a".to_string(), vec![1u8, 2, 3])]);
        let tuple = (map,);
        let encoded = encode(&tuple).unwrap();
        let _: destream::IgnoredAny = try_decode((), encoded).await.unwrap();

        run_test(i128::MAX).await;
        run_test(u128::MAX).await;
        run_test(NonZeroI128::new(-5_i128).unwrap()).await;
        run_test(NonZeroU128::new(5_u128).unwrap()).await;

        run_test(Duration::new(5, 7)).await;

        run_test(Ipv4Addr::new(127, 0, 0, 1)).await;
        run_test(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)).await;
        run_test(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))).await;
        run_test(SocketAddr::from((Ipv4Addr::new(127, 0, 0, 1), 80))).await;
    }

    #[tokio::test]
    async fn test_extended_default_impl_numeric_tokens() {
        let encoded = encode(123_i64).unwrap();
        let decoded: i128 = try_decode((), encoded).await.unwrap();
        assert_eq!(decoded, 123_i128);

        let encoded = encode(123_u64).unwrap();
        let decoded: u128 = try_decode((), encoded).await.unwrap();
        assert_eq!(decoded, 123_u128);

        let encoded = encode(i64::MAX).unwrap();
        let decoded: i128 = try_decode((), encoded).await.unwrap();
        assert_eq!(decoded, i64::MAX as i128);

        let encoded = encode(u64::MAX).unwrap();
        let decoded: u128 = try_decode((), encoded).await.unwrap();
        assert_eq!(decoded, u64::MAX as u128);
    }

    #[tokio::test]
    async fn test_extended_default_impl_decode_errors() {
        assert_decode_fails::<u128, _>(-1_i64).await;
        assert_decode_fails::<NonZeroU128, _>(0_u64).await;
        assert_decode_fails::<NonZeroI128, _>(0_i64).await;

        assert_decode_fails::<u128, _>(format!("{}0", u128::MAX)).await;
        assert_decode_fails::<i128, _>(format!("{}0", i128::MAX)).await;

        assert_decode_fails::<Duration, _>((5_u64,)).await;
        assert_decode_fails::<Duration, _>((5_u64, "7".to_string())).await;
        assert_decode_fails::<Duration, _>((5_u64, 1_000_000_000_u32)).await;

        assert_decode_fails::<Ipv4Addr, _>("999.0.0.1").await;
        assert_decode_fails::<Ipv6Addr, _>("not an ip").await;
        assert_decode_fails::<IpAddr, _>("not an ip").await;
        assert_decode_fails::<SocketAddr, _>("127.0.0.1").await;
    }

    #[tokio::test]
    async fn test_primitives() {
        run_test(true).await;
        run_test(false).await;

        for u in 0..66000u64 {
            run_test(u).await;
        }

        for i in -66000..66000i64 {
            run_test(i).await;
        }

        for _ in 0..100000 {
            let f: f32 = rand::rng().random();
            run_test(f).await;
        }
    }

    #[tokio::test]
    async fn test_undefined_numbers() {
        async fn recode<'en, T>(value: T) -> T
        where
            T: FromStream<Context = ()> + IntoStream<'en> + fmt::Debug + PartialEq + Clone + 'en,
        {
            let encoded = encode(value.clone()).unwrap();
            try_decode((), encoded).await.unwrap()
        }

        assert!(recode(f32::NAN).await.is_nan());

        let inf = recode(f32::INFINITY).await;
        assert!(inf.is_infinite() && inf.is_positive());

        let inf = recode(f32::NEG_INFINITY).await;
        assert!(inf.is_infinite() && inf.is_negative());

        assert!(recode(f64::NAN).await.is_nan());

        let inf = recode(f64::INFINITY).await;
        assert!(inf.is_infinite() && inf.is_sign_positive());

        let inf = recode(f64::NEG_INFINITY).await;
        assert!(inf.is_infinite() && inf.is_sign_negative());
    }

    #[tokio::test]
    async fn test_strings() {
        run_test(String::from("hello world")).await;
        run_test(String::from("Привет, мир")).await;
        run_test(String::from("this is a \"string\" within a \\ string")).await;
        run_test(String::from("this \"string\" is \\\terminated by a \\")).await;
    }

    #[tokio::test]
    async fn test_compound() {
        let list = vec![String::from("hello"), String::from("world")];
        run_test(list).await;

        let mut map = HashMap::new();
        map.insert(-1i32, String::from("I'm a teapot"));
        map.insert(-1i32, String::from("\' \"\"     "));
        run_test(map).await;

        let mut map = HashMap::new();
        map.insert("one".to_string(), HashMap::new());
        map.insert(
            "two".to_string(),
            HashMap::from_iter(vec![("three".to_string(), 4f32)]),
        );
        run_test(map).await;
    }

    #[tokio::test]
    async fn test_tuple() {
        let tuple: (Vec<u8>, HashMap<u64, String>) = (Vec::new(), HashMap::new());
        run_test(tuple).await;

        let tuple = (
            true,
            -1i16,
            3.14,
            String::from(" hello \"world\""),
            (0..255).collect::<Vec<u8>>(),
        );
        run_test(tuple).await;

        let tuple: (bool, Vec<String>, Option<String>, Vec<String>, bool) =
            (true, vec![], None, vec![], false);

        run_test(tuple).await;

        let tuple: (Bytes, BTreeMap<u64, String>) = (vec![1, 2, 3].into(), BTreeMap::new());
        run_test(tuple).await;

        let one = Bytes::from(vec![1, 2, 3]);
        let two = BTreeMap::new();
        let tuple: (Bytes, BTreeMap<u64, String>) = (one.clone(), two.clone());

        let encoded = encode(tuple).unwrap();
        let decoded: (Bytes, BTreeMap<u64, String>) = try_decode((), encoded).await.unwrap();
        assert_eq!(decoded, (one, two));
    }

    #[tokio::test]
    async fn test_array() {
        #[derive(Eq, PartialEq)]
        struct TestArray {
            data: Vec<i16>,
        }

        struct TestVisitor;

        impl destream::de::Visitor for TestVisitor {
            type Value = TestArray;

            fn expecting() -> &'static str {
                "a TestArray"
            }

            async fn visit_array_i16<A: destream::de::ArrayAccess<i16>>(
                self,
                mut array: A,
            ) -> Result<Self::Value, A::Error> {
                let mut data = Vec::with_capacity(3);
                let mut buffer = [0; 100];
                loop {
                    let num_items = array.buffer(&mut buffer).await?;
                    if num_items > 0 {
                        data.extend(&buffer[..num_items]);
                    } else {
                        break;
                    }
                }

                Ok(TestArray { data })
            }
        }

        impl FromStream for TestArray {
            type Context = ();

            async fn from_stream<D: destream::de::Decoder>(
                _: (),
                decoder: &mut D,
            ) -> Result<Self, D::Error> {
                decoder.decode_array_i16(TestVisitor).await
            }
        }

        impl<'en> destream::en::ToStream<'en> for TestArray {
            fn to_stream<E: destream::en::Encoder<'en>>(
                &'en self,
                encoder: E,
            ) -> Result<E::Ok, E::Error> {
                encoder.encode_array_i16(futures::stream::once(future::ready(
                    self.data.iter().copied(),
                )))
            }
        }

        impl fmt::Debug for TestArray {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                fmt::Debug::fmt(&self.data, f)
            }
        }

        let test = TestArray {
            data: (0..512).collect(),
        };

        let mut encoded = encode(&test).unwrap();
        let mut buf = Vec::new();
        while let Some(chunk) = encoded.try_next().await.unwrap() {
            buf.extend_from_slice(&chunk);
        }

        let decoded: TestArray = try_decode((), encode(&test).unwrap()).await.unwrap();
        assert_eq!(test, decoded);
    }

    #[tokio::test]
    async fn test_bytes() {
        run_test(Bytes::from(vec![1, 2, 3])).await;
    }

    #[tokio::test]
    async fn test_uuid() {
        run_test(Uuid::from_bytes([0u8; 16])).await;
    }
}