httlib-protos 0.3.2

Protocol Buffers binary format implementation.
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
use std::io;
pub use super::EncoderError;
use crate::{Typ, Encoder};

/// Encodes the provided `val` into LEB128 format and writes the resulting
/// bytes into `buf`.
/// 
/// Varints are a method for serializing integers with one or more bytes. The
/// algorithm used here is known as [LEB128]. All bytes except the last have the
/// most significant bit (MSB) set (`C`), so that the decoder can determine
/// where the value ends. The other `7` bits (`N`) of each byte are intended to
/// represent the number.
/// 
/// LEB128 is an algorithm for encoding integers of arbitrary length in which
/// the bytes are arranged in a little-endian sequence. However, the Protocol
/// Buffers limit the size of the numbers to the supported data types.
/// 
/// ```txt
/// value = 150 (unsigned 32-bit)
/// 
/// Standard varint encoding:
///    XXXXXXXX 10010110 ... Number 150 in bytes.
///    X0000001 X0010110 ... Split to 7-bit sequence.
///    X0010110 X0000001 ... Revert the array of bytes.
///    10010110 00000001 ... Add MSB (1=continuation, 0=last byte).
/// ```
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_varint<W>(
    mut val: u64,
    buf: &mut W,
) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    let mut size = 0;
    loop {
        size += 1;
        if val < 0x80 {
            buf.write_all(&[val as u8 & 0x7F])?;
            return Ok(size);
        } else {
            let byte = ((val & 0x7F) | 0x80) as u8;
            buf.write_all(&[byte])?;
            val >>= 7;
        }
    }
}

/// Encodes field's key and writes the resulting bytes into `buf`.
/// 
/// The key is encoded as a `uint32` varint type, and in the last `3` bits (`T`)
/// contain the wire type. The key's field tag can thus be between `1` and
/// `2^29 - 1` = `536,870,911` (`0` is not a valid tag number).
/// 
/// ```txt
/// tag = 12345 (unsigned 32-bit), type = 1 (Varint)
/// 
/// 11001000 10000011 00000110 ... on the wire
/// CNNNNNNN CNNNNNNN CNNNNTTT ... bits per type
/// 
/// C = Contiunation, X = Number, T = Type
/// ```
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_key<W>(
    tag: u32,
    typ: Typ,
    buf: &mut W,
) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    if tag < Encoder::TAG_MIN || tag > Encoder::TAG_MAX {
        return Err(EncoderError::InvalidTag);
    }

    let key = (tag << 3) | typ as u32;
    encode_varint(u64::from(key), buf)
}

/// Encodes the provided `val` into `bool` format and writes the resulting
/// bytes into `buf`.
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_bool<W>(val: bool, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    let res = if val { 1u64 } else { 0u64 };
    encode_varint(res, buf)
}

/// Encodes the provided `val` into `int32` format and writes the resulting
/// bytes into `buf`.
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_int32<W>(val: i32, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    encode_varint(val as u64, buf)
}

/// Encodes the provided `val` into `int64` format and writes the resulting
/// bytes into `buf`.
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_int64<W>(val: i64, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    encode_varint(val as u64, buf)
}

/// Encodes the provided `val` into `uint32` format and writes the resulting
/// bytes into `buf`.
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_uint32<W>(val: u32, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    encode_varint(val as u64, buf)
}

/// Encodes the provided `val` into `uint64` format and writes the resulting
/// bytes into `buf`.
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_uint64<W>(val: u64, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    encode_varint(val, buf)
}

/// Encodes the provided `val` into `sint32` format and writes the resulting
/// bytes into `buf`.
/// 
/// There is a big difference between signed integer types (`sint32`) and the
/// "standard" integer types (`int32`). If you use `int32` as the type for a
/// negative number, the result is always ten bytes long, which makes a very
/// large unsigned integer. In case you know that the value will most likely be
/// negative, you can optimize the result and use one of the signed types, where
/// the resulting varint uses [ZigZag] encoding for efficiency. Essentially,
/// this means that the positive and negative integers are zigzagged through, so
/// that `-1` is encoded as `1`, `1` as `2`, `-2` as `3`, and so on.
/// 
/// ```txt
/// value = -12345 (signed 32-bit)
/// 
/// Signed 32-bit varint encoding:
///    -12345 ... Unsigned 32-bit integer.
///     24689 ... ZigZag value using (value << 1) ^ (value >> 31).
///           ... Continue with the standard varint encoding.
/// ```
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_sint32<W>(val: i32, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    let res = ((val << 1) ^ (val >> 31)) as u32 as u64;
    encode_varint(res, buf)
}

/// Encodes the provided `val` into `sint64` format and writes the resulting
/// bytes into `buf`.
/// 
/// There is a big difference between signed integer types (`sint64`) and the
/// "standard" integer types (`int64`). If you use `int64` as the type for a
/// negative number, the result is always ten bytes long, which makes a very
/// large unsigned integer. In case you know that the value will most likely be
/// negative, you can optimize the result and use one of the signed types, where
/// the resulting varint uses [ZigZag] encoding for efficiency. Essentially,
/// this means that the positive and negative integers are zigzagged through, so
/// that `-1` is encoded as `1`, `1` as `2`, `-2` as `3`, and so on.
/// 
/// ```txt
/// value = -54321 (signed 64-bit)
/// 
/// Signed 64-bit varint encoding:
///    -54321 ... Unsigned 64-bit integer.
///    108641 ... ZigZag value using (val << 1) ^ (val >> 63).
///           ... Continue with the standard varint encoding.
/// ```
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_sint64<W>(val: i64, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    let res = ((val << 1) ^ (val >> 63)) as u64;
    encode_varint(res, buf)
}

/// Encodes the provided `val` into `fixed32` format and writes the resulting
/// bytes into `buf`.
/// 
/// Such format represents an encoded 32-bit number with wire types `5`. Fixed
/// size number format is represented by bytes in little-endian byte order
/// (reversed order).
/// 
/// ```txt
/// value = 12345 (signed 32-bit)
/// 
/// Fixed-size encoding:
///    00000000 00000000 00110000 00111001 ... Value in (big-endian) bytes.
///    00111001 00110000 00000000 00000000 ... Reverse bytes to little-endian order.
/// ```
/// 
/// Use this format only when data is predictable and you know that the result
/// will be smaller than when using the "standard" formats. 
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_fixed32<W>(val: u32, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    buf.write_all(&val.to_le_bytes())?;
    Ok(4)
}

/// Encodes the provided `val` into `fixed64` format and writes the resulting
/// bytes into `buf`.
/// 
/// Such format represents an encoded 64-bit number with wire types `1`. Fixed
/// size number format is represented by bytes in little-endian byte order
/// (reversed order).
/// 
/// Use this format only when data is predictable and you know that the result
/// will be smaller than when using the "standard" formats. 
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_fixed64<W>(val: u64, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    buf.write_all(&val.to_le_bytes())?;
    Ok(8)
}

/// Encodes the provided `val` into `sfixed32` format and writes the resulting
/// bytes into `buf`.
/// 
/// Such format represents an encoded 32-bit number with wire types `5`. Fixed
/// size number format is represented by bytes in little-endian byte order
/// (reversed order).
/// 
/// Use this format only when data is predictable and you know that the result
/// will be smaller than when using the "standard" formats. 
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_sfixed32<W>(val: i32, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    buf.write_all(&val.to_le_bytes())?;
    Ok(4)
}

/// Encodes the provided `val` into `sfixed64` format and writes the resulting
/// bytes into `buf`.
/// 
/// Such format represents an encoded 64-bit number with wire types `1`. Fixed
/// size number format is represented by bytes in little-endian byte order
/// (reversed order).
/// 
/// Use this format only when data is predictable and you know that the result
/// will be smaller than when using the "standard" formats. 
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_sfixed64<W>(val: i64, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    buf.write_all(&val.to_le_bytes())?;
    Ok(8)
}

/// Encodes the provided `val` into `float` format and writes the resulting
/// bytes into `buf`.
/// 
/// Float is encoded as a 32-bit number with wire types `5`. Fixed-size number
/// format is represented by bytes in little-endian byte order (reversed order).
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_float<W>(val: f32, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    buf.write_all(&val.to_le_bytes())?;
    Ok(4)
}

/// Encodes the provided `val` into `double` format and writes the resulting
/// bytes into `buf`.
/// 
/// Double is encoded as a 64-bit number with wire types `1`. Fixed-size number
/// format is represented by bytes in little-endian byte order (reversed order).
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_double<W>(val: f64, buf: &mut W) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    buf.write_all(&val.to_le_bytes())?;
    Ok(8)
}

/// Wraps the provided `val` into `bytes` format and writes the resulting bytes
/// into `buf`.
/// 
/// Length-delimited type, represented with number `2`, encodes data into a
/// sequence of bytes prepended with a value of varint encoded which represents
/// the number of bytes that represent the content. This describes data types
/// `bytes` and `string`, `embedded` messages (nested objects) and `repated`
/// numeric fields.
/// 
/// ```txt
/// value = b"foo"
/// 
/// Length-delimited encoding:
///    00000011 XXXXXXXX XXXXXXXX XXXXXXXX ... Encode message size (3 bytes) as standard 32-bit varint.
///    00000011 01100110 01101111 01101111 ... Append string (foo) in bytes.
/// ```
/// 
/// On success, the number of written bytes is returned otherwise an error is 
/// thrown.
pub fn encode_bytes<W>(
    mut bytes: Vec<u8>,
    buf: &mut W,
) -> Result<usize, EncoderError>
where
    W: ?Sized + io::Write,
{
    let mut size = bytes.len();
    if size > u64::MAX as usize {
        return Err(EncoderError::DataOverflow);
    }
    size += encode_varint(size as u64, buf)?; // number of bytes
    buf.write_all(&mut bytes)?;
    Ok(size)
}

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

    /// Should encode a number into LEB128 wire format.
    #[test]
    fn encodes_varints() {
        let mut dst = vec![];
        let size = encode_varint(u64::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![0]);
        assert_eq!(size, 1);

        dst.clear();
        let size = encode_varint(u64::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 255, 255, 255, 1]);
        assert_eq!(size, 10);
    }

    /// Should encode field's header key which consists of field tag number and
    /// field wire type.
    #[test]
    fn encodes_key() {
        let mut dst = vec![];
        let size = encode_key(123456789, Typ::Varint, &mut dst).unwrap();
        assert_eq!(dst, vec![168, 209, 249, 214, 3]);
        assert_eq!(size, 5);

        dst.clear();
        let size = encode_key(1234, Typ::Bit32, &mut dst).unwrap();
        assert_eq!(dst, vec![149, 77]);
        assert_eq!(size, 2);
    }

    /// Should encode a numeric value as `int32` data type.
    #[test]
    fn encodes_int32() {
        let mut dst = vec![];
        let size = encode_int32(i32::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![128, 128, 128, 128, 248, 255, 255, 255, 255, 1]);
        assert_eq!(size, 10);

        dst.clear();
        let size = encode_int32(i32::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 7]);
        assert_eq!(size, 5);
    }

    /// Should encode a numeric value as `uint32` data type.
    #[test]
    fn encodes_uint32() {
        let mut dst = vec![];
        let size = encode_uint32(u32::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![0]);
        assert_eq!(size, 1);

        dst.clear();
        let size = encode_uint32(u32::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 15]);
        assert_eq!(size, 5);
    }

    /// Should encode a numeric value as `sint32` data type.
    #[test]
    fn encodes_sint32() {
        let mut dst = vec![];
        let size = encode_sint32(i32::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 15]);
        assert_eq!(size, 5);

        dst.clear();
        let size = encode_sint32(i32::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![254, 255, 255, 255, 15]);
        assert_eq!(size, 5);
    }

    /// Should encode a numeric value as `int64` data type.
    #[test]
    fn encodes_int64() {
        let mut dst = vec![];
        let size = encode_int64(i64::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![128, 128, 128, 128, 128, 128, 128, 128, 128, 1]);
        assert_eq!(size, 10);

        dst.clear();
        let size = encode_int64(i64::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 255, 255, 127]);
        assert_eq!(size, 9);
    }

    /// Should encode a numeric value as `uint64` data type.
    #[test]
    fn encodes_uint64() {
        let mut dst = vec![];
        let size = encode_uint64(u64::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![0]);
        assert_eq!(size, 1);

        dst.clear();
        let size = encode_uint64(u64::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 255, 255, 255, 1]);
        assert_eq!(size, 10);
    }

    /// Should encode a numeric value as `sint64` data type.
    #[test]
    fn encodes_sint64() {
        let mut dst = vec![];
        let size = encode_sint64(i64::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 255, 255, 255, 1]);
        assert_eq!(size, 10);

        dst.clear();
        let size = encode_sint64(i64::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![254, 255, 255, 255, 255, 255, 255, 255, 255, 1]);
        assert_eq!(size, 10);
    }

    /// Should encode a numeric value as `bool` data type.
    #[test]
    fn encodes_bool() {
        let mut dst = vec![];
        let size = encode_bool(true, &mut dst).unwrap();
        assert_eq!(dst, vec![1]);
        assert_eq!(size, 1);

        dst.clear();
        let size = encode_bool(false, &mut dst).unwrap();
        assert_eq!(dst, vec![0]);
        assert_eq!(size, 1);
    }

    /// Should encode a numeric value as `fixed64` data type.
    #[test]
    fn encodes_fixed64() {
        let mut dst = vec![];
        let size = encode_fixed64(u64::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![0, 0, 0, 0, 0, 0, 0, 0]);
        assert_eq!(size, 8);

        dst.clear();
        let size = encode_fixed64(u64::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 255, 255]);
        assert_eq!(size, 8);
    }

    /// Should encode a numeric value as `sfixed64` data type.
    #[test]
    fn encodes_sfixed64() {
        let mut dst = vec![];
        let size = encode_sfixed64(i64::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![0, 0, 0, 0, 0, 0, 0, 128]);
        assert_eq!(size, 8);

        dst.clear();
        let size = encode_sfixed64(i64::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 255, 127]);
        assert_eq!(size, 8);
    }

    /// Should encode a numeric value as `double` data type.
    #[test]
    fn encodes_double() {
        let mut dst = vec![];
        let size = encode_double(f64::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 239, 255]);
        assert_eq!(size, 8);

        dst.clear();
        let size = encode_double(f64::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255, 255, 255, 239, 127]);
        assert_eq!(size, 8);
    }

    /// Should encode a numeric value as `fixed32` data type.
    #[test]
    fn encodes_fixed32() {
        let mut dst = vec![];
        let size = encode_fixed32(u32::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![0, 0, 0, 0]);
        assert_eq!(size, 4);

        dst.clear();
        let size = encode_fixed32(u32::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 255]);
        assert_eq!(size, 4);
    }

    /// Should encode a numeric value as `sfixed32` data type.
    #[test]
    fn encodes_sfixed32() {
        let mut dst = vec![];
        let size = encode_sfixed32(i32::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![0, 0, 0, 128]);
        assert_eq!(size, 4);

        dst.clear();
        let size = encode_sfixed32(i32::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 255, 127]);
        assert_eq!(size, 4);
    }

    /// Should encode a numeric value as `float` data type.
    #[test]
    fn encodes_float() {
        let mut dst = vec![];
        let size = encode_float(f32::MIN, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 127, 255]);
        assert_eq!(size, 4);

        dst.clear();
        let size = encode_float(f32::MAX, &mut dst).unwrap();
        assert_eq!(dst, vec![255, 255, 127, 127]);
        assert_eq!(size, 4);
    }

    /// Should encode a bytes value as raw `bytes` data type.
    #[test]
    fn encodes_bytes() {
        let mut dst = vec![];
        let size = encode_bytes(vec![1, 2, 3, 4, 5], &mut dst).unwrap();
        assert_eq!(dst, vec![5, 1, 2, 3, 4, 5]);
        assert_eq!(size, 6);
    }
}