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
use serde::{ser, Serialize};

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

/// A Serializer implementation for Bebop
pub struct Serializer {
    // This buffer starts empty and we append bytes to it
    output: Vec<u8>,
    is_message: bool,
    has_option: bool,
}

/// Serializes the given object into a Bebop encoded vec of bytes
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
where
    T: Serialize,
{
    let mut serializer = Serializer {
        output: Vec::new(),
        is_message: false,
        has_option: false,
    };
    value.serialize(&mut serializer)?;
    Ok(serializer.output)
}

/// Forces serialization as a message. Normally, the serializer will
/// automatically assume a Bebop message if there are optional fields and a
/// Bebop struct if all the fields are required. This will force encoding as a
/// message. Right now this will only force the top level struct to be a
/// message, any embedded structs will not be assumed
// TODO: Should we add a parameter to recurse all the way down? Specifically,
// should we only encode the top level struct as a message unless recurse: true
// is specified?
pub fn to_bytes_message<T>(value: &T) -> Result<Vec<u8>>
where
    T: Serialize,
{
    let mut serializer = Serializer {
        output: Vec::new(),
        is_message: true,
        has_option: false,
    };
    value.serialize(&mut serializer)?;
    Ok(serializer.output)
}

// TODO: What would supporting the GUID and date types from bebop look like?

impl<'a> ser::Serializer for &'a mut Serializer {
    type Ok = ();

    type Error = Error;

    type SerializeSeq = Self;
    type SerializeTuple = Self;
    type SerializeTupleStruct = Self;
    type SerializeTupleVariant = Self;
    type SerializeMap = Self;
    type SerializeStruct = StructSerializer<'a>;
    type SerializeStructVariant = StructSerializer<'a>;

    fn serialize_bool(self, v: bool) -> Result<()> {
        self.output.push(if v { 0x01 } else { 0x00 });
        Ok(())
    }

    fn serialize_i8(self, _v: i8) -> Result<()> {
        Err(Error::Int8NotSupported)
    }

    fn serialize_i16(self, v: i16) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_i32(self, v: i32) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_i64(self, v: i64) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_u8(self, v: u8) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_u16(self, v: u16) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_u32(self, v: u32) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_u64(self, v: u64) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_f32(self, v: f32) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    fn serialize_f64(self, v: f64) -> Result<()> {
        self.output.extend(&v.to_le_bytes());
        Ok(())
    }

    // Serialize a char as a single-character string.
    fn serialize_char(self, v: char) -> Result<()> {
        self.output.extend(&(v.len_utf8() as u32).to_le_bytes());
        let mut encoded = Vec::with_capacity(v.len_utf8());
        v.encode_utf8(&mut encoded);
        self.output.extend(encoded);
        Ok(())
    }

    fn serialize_str(self, v: &str) -> Result<()> {
        self.output
            .extend(&(v.as_bytes().len() as u32).to_le_bytes());
        self.output.extend(v.as_bytes());
        Ok(())
    }

    fn serialize_bytes(self, v: &[u8]) -> Result<()> {
        use serde::ser::SerializeSeq;
        let mut seq = self.serialize_seq(Some(v.len()))?;
        for byte in v {
            seq.serialize_element(byte)?;
        }
        seq.end()
    }

    fn serialize_none(self) -> Result<()> {
        // Mark that we have an optional
        self.has_option = true;
        Ok(())
    }

    fn serialize_some<T>(self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        // Mark that we have an optional
        self.has_option = true;
        value.serialize(self)
    }

    fn serialize_unit(self) -> Result<()> {
        Ok(())
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
        self.serialize_unit()
    }

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
    ) -> Result<()> {
        self.output.extend(&variant_index.to_le_bytes());
        Ok(())
    }

    // Serializers are encouraged to treat newtype structs as
    // insignificant wrappers around the data they contain.
    fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(self)
    }

    fn serialize_newtype_variant<T>(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _value: &T,
    ) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        // This cannot be supported as enums in bebop cannot contain additional data
        Err(Error::VariantDataNotAllowed)
    }

    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
        self.output
            .extend(&(len.ok_or(Error::ExpectedArrayLength)? as u32).to_le_bytes());
        Ok(self)
    }

    // We'll just treat a tuple as an array
    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
        self.serialize_seq(Some(len))
    }

    // Tuple structs look just like sequences in Bebop
    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        len: usize,
    ) -> Result<Self::SerializeTupleStruct> {
        self.serialize_seq(Some(len))
    }

    fn serialize_tuple_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant> {
        // This cannot be supported as enums in bebop cannot contain additional data
        Err(Error::VariantDataNotAllowed)
    }

    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
        // Add the length of the map
        self.output
            .extend(&(len.ok_or(Error::ExpectedMapLength)? as u32).to_le_bytes());
        Ok(self)
    }

    fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
        // Set up our custom serializer
        Ok(StructSerializer {
            parts: Vec::new(),
            expected_length: len,
            has_option: false,
            inner: self,
        })
    }

    fn serialize_struct_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant> {
        // This cannot be supported as enums in bebop cannot contain additional data
        Err(Error::VariantDataNotAllowed)
    }
}

pub struct StructSerializer<'a> {
    // Because bebop structs require all fields to be set, we need to track for
    // `Option` values so we can represent as a message or a struct
    parts: Vec<Option<Vec<u8>>>,
    expected_length: usize,
    has_option: bool,
    inner: &'a mut Serializer,
}

impl<'a> StructSerializer<'a> {
    // Messages (i.e. things that can be optional) are serialized differently
    fn serialize_message(self) -> Result<()> {
        // Create the body of the message (so we can get length), we are using
        // zip so we have the right index type (and starting index) for
        // serialization
        let mut body = (1u8..)
            .zip(self.parts)
            .fold(Vec::new(), |mut acc, (index, data)| {
                // If there is data push the index and then extend with the body
                if let Some(bytes) = data {
                    acc.push(index);
                    acc.extend(bytes);
                }
                acc
            });
        // Push the final empty terminator byte
        body.push(0u8);

        // Now we can get a length and add that to the actual serializer
        self.inner.output.extend(&(body.len() as u32).to_le_bytes());
        self.inner.output.extend(body);
        Ok(())
    }

    // Structs are a simple concat of all the data members
    fn serialize_struct(self) -> Result<()> {
        // This is only called internally, so we aren't handling the case where
        // a None exists in the Vec
        self.inner
            .output
            .extend(self.parts.into_iter().flatten().flatten());
        Ok(())
    }

    // These are the same functions needed for the trait implementation so they
    // can be reused
    fn serialize_field_inner<T>(&mut self, _key: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        let mut serializer = Serializer {
            output: Vec::new(),
            is_message: false,
            has_option: false,
        };
        value.serialize(&mut serializer)?;

        // Set that we have an optional if needed
        self.has_option = serializer.has_option || self.has_option;

        // Not sure if there is a better way to check, but basically, if it is
        // none, it will still be empty
        if serializer.output.is_empty() {
            self.parts.push(None)
        } else {
            self.parts.push(Some(serializer.output))
        }
        Ok(())
    }

    fn end_inner(self) -> Result<()> {
        // Validate that we are serializing the right amount of things
        if self.expected_length != self.parts.len() {
            return Err(Error::StructLengthMismatch(
                self.expected_length,
                self.parts.len(),
            ));
        }
        // If we are forcing message encoding or we have an option, encode as a message
        if self.has_option || self.inner.is_message {
            self.serialize_message()
        } else {
            self.serialize_struct()
        }
    }
}

impl<'a> ser::SerializeStruct for StructSerializer<'a> {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        self.serialize_field_inner(key, value)
    }

    fn end(self) -> Result<()> {
        self.end_inner()
    }
}

impl<'a> ser::SerializeSeq for &'a mut Serializer {
    // Must match the `Ok` type of the serializer.
    type Ok = ();
    // Must match the `Error` type of the serializer.
    type Error = Error;

    // Serialize a single element of the sequence.
    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    // Close the sequence.
    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a> ser::SerializeTuple for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a> ser::SerializeTupleStruct for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a> ser::SerializeTupleVariant for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a> ser::SerializeMap for &'a mut Serializer {
    type Ok = ();
    type Error = Error;

    fn serialize_key<T>(&mut self, key: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        key.serialize(&mut **self)
    }

    fn serialize_value<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a> ser::SerializeStructVariant for StructSerializer<'a> {
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        self.serialize_field_inner(key, value)
    }

    fn end(self) -> Result<()> {
        self.end_inner()
    }
}

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

    use super::*;
    use serde::Serialize;

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

    #[test]
    fn test_valid_struct() {
        let data = SimpleStruct {
            name: "Charlie".to_string(),
            age: 28,
        };

        let raw = to_bytes(&data).expect("Unable to serialize");
        // Taken from one of the bebop reference implementations
        let expected: Vec<u8> = vec![7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 28, 0];
        assert_eq!(raw, expected);
    }

    #[test]
    fn test_valid_struct_message() {
        let data = SimpleStruct {
            name: "Charlie".to_string(),
            age: 28,
        };

        // Make sure if we force encoding as a message that it works
        let raw = to_bytes_message(&data).expect("Unable to serialize");
        // Taken from one of the bebop reference implementations
        let expected: Vec<u8> = vec![
            16, 0, 0, 0, 1, 7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 2, 28, 0, 0,
        ];
        assert_eq!(raw, expected);
    }

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

    #[test]
    fn test_valid_message_all_fields() {
        let data = SimpleMessage {
            name: "Charlie".to_string(),
            age: Some(28),
        };

        let raw = to_bytes(&data).expect("Unable to serialize");
        // Taken from one of the bebop reference implementations
        let expected: Vec<u8> = vec![
            16, 0, 0, 0, 1, 7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 2, 28, 0, 0,
        ];
        assert_eq!(raw, expected);
    }

    #[test]
    fn test_valid_message_some_fields() {
        let data = SimpleMessage {
            name: "Charlie".to_string(),
            age: None,
        };

        let raw = to_bytes(&data).expect("Unable to serialize");
        // Taken from one of the bebop reference implementations
        let expected: Vec<u8> = vec![
            13, 0, 0, 0, 1, 7, 0, 0, 0, 67, 104, 97, 114, 108, 105, 101, 0,
        ];
        assert_eq!(raw, expected);
    }

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

    #[derive(Serialize)]
    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() {
        // We are only inserting one entry into each map so the byte array will
        // match (ordering doesn't matter for hash maps and so they could get
        // encoded in different ways)
        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 data = 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,
        };

        let raw = to_bytes(&data).expect("Unable to serialize");
        // Taken from one of the bebop reference implementations
        let expected: 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,
        ];
        assert_eq!(raw, expected);
    }
}