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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! A minimal (like seriously), zero dependency protobuf encoder.
//!
//! ## Non goals
//! - ~~Decoding~~ (Upcoming in <https://github.com/noislabs/anybuf/pull/2>)
//! - protobuf 2 things
//! - Field sorting
//! - Groups support (deprecated, see <https://protobuf.dev/programming-guides/proto2/#groups>)
//!
//! Supported:
//! - Varint fields (bool/uint32/uint64/sint32/sint64)
//! - Variable length fields (string/bytes)
//! - Repeated (bool/uint32/uint64/string/bytes/messages)
//! - Nested: Just append an `Anybuf` instance
//!
//! Not yet supported:
//!
//! - Fixed length types
//! - Packed encoding for repeated fields
//! - int32/int64

mod varint;

use varint::{to_zigzag32, to_zigzag64};

#[derive(Default)]
pub struct Anybuf {
    output: Vec<u8>,
}

/// The protobuf wire types
///
/// <https://protobuf.dev/programming-guides/encoding/#structure>
#[repr(u32)]
enum WireType {
    /// Variable length field (int32, int64, uint32, uint64, sint32, sint64, bool, enum)
    Varint = 0,
    // Fixed length types unsupported
    // I64 = 1,
    /// Lengths prefixed field (string, bytes, embedded messages, packed repeated fields)
    Len = 2,
    // group start/end (deprecated, unsupported)
    // SGROUP = 3,
    // EGROUP = 4,
    // Fixed length types unsupported
    // I32 = 5,
}

/// Encodes the unsigned integer `n` using the protobuf varint (variable integer)
/// format.
fn unsigned_varint_encode(mut n: u64, dest: &mut Vec<u8>) {
    let mut buf = [0u8; 10];
    let mut len = 0;
    loop {
        // Read least significant 7 bits
        let mut b = (n & 0b0111_1111) as u8;
        n >>= 7;
        // Set top bit when not yet done
        if n != 0 {
            b |= 0b1000_0000;
        }
        buf[len] = b;
        len += 1;
        if n == 0 {
            break;
        }
    }
    dest.extend_from_slice(&buf[0..len]);
}

impl Anybuf {
    /// Creates a new serializer.
    pub fn new() -> Self {
        Self::default()
    }

    /// Appends a bytes field with the given field number.
    pub fn append_bytes(mut self, field_number: u32, data: impl AsRef<[u8]>) -> Self {
        let data = data.as_ref();
        if data.is_empty() {
            return self;
        }
        // tag
        self.append_tag(field_number, WireType::Len);
        // length
        unsigned_varint_encode(data.len() as u64, &mut self.output);
        // value
        self.output.extend_from_slice(data);
        self
    }

    /// Appends a string field with the given field number.
    #[inline]
    pub fn append_string(self, field_number: u32, data: impl AsRef<str>) -> Self {
        self.append_bytes(field_number, data.as_ref().as_bytes())
    }

    /// Appends a uint64 field with the given field number.
    pub fn append_uint64(mut self, field_number: u32, value: u64) -> Self {
        if value == 0 {
            return self;
        }
        self.append_tag(field_number, WireType::Varint);
        unsigned_varint_encode(value, &mut self.output);
        self
    }

    /// Appends a uint32 field with the given field number.
    #[inline]
    pub fn append_uint32(self, field_number: u32, value: u32) -> Self {
        self.append_uint64(field_number, value.into())
    }

    /// Appends a bool field with the given field number.
    #[inline]
    pub fn append_bool(self, field_number: u32, value: bool) -> Self {
        self.append_uint64(field_number, value.into())
    }

    /// Appends an sint32 field with the given field number.
    ///
    /// Please note that protobuf has two different 32 bit signed integer types
    /// with different encodings: sint32 and int32. This only works for the former.
    pub fn append_sint32(self, field_number: u32, value: i32) -> Self {
        self.append_uint32(field_number, to_zigzag32(value))
    }

    /// Appends an sint64 field with the given field number.
    ///
    /// Please note that protobuf has two different 64 bit signed integer types
    /// with different encodings: sint64 and int64. This only works for the former.
    pub fn append_sint64(self, field_number: u32, value: i64) -> Self {
        self.append_uint64(field_number, to_zigzag64(value))
    }

    /// Appends a nested protobuf message with the given field number.
    pub fn append_message(self, field_number: u32, value: &Anybuf) -> Self {
        self.append_bytes(field_number, value.as_bytes())
    }

    /// Appends a repeated field of type uint32.
    ///
    /// Use this instead of multiple [`Anybuf::append_uint32`] to ensure 0 values are not lost.
    pub fn append_repeated_uint32(mut self, field_number: u32, data: &[u32]) -> Self {
        for value in data {
            self.append_tag(field_number, WireType::Varint);
            unsigned_varint_encode((*value).into(), &mut self.output);
        }
        self
    }

    /// Appends a repeated field of type uint64.
    ///
    /// Use this instead of multiple [`Anybuf::append_uint64`] to ensure 0 values are not lost.
    pub fn append_repeated_uint64(mut self, field_number: u32, data: &[u64]) -> Self {
        for value in data {
            self.append_tag(field_number, WireType::Varint);
            unsigned_varint_encode(*value, &mut self.output);
        }
        self
    }

    /// Appends a repeated field of type sint32.
    ///
    /// Use this instead of multiple [`Anybuf::append_sint32`] to ensure 0 values are not lost.
    ///
    /// ## Example
    ///
    /// ```
    /// # use anybuf::Anybuf;
    /// // Three signed ints with field number 4
    /// let serialized = Anybuf::new()
    ///     .append_repeated_sint32(3, &[-30, 0, 17])
    ///     .into_vec();
    /// ```
    pub fn append_repeated_sint32(mut self, field_number: u32, data: &[i32]) -> Self {
        for value in data {
            self.append_tag(field_number, WireType::Varint);
            unsigned_varint_encode(to_zigzag32(*value).into(), &mut self.output);
        }
        self
    }

    /// Appends a repeated field of type sint64.
    ///
    /// Use this instead of multiple [`Anybuf::append_sint64`] to ensure 0 values are not lost.
    ///
    /// ## Example
    ///
    /// ```
    /// # use anybuf::Anybuf;
    /// // Three signed ints with field number 4
    /// let serialized = Anybuf::new()
    ///     .append_repeated_sint64(4, &[-30, 0, 17])
    ///     .into_vec();
    /// ```
    pub fn append_repeated_sint64(mut self, field_number: u32, data: &[i64]) -> Self {
        for value in data {
            self.append_tag(field_number, WireType::Varint);
            unsigned_varint_encode(to_zigzag64(*value), &mut self.output);
        }
        self
    }

    /// Appends a repeated field of type bool.
    ///
    /// Use this instead of multiple [`Anybuf::append_bool`] to ensure false values are not lost.
    pub fn append_repeated_bool(mut self, field_number: u32, data: &[bool]) -> Self {
        for value in data {
            self.append_tag(field_number, WireType::Varint);
            unsigned_varint_encode((*value).into(), &mut self.output);
        }
        self
    }

    /// Appends a repeated field of type string.
    ///
    /// Use this instead of multiple [`Anybuf::append_string`] to ensure "" values are not lost.
    ///
    /// ## Example
    ///
    /// ```
    /// # use anybuf::Anybuf;
    /// let name = "Bono".to_string();
    ///
    /// // Three string fields with field number 4
    /// let serialized = Anybuf::new()
    ///     .append_repeated_string(4, &["", "Caro", &name])
    ///     .into_vec();
    /// ```
    pub fn append_repeated_string(mut self, field_number: u32, data: &[&str]) -> Self {
        for value in data {
            self.append_tag(field_number, WireType::Len);
            unsigned_varint_encode(value.len() as u64, &mut self.output);
            self.output.extend_from_slice(value.as_bytes());
        }
        self
    }

    /// Appends a repeated field of type bytes.
    ///
    /// Use this instead of multiple [`Anybuf::append_bytes`] to ensure empty values are not lost.
    ///
    /// ## Example
    ///
    /// ```
    /// # use anybuf::Anybuf;
    /// let blob = vec![4u8; 75];
    ///
    /// // Three bytes fields with field number 5
    /// let serialized = Anybuf::new()
    ///     .append_repeated_bytes(5, &[b"", b"abcd", &blob])
    ///     .into_vec();
    /// ```
    pub fn append_repeated_bytes(mut self, field_number: u32, data: &[&[u8]]) -> Self {
        for value in data {
            // tag
            self.append_tag(field_number, WireType::Len);
            // length
            unsigned_varint_encode(value.len() as u64, &mut self.output);
            // value
            self.output.extend_from_slice(value);
        }
        self
    }

    /// Appends a repeated field of type message.
    ///
    /// Use this instead of multiple [`Anybuf::append_message`] to ensure empty values are not lost.
    ///
    /// ## Example
    ///
    /// ```
    /// # use anybuf::Anybuf;
    /// // A repeated message at field number 11. This adds 3 elements, one of them is the default message.
    /// let serialized = Anybuf::new()
    ///     .append_repeated_message(11, &[
    ///         &Anybuf::new().append_uint32(1, 1),
    ///         &Anybuf::new(),
    ///         &Anybuf::new().append_uint32(1, 3),
    ///     ])
    ///     .into_vec();
    /// ```
    pub fn append_repeated_message(mut self, field_number: u32, messages: &[&Anybuf]) -> Self {
        for message in messages {
            let data = message.as_bytes();
            // tag
            self.append_tag(field_number, WireType::Len);
            // length
            unsigned_varint_encode(data.len() as u64, &mut self.output);
            // value
            self.output.extend_from_slice(data);
        }
        self
    }

    /// Returns the protobuf bytes of the instance.
    ///
    /// The data is the same as [`Anybuf::into_vec`] but does not consume the instance.
    pub fn as_bytes(&self) -> &[u8] {
        &self.output
    }

    /// Takes the instance and returns the protobuf bytes.
    ///
    /// The data is the same as [`Anybuf::as_bytes`] but consumes the instance in order to
    /// return an owned vector without cloning the data.
    pub fn into_vec(self) -> Vec<u8> {
        self.output
    }

    fn append_tag(&mut self, field_number: u32, field_type: WireType) {
        // The top 3 bits of a field number must be unset, ie.e this shift is safe for valid field numbers
        // "The smallest field number you can specify is 1, and the largest is 2^29-1, or 536,870,911"
        // https://protobuf.dev/programming-guides/proto3/#assigning-field-numbers
        let tag: u32 = (field_number << 3) | field_type as u32;
        unsigned_varint_encode(tag as u64, &mut self.output);
    }
}

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

    #[test]
    fn new_returns_empty_data() {
        let data = Anybuf::new();
        assert_eq!(data.into_vec(), &[]);
    }

    #[test]
    fn append_uint64_works() {
        let data = Anybuf::new().append_uint64(1, 150);
        assert_eq!(data.into_vec(), [0b00001000, 0b10010110, 0b00000001]);

        // Zero/Default field not written
        let data = Anybuf::new().append_uint64(1, 0);
        assert_eq!(data.into_vec(), &[]);
    }

    #[test]
    fn append_uint32_works() {
        let data = Anybuf::new().append_uint32(1, 150);
        assert_eq!(data.into_vec(), [0b00001000, 0b10010110, 0b00000001]);

        // large value (echo "number: 215874321" | protoc --encode=Room *.proto | hexdump -C)
        let data = Anybuf::new().append_uint32(1, 215874321);
        assert_eq!(data.into_vec(), b"\x08\x91\xf6\xf7\x66");

        // max value (echo "number: 4294967295" | protoc --encode=Room *.proto | hexdump -C)
        let data = Anybuf::new().append_uint32(1, u32::MAX);
        assert_eq!(data.into_vec(), b"\x08\xff\xff\xff\xff\x0f");

        // Zero/Default field not written
        let data = Anybuf::new().append_uint32(1, 0);
        assert_eq!(data.into_vec(), &[]);
    }

    #[test]
    fn append_sint32_works() {
        // for x in -2147483648 -735983 -456 -2 -1 0 1 5 21 900 8247598 2147483647; do echo "$x,$(echo "altitude: $x" | protoc --encode=Room *.proto | xxd -p)"; done
        assert_eq!(
            Anybuf::new().append_sint32(4, -2147483648).into_vec(),
            hex!("20ffffffff0f")
        );
        assert_eq!(
            Anybuf::new().append_sint32(4, -735983).into_vec(),
            hex!("20ddeb59")
        );
        assert_eq!(
            Anybuf::new().append_sint32(4, -456).into_vec(),
            hex!("208f07")
        );
        assert_eq!(Anybuf::new().append_sint32(4, -2).into_vec(), hex!("2003"));
        assert_eq!(Anybuf::new().append_sint32(4, -1).into_vec(), hex!("2001"));
        assert_eq!(Anybuf::new().append_sint32(4, 0).into_vec(), hex!(""));
        assert_eq!(Anybuf::new().append_sint32(4, 1).into_vec(), hex!("2002"));
        assert_eq!(Anybuf::new().append_sint32(4, 5).into_vec(), hex!("200a"));
        assert_eq!(Anybuf::new().append_sint32(4, 21).into_vec(), hex!("202a"));
        assert_eq!(
            Anybuf::new().append_sint32(4, 900).into_vec(),
            hex!("20880e")
        );
        assert_eq!(
            Anybuf::new().append_sint32(4, 8247598).into_vec(),
            hex!("20dce4ee07")
        );
        assert_eq!(
            Anybuf::new().append_sint32(4, 2147483647).into_vec(),
            hex!("20feffffff0f")
        );
    }

    #[test]
    fn append_sint64_works() {
        // for x in -9223372036854775808 -2147483649 -2147483648 -735983 -456 -2 -1 0 1 5 21 900 8247598 2147483647 2147483648 9223372036854775807; do echo "$x,$(echo "temperature: $x" | protoc --encode=Room *.proto | xxd -p)"; done
        assert_eq!(
            Anybuf::new().append_sint64(5, i64::MIN).into_vec(),
            hex!("28ffffffffffffffffff01")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, -2147483649).into_vec(),
            hex!("288180808010")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, -2147483648).into_vec(),
            hex!("28ffffffff0f")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, -735983).into_vec(),
            hex!("28ddeb59")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, -456).into_vec(),
            hex!("288f07")
        );
        assert_eq!(Anybuf::new().append_sint64(5, -2).into_vec(), hex!("2803"));
        assert_eq!(Anybuf::new().append_sint64(5, -1).into_vec(), hex!("2801"));
        assert_eq!(Anybuf::new().append_sint64(5, 0).into_vec(), hex!(""));
        assert_eq!(Anybuf::new().append_sint64(5, 1).into_vec(), hex!("2802"));
        assert_eq!(Anybuf::new().append_sint64(5, 5).into_vec(), hex!("280a"));
        assert_eq!(Anybuf::new().append_sint64(5, 21).into_vec(), hex!("282a"));
        assert_eq!(
            Anybuf::new().append_sint64(5, 900).into_vec(),
            hex!("28880e")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, 8247598).into_vec(),
            hex!("28dce4ee07")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, 2147483647).into_vec(),
            hex!("28feffffff0f")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, 2147483648).into_vec(),
            hex!("288080808010")
        );
        assert_eq!(
            Anybuf::new().append_sint64(5, i64::MAX).into_vec(),
            hex!("28feffffffffffffffff01")
        );
    }

    #[test]
    fn append_bool_works() {
        // echo "on: true" | protoc --encode=Lights *.proto | hexdump -C
        let data = Anybuf::new().append_bool(3, true);
        assert_eq!(data.into_vec(), [0x18, 0x01]);

        // Zero/Default field not written
        let data = Anybuf::new().append_bool(3, false);
        assert_eq!(data.into_vec(), &[]);
    }

    #[test]
    fn append_bytes() {
        // &str
        let data = Anybuf::new().append_bytes(2, "testing");
        assert_eq!(
            data.into_vec(),
            [0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67]
        );

        // String
        let data = Anybuf::new().append_bytes(2, String::from("testing"));
        assert_eq!(
            data.into_vec(),
            [0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67]
        );

        // &[u8]
        let data = Anybuf::new().append_bytes(2, b"testing");
        assert_eq!(
            data.into_vec(),
            [0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67]
        );

        // Empty field not written
        let data = Anybuf::new().append_bytes(2, b"");
        assert_eq!(data.into_vec(), []);
    }

    #[test]
    fn append_string() {
        // &str
        let data = Anybuf::new().append_string(2, "testing");
        assert_eq!(
            data.into_vec(),
            [0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67]
        );

        // String
        let data = Anybuf::new().append_string(2, String::from("testing"));
        assert_eq!(
            data.into_vec(),
            [0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67]
        );

        // Empty field not written
        let data = Anybuf::new().append_string(2, "");
        assert_eq!(data.into_vec(), []);
    }

    #[test]
    fn append_message_works() {
        // echo "number: 4; lights: {on: true}; size: 56" | protoc --encode=Room *.proto | hexdump -C
        let data = Anybuf::new()
            .append_uint64(1, 4)
            .append_message(2, &Anybuf::new().append_bool(3, true))
            .append_uint64(3, 56);
        assert_eq!(data.into_vec(), b"\x08\x04\x12\x02\x18\x01\x18\x38");
    }

    #[test]
    fn append_repeated_uint32_works() {
        // echo "id: \"uint32s\"; numbers_uint32: [0, 1, 2, 37546, 4294967295]" | protoc --encode=Collection *.proto | xxd -p
        let data = Anybuf::new()
            .append_string(1, "uint32s")
            .append_repeated_uint32(2, &[0, 1, 2, 37546, 4294967295]);
        assert_eq!(
            data.into_vec(),
            hex!("0a0775696e7433327310001001100210aaa50210ffffffff0f")
        );

        // echo "id: \"no uint32s\"; numbers_uint32: []" | protoc --encode=Collection *.proto | xxd -p
        let data = Anybuf::new()
            .append_string(1, "no uint32s")
            .append_repeated_uint32(2, &[]);
        assert_eq!(data.into_vec(), hex!("0a0a6e6f2075696e74333273"));
    }

    #[test]
    fn append_repeated_uint64_works() {
        // echo "id: \"uint64s\"; numbers_uint64: [0, 1, 2, 37546, 4294967295, 18446744073709551615]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "uint64s")
            .append_repeated_uint64(3, &[0, 1, 2, 37546, 4294967295, u64::MAX]);
        assert_eq!(
            data.into_vec(),
            hex!("0a0775696e7436347318001801180218aaa50218ffffffff0f18ffffffffffffffffff01")
        );

        // echo "id: \"no uint64s\"; numbers_uint64: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "no uint64s")
            .append_repeated_uint64(3, &[]);
        assert_eq!(data.into_vec(), hex!("0a0a6e6f2075696e74363473"));
    }

    #[test]
    fn append_repeated_sint32_works() {
        // echo "id: \"sint32s\"; numbers_sint32: [-2147483648, -1, 0, 1, 2, 37546, 2147483647]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "sint32s")
            .append_repeated_sint32(4, &[i32::MIN, -1, 0, 1, 2, 37546, i32::MAX]);
        assert_eq!(
            data.into_vec(),
            hex!("0a0773696e7433327320ffffffff0f200120002002200420d4ca0420feffffff0f")
        );

        // echo "id: \"no sint32s\"; numbers_sint32: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "no sint32s")
            .append_repeated_sint32(4, &[]);
        assert_eq!(data.into_vec(), hex!("0a0a6e6f2073696e74333273"));
    }

    #[test]
    fn append_repeated_sint64_works() {
        // echo "id: \"sint64s\"; numbers_sint64: [-9223372036854775808, -1, 0, 1, 2, 37546, 4294967295, 9223372036854775807]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "sint64s")
            .append_repeated_sint64(5, &[i64::MIN, -1, 0, 1, 2, 37546, 4294967295, i64::MAX]);
        assert_eq!(
            data.into_vec(),
            hex!("0a0773696e7436347328ffffffffffffffffff01280128002802280428d4ca0428feffffff1f28feffffffffffffffff01")
        );

        // echo "id: \"no sint64s\"; numbers_sint64: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "no sint64s")
            .append_repeated_uint64(5, &[]);
        assert_eq!(data.into_vec(), hex!("0a0a6e6f2073696e74363473"));
    }

    #[test]
    fn append_repeated_bool_works() {
        // echo "id: \"bools\"; booleans: [false, false, true, true, false]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "bools")
            .append_repeated_bool(8, &[false, false, true, true, false]);
        assert_eq!(data.into_vec(), hex!("0a05626f6f6c7340004000400140014000"));

        // echo "id: \"no bools\"; booleans: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "no bools")
            .append_repeated_uint64(8, &[]);
        assert_eq!(data.into_vec(), hex!("0a086e6f20626f6f6c73"));
    }

    #[test]
    fn append_repeated_string_works() {
        // echo "id: \"strings\"; strings: [\"\", \"a\", \"bcde\"]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "strings")
            .append_repeated_string(9, &["", "a", "bcde"]);
        assert_eq!(
            data.into_vec(),
            hex!("0a07737472696e67734a004a01614a0462636465")
        );

        // echo "id: \"no strings\"; strings: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "no strings")
            .append_repeated_string(9, &[]);
        assert_eq!(data.into_vec(), hex!("0a0a6e6f20737472696e6773"));
    }

    #[test]
    fn append_repeated_bytes_works() {
        // echo "id: \"bytess\"; bytess: [\"\", \"a\", \"bcde\"]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "bytess")
            .append_repeated_bytes(10, &[b"", b"a", b"bcde"]);
        assert_eq!(
            data.into_vec(),
            hex!("0a066279746573735200520161520462636465")
        );

        // echo "id: \"no bytess\"; bytess: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "no bytess")
            .append_repeated_bytes(10, &[]);
        assert_eq!(data.into_vec(), hex!("0a096e6f20627974657373"));
    }

    #[test]
    fn append_repeated_message_works() {
        // echo "messages: [{ number: 1}, { number: 2}, { number: 3}]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new().append_repeated_message(
            11,
            &[
                &Anybuf::new().append_uint32(1, 1),
                &Anybuf::new().append_uint32(1, 2),
                &Anybuf::new().append_uint32(1, 3),
            ],
        );
        assert_eq!(data.into_vec(), hex!("5a0208015a0208025a020803"));

        // An empty message
        // echo "messages: [{ number: 1}, { }, { number: 3}]" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new().append_repeated_message(
            11,
            &[
                &Anybuf::new().append_uint32(1, 1),
                &Anybuf::new(),
                &Anybuf::new().append_uint32(1, 3),
            ],
        );
        assert_eq!(data.into_vec(), hex!("5a0208015a005a020803"));

        // echo "id: \"no messages\"; messages: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
        let data = Anybuf::new()
            .append_string(1, "no messages")
            .append_repeated_message(11, &[]);
        assert_eq!(data.into_vec(), hex!("0a0b6e6f206d65737361676573"));
    }

    #[test]
    fn unsigned_varint_encode_works() {
        // examples from https://github.com/multiformats/unsigned-varint
        for (value, expected) in [
            (1, vec![0x01]),
            (127, vec![0x7f]),
            (128, vec![0x80, 0x01]),
            (255, vec![0xff, 0x01]),
            (300, vec![0xac, 0x02]),
            (16384, vec![0x80, 0x80, 0x01]),
        ] {
            let mut dest = Vec::new();
            unsigned_varint_encode(value, &mut dest);
            assert_eq!(dest, expected);
        }

        // https://ngtzeyang94.medium.com/go-with-examples-protobuf-encoding-mechanics-54ceff48ebaa
        let mut dest = Vec::new();
        unsigned_varint_encode(18789, &mut dest);
        assert_eq!(dest, [229, 146, 1]);

        // From https://github.com/tokio-rs/prost/blob/v0.12.1/src/encoding.rs#L1626-L1678
        let tests: Vec<(u64, &[u8])> = vec![
            (2u64.pow(0) - 1, &[0x00]),
            (2u64.pow(0), &[0x01]),
            (2u64.pow(7) - 1, &[0x7F]),
            (2u64.pow(7), &[0x80, 0x01]),
            (300, &[0xAC, 0x02]),
            (2u64.pow(14) - 1, &[0xFF, 0x7F]),
            (2u64.pow(14), &[0x80, 0x80, 0x01]),
            (2u64.pow(21) - 1, &[0xFF, 0xFF, 0x7F]),
            (2u64.pow(21), &[0x80, 0x80, 0x80, 0x01]),
            (2u64.pow(28) - 1, &[0xFF, 0xFF, 0xFF, 0x7F]),
            (2u64.pow(28), &[0x80, 0x80, 0x80, 0x80, 0x01]),
            (2u64.pow(35) - 1, &[0xFF, 0xFF, 0xFF, 0xFF, 0x7F]),
            (2u64.pow(35), &[0x80, 0x80, 0x80, 0x80, 0x80, 0x01]),
            (2u64.pow(42) - 1, &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]),
            (2u64.pow(42), &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01]),
            (
                2u64.pow(49) - 1,
                &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F],
            ),
            (
                2u64.pow(49),
                &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01],
            ),
            (
                2u64.pow(56) - 1,
                &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F],
            ),
            (
                2u64.pow(56),
                &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01],
            ),
            (
                2u64.pow(63) - 1,
                &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F],
            ),
            (
                2u64.pow(63),
                &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01],
            ),
            (
                u64::MAX,
                &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01],
            ),
        ];
        for (value, expected) in tests {
            let mut dest = Vec::new();
            unsigned_varint_encode(value, &mut dest);
            assert_eq!(dest, expected);
        }

        // https://codeberg.org/ft/ufw/src/tag/v4.1.0/test/t-varint.c#L90-L101 and
        // https://codeberg.org/ft/ufw/src/tag/v4.1.0/test/t-varint.c#L39-L52
        for (value, expected) in [
            (0, vec![0x00]),
            (128, vec![0x80, 0x01]),
            (1234, vec![0xd2, 0x09]),
            (u32::MAX as u64, vec![0xff, 0xff, 0xff, 0xff, 0x0f]),
            (
                u64::MAX,
                vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01],
            ),
        ] {
            let mut dest = Vec::new();
            unsigned_varint_encode(value, &mut dest);
            assert_eq!(dest, expected);
        }
    }
}