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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
use std::{convert::TryInto, mem::size_of};

use serde::de::{
    self, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, SeqAccess, VariantAccess,
    Visitor,
};
use serde::Deserialize;

use crate::error::{Error, Result};

const BEBOP_STARTING_INDEX: usize = 1;

/// A Deserializer implementation for Bebop
pub struct Deserializer<'de> {
    // This string starts with the input data and characters are truncated off
    // the beginning as data is parsed.
    input: &'de [u8],
    skipped_index: bool,
    is_message: bool,
}

impl<'de> Deserializer<'de> {
    /// Creates a new Deserializer that will deserialize the given byte slice
    pub fn from_bytes(input: &'de [u8]) -> Self {
        Deserializer {
            input,
            skipped_index: false,
            is_message: false,
        }
    }
}

/// Deserializes from raw bytes to the given type
///
/// If you need a deserializer, use `Deserializer::from_bytes` to create one
pub fn from_bytes<'a, T>(s: &'a [u8]) -> Result<T>
where
    T: Deserialize<'a>,
{
    let mut deserializer = Deserializer::from_bytes(s);
    let t = T::deserialize(&mut deserializer)?;
    if deserializer.input.is_empty() {
        Ok(t)
    } else {
        Err(Error::TrailingBytes)
    }
}

impl<'de> Deserializer<'de> {
    fn parse_string(&mut self) -> Result<&'de str> {
        // First, let's get the length of the string
        let str_len = self.parse_object_size()?;

        // Now that we have the string length, split again
        if str_len > self.input.len() {
            return Err(Error::Eof);
        }
        let (data, remaining) = self.input.split_at(str_len);
        self.input = remaining;

        // Validate that the string is actually utf-8
        Ok(std::str::from_utf8(data).map_err(|_| Error::InvalidUtf8)?)
    }

    /// grabs the size of the current object in the input, advances the input to
    /// the start of the object, and returns the size
    fn parse_object_size(&mut self) -> Result<usize> {
        let size = size_of::<u32>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        Ok(u32::from_le_bytes(raw.try_into().map_err(|_| Error::InvalidNumberBytes)?) as usize)
    }
}

impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
    type Error = Error;

    fn is_human_readable(&self) -> bool {
        false
    }

    // Look at the input data to decide what Serde data model type to
    // deserialize as. Not all data formats are able to support this operation.
    // Formats that support `deserialize_any` are known as self-describing.
    fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Message(
            "Bebop does not support deserializer_any".to_string(),
        ))
    }

    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        // Grab the single byte representing the bool
        let byte = self.input.first().ok_or(Error::Eof)?;
        let val = if *byte == 0u8 {
            false
        } else if *byte == 1u8 {
            true
        } else {
            return Err(Error::InvalidBool);
        };
        // Advance the input
        self.input = &self.input[1..];
        visitor.visit_bool(val)
    }

    // The `parse_signed` function is generic over the integer type `T` so here
    // it is invoked with `T=i8`. The next 8 methods are similar.
    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<i8>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_i8(i8::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<i16>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_i16(i16::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<i32>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_i32(i32::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<i64>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_i64(i64::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        // This is easier than the others because we just pop a byte off of the input
        let byte = self.input.first().ok_or(Error::Eof)?;
        self.input = &self.input[1..];
        visitor.visit_u8(*byte)
    }

    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<u16>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_u16(u16::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<u32>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_u32(u32::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<u64>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_u64(u64::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    // Float parsing is stupidly hard.
    fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<f32>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_f32(f32::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    // Float parsing is stupidly hard.
    fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let size = size_of::<f64>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        let (raw, remaining) = self.input.split_at(size);
        self.input = remaining;
        visitor.visit_f64(f64::from_le_bytes(
            raw.try_into().map_err(|_| Error::InvalidNumberBytes)?,
        ))
    }

    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        // Chars are serialized as strings, so deserialize it and check its length
        let data = self.parse_string()?;
        if data.chars().count() != 1 {
            return Err(Error::InvalidChar);
        }
        // We just checked length, so unwrapping is ok
        visitor.visit_char(data.chars().next().unwrap())
    }

    // Refer to the "Understanding deserializer lifetimes" page for information
    // about the three deserialization flavors of strings in Serde.
    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_borrowed_str(self.parse_string()?)
    }

    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_string(self.parse_string()?.to_owned())
    }

    // The `Serializer` implementation on the previous page serialized byte
    // arrays as JSON arrays of bytes. Handle that representation here.
    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        // Byte arrays can be read directly off the input after getting the length
        let len = self.parse_object_size()?;

        // Now grab the data
        if len > self.input.len() {
            return Err(Error::Eof);
        }
        let (data, remaining) = self.input.split_at(len);
        self.input = remaining;
        visitor.visit_bytes(data)
    }

    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        // Byte arrays can be read directly off the input after getting the length
        let len = self.parse_object_size()?;

        // Now grab the data
        if len > self.input.len() {
            return Err(Error::Eof);
        }
        let (data, remaining) = self.input.split_at(len);
        self.input = remaining;
        visitor.visit_byte_buf(data.to_owned())
    }

    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        if self.is_message && self.skipped_index {
            // We are handling a message, so check for the index
            visitor.visit_none()
        } else {
            // We are handling a struct, so the data is present. Just return a visit_some
            visitor.visit_some(self)
        }
    }

    // In Serde, unit means an anonymous value containing no data.
    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        // A unit in a struct means that we should always be missing a field in
        // a message. A Bebop struct cannot contain it
        if self.is_message && self.skipped_index {
            // A unit type means there is a missing index, so return an error if there is data
            Err(Error::UnexpectedData)
        } else if self.is_message && !self.skipped_index {
            visitor.visit_unit()
        } else {
            // Structs cannot contain empty
            Err(Error::InvalidUnit)
        }
    }

    // Unit struct means a named value containing no data.
    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_unit(visitor)
    }

    // As is done here, serializers are encouraged to treat newtype structs as
    // insignificant wrappers around the data they contain. That means not
    // parsing anything other than the contained value.
    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_newtype_struct(self)
    }

    // Deserialization of compound types like sequences and maps happens by
    // passing the visitor an "Access" object that gives it the ability to
    // iterate through the data contained in the sequence.
    fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let len = self.parse_object_size()?;
        visitor.visit_seq(List::new(&mut self, len))
    }

    // We treat tuples like arrays
    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_seq(visitor)
    }

    // Tuple structs are also like arrays
    fn deserialize_tuple_struct<V>(
        self,
        _name: &'static str,
        _len: usize,
        visitor: V,
    ) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_seq(visitor)
    }

    fn deserialize_map<V>(mut self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let len = self.parse_object_size()?;
        visitor.visit_map(List::new(&mut self, len))
    }

    fn deserialize_struct<V>(
        mut self,
        _name: &'static str,
        fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        // Parse the first 4 bytes as a u32. If that length matches the length
        // of the rest of the body, it is a message. Otherwise, assume it is a
        // Bebop struct and handle accordingly
        let size = size_of::<u32>();
        // Before we split, check remaining output to avoid panic
        if size > self.input.len() {
            return Err(Error::Eof);
        }
        // Just peek the bytes, don't consume them yet
        let bytes = &self.input[..size];
        let len =
            u32::from_le_bytes(bytes.try_into().map_err(|_| Error::InvalidNumberBytes)?) as usize;
        // If this is the correct length, we should find a 0 byte at the end of the length
        let is_message = if self.input[size..][len - 1] == 0u8 {
            // Consume the bytes we used for the message length and trim off the trailing 0 byte
            self.input = &self.input[size..];
            self.is_message = true;
            // If it is a message, make sure to set the message index (starting with 1)
            true
        } else {
            false
        };
        visitor.visit_seq(StructAccess::new(&mut self, fields.len(), is_message))
    }

    fn deserialize_enum<V>(
        self,
        _name: &'static str,
        _variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_enum(self)
    }

    // An identifier in Serde is the type that identifies a field of a struct or
    // the variant of an enum. In JSON, struct fields and enum variants are
    // represented as strings. In other formats they may be represented as
    // numeric indices.
    fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Message(
            "Bebop does not support deserialize identifier".to_string(),
        ))
    }

    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_any(visitor)
    }
}

struct List<'a, 'de: 'a> {
    de: &'a mut Deserializer<'de>,
    expected_len: usize,
    current_len: usize,
}

impl<'a, 'de> List<'a, 'de> {
    fn new(de: &'a mut Deserializer<'de>, expected_len: usize) -> Self {
        List {
            de,
            expected_len,
            current_len: 0,
        }
    }
}

impl<'de, 'a> SeqAccess<'de> for List<'a, 'de> {
    type Error = Error;

    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
    where
        T: DeserializeSeed<'de>,
    {
        // If we have serialized all entries, we are done
        if self.current_len == self.expected_len {
            Ok(None)
        } else {
            // Otherwise, increment the current and deserialize the next message
            self.current_len += 1;
            seed.deserialize(&mut *self.de).map(Some)
        }
    }
}

impl<'de, 'a> MapAccess<'de> for List<'a, 'de> {
    type Error = Error;

    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
    where
        K: DeserializeSeed<'de>,
    {
        // If we have serialized all entries, we are done
        if self.current_len == self.expected_len {
            Ok(None)
        } else {
            // Otherwise, increment the current and deserialize the next message
            self.current_len += 1;
            seed.deserialize(&mut *self.de).map(Some)
        }
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
    where
        V: DeserializeSeed<'de>,
    {
        // We handle the index incrementing in the key seed, so just serialize
        seed.deserialize(&mut *self.de)
    }
}

struct StructAccess<'a, 'de: 'a> {
    de: &'a mut Deserializer<'de>,
    expected_fields: usize,
    is_message: bool,
    next_index: usize,
}

impl<'a, 'de> StructAccess<'a, 'de> {
    fn new(de: &'a mut Deserializer<'de>, expected_fields: usize, is_message: bool) -> Self {
        StructAccess {
            de,
            expected_fields,
            is_message,
            next_index: BEBOP_STARTING_INDEX,
        }
    }
}

impl<'de, 'a> SeqAccess<'de> for StructAccess<'a, 'de> {
    type Error = Error;

    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
    where
        T: DeserializeSeed<'de>,
    {
        // If we get to the end of the expected fields in a struct we are done
        // TODO: These first blocks don't seem to matter and never appear to get called
        if self.next_index > self.expected_fields && !self.is_message {
            Ok(None)
        } else if self.next_index > self.expected_fields && self.is_message {
            // Trim the trailing byte if we got here
            self.de.input = &self.de.input[1..];
            Ok(None)
        } else {
            // Otherwise, increment the current index and trim the index off the input if it is a message
            let expected_index = self.next_index;
            // Either way, the index increments, so we know that this one wasn't present in the
            // message (or was handled)
            self.next_index = expected_index + 1;
            if self.is_message {
                let possible_index = *self.de.input.first().ok_or(Error::Eof)? as usize;

                if expected_index == possible_index {
                    // Consume the index so the next part parses properly
                    self.de.input = &self.de.input[1..];
                    // Let the deserializer know that this index wasn't skipped
                    self.de.skipped_index = false;
                } else {
                    // Let the deserializer that this one was skipped
                    self.de.skipped_index = true;
                }
            }

            let res = seed.deserialize(&mut *self.de).map(Some)?;
            if self.is_message && self.next_index > self.expected_fields {
                // Trim the terminator byte if this is the last part of the message
                self.de.input = &self.de.input[1..];
            }
            Ok(res)
        }
    }
}

impl<'a, 'de> EnumAccess<'de> for &'a mut Deserializer<'de> {
    type Error = Error;
    type Variant = Self;

    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
    where
        V: DeserializeSeed<'de>,
    {
        // All we need is a u32, so get that data and deserialize
        let index = self.parse_object_size()?;
        let val = seed.deserialize(index.into_deserializer())?;
        // Parse the colon separating map key from value.
        Ok((val, self))
    }
}

impl<'a, 'de> VariantAccess<'de> for &'a mut Deserializer<'de> {
    type Error = Error;

    // If the `Visitor` expected this variant to be a unit variant, the input
    // should have been the plain string case handled in `deserialize_enum`.
    fn unit_variant(self) -> Result<()> {
        // We already serialized the index, so just return ok
        Ok(())
    }

    // The rest of these all return errors as they are not supported in bebop
    fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value>
    where
        T: DeserializeSeed<'de>,
    {
        Err(Error::VariantDataNotAllowed)
    }

    fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::VariantDataNotAllowed)
    }

    fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::VariantDataNotAllowed)
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashMap;

    use super::*;
    use serde::Deserialize;

    #[derive(Debug, Deserialize, PartialEq)]
    struct SimpleStruct {
        name: String,
        age: u16,
    }

    #[test]
    fn test_valid_struct() {
        // Taken from one of the bebop reference implementations
        let data: Vec<u8> = vec![7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 28, 0];
        let deserialized: SimpleStruct = from_bytes(&data).expect("Unable to deserialize");
        let expected = SimpleStruct {
            name: "Charlie".to_string(),
            age: 28,
        };
        assert_eq!(deserialized, expected);
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct SimpleMessage {
        name: String,
        age: Option<u16>,
    }

    #[test]
    fn test_valid_message_all_fields() {
        // Taken from one of the bebop reference implementations
        let data: Vec<u8> = vec![
            16, 0, 0, 0, 1, 7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 2, 28, 0, 0,
        ];

        let deserialized: SimpleMessage = from_bytes(&data).expect("Unable to deserialize");
        let expected = SimpleMessage {
            name: "Charlie".to_string(),
            age: Some(28),
        };

        assert_eq!(deserialized, expected);
    }

    #[test]
    fn test_valid_message_some_fields() {
        // Taken from one of the bebop reference implementations
        let data: Vec<u8> = vec![
            13, 0, 0, 0, 1, 7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 0,
        ];

        let deserialized: SimpleMessage = from_bytes(&data).expect("Unable to deserialize");
        let expected = SimpleMessage {
            name: "Charlie".to_string(),
            age: None,
        };

        assert_eq!(deserialized, expected);
    }

    #[derive(Debug, Deserialize, PartialEq)]
    #[allow(dead_code)]
    enum Fun {
        Not,
        Somewhat,
        Really,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Complex {
        name: Option<String>,
        fun_level: Fun,
        map: HashMap<String, SimpleStruct>,
        message_map: HashMap<String, SimpleMessage>,
        list: Vec<f32>,
        boolean: bool,
        int16: i16,
        int32: i32,
        int64: i64,
        uint16: u16,
        uint32: u32,
        uint64: u64,
        byte: u8,
        float64: f64,
    }

    #[test]
    fn test_complex() {
        // Taken from one of the bebop reference implementations
        let data: Vec<u8> = vec![
            124, 0, 0, 0, 1, 7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 2, 1, 0, 0, 0, 3, 1, 0,
            0, 0, 3, 0, 0, 0, 111, 110, 101, 3, 0, 0, 0, 79, 110, 101, 16, 0, 4, 1, 0, 0, 0, 3, 0,
            0, 0, 111, 110, 101, 9, 0, 0, 0, 1, 3, 0, 0, 0, 79, 110, 101, 0, 5, 2, 0, 0, 0, 218,
            15, 73, 64, 77, 248, 45, 64, 6, 1, 7, 253, 255, 8, 42, 0, 0, 0, 9, 21, 205, 91, 7, 0,
            0, 0, 0, 10, 3, 0, 11, 42, 0, 0, 0, 12, 21, 205, 91, 7, 0, 0, 0, 0, 13, 17, 14, 74,
            216, 18, 77, 251, 33, 9, 64, 0,
        ];

        let deserialized: Complex = from_bytes(&data).expect("Unable to deserialize");

        let mut map = HashMap::new();
        map.insert(
            "one".to_string(),
            SimpleStruct {
                name: "One".to_string(),
                age: 16,
            },
        );
        let mut message_map = HashMap::new();
        message_map.insert(
            "one".to_string(),
            SimpleMessage {
                name: "One".to_string(),
                age: None,
            },
        );
        #[allow(clippy::approx_constant, clippy::clippy::excessive_precision)]
        let expected = Complex {
            name: Some("Charlie".to_string()),
            fun_level: Fun::Somewhat,
            map,
            message_map,
            list: vec![3.1415926, 2.71828],
            boolean: true,
            int16: -3,
            int32: 42,
            int64: 123456789,
            uint16: 3,
            uint32: 42,
            uint64: 123456789,
            byte: 17,
            float64: 3.1415926,
        };
        assert_eq!(deserialized, expected);
    }
}