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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
//! Reading data from an octet sequence.
//!
//! Parsing is a little more complicated since encoded data may very well be
//! broken or ambiguously encoded. The helper type [`Parser`] wraps an octets
//! ref and allows to parse values from the octets.

use core::fmt;
use core::ops::{Bound, RangeBounds};
use crate::octets::Octets;

//------------ Parser --------------------------------------------------------

/// A parser for sequentially extracting data from an octets sequence.
///
/// The parser wraps an [octets reference] and remembers the read position on
/// the referenced sequence. Methods allow reading out data and progressing
/// the position beyond processed data.
///
/// [octets reference]: trait.OctetsRef.html
#[derive(Debug)]
pub struct Parser<'a, Octs: ?Sized> {
    /// The underlying octets reference.
    octets: &'a Octs,

    /// The current position of the parser from the beginning of `octets`.
    pos: usize,

    /// The length of the octets sequence.
    ///
    /// This starts out as the length of the underlying sequence and is kept
    /// here to be able to temporarily limit the allowed length for
    /// `parse_blocks`.
    len: usize,
}

impl<'a, Octs: ?Sized> Parser<'a, Octs> {
    /// Creates a new parser atop a reference to an octet sequence.
    pub fn from_ref(octets: &'a Octs) -> Self
    where
        Octs: AsRef<[u8]>,
    {
        Parser {
            pos: 0,
            len: octets.as_ref().len(),
            octets,
        }
    }

    /// Creates a new parser only using a range of the given octets.
    ///
    /// # Panics
    ///
    /// Panics if `range` is decreasing or out of bounds.
    pub fn with_range<R>(octets: &'a Octs, range: R) -> Self
    where
        Octs: AsRef<[u8]>,
        R: RangeBounds<usize>
    {
        match Self::_try_with_range(octets, range) {
            Ok(p) => p,
            Err(e) => panic!("{}", e)
        }
    }

    /// Creates a new parser only using a range if possible.
    ///
    /// If `range` is decreasing or out of bounds, returns an Error.
    pub fn try_with_range<R>(
        octets: &'a Octs, range: R
    ) -> Option<Self>
    where
        Octs: AsRef<[u8]>,
        R: RangeBounds<usize>
    {
        Self::_try_with_range(octets, range).ok()
    }

    /// Creates a new parser only using a range if possible.
    ///
    /// If `range` is decreasing or out of bounds, returns an Error.
    fn _try_with_range<R>(
        octets: &'a Octs, range: R
    ) -> Result<Self, &'static str>
    where
        Octs: AsRef<[u8]>,
        R: RangeBounds<usize>
    {
        let octets_len = octets.as_ref().len();

        let pos = match range.start_bound() {
            Bound::Unbounded => 0,
            Bound::Included(n) => *n,
            Bound::Excluded(n) => *n + 1,
        };

        if pos > octets_len {
            return Err("range start is out of range for octets")
        }

        let len = match range.end_bound() {
            Bound::Unbounded => octets_len,
            Bound::Excluded(n) => *n,
            Bound::Included(n) => *n + 1,
        };

        if len > octets_len {
            return Err("range end is out of range for octets")
        }

        if len < pos {
            return Err("range starts after end")
        }

        Ok(
            Parser {
                pos,
                len,
                octets
            }
        )

    }



    /// Returns the wrapped reference to the underlying octets sequence.
    pub fn octets_ref(&self) -> &'a Octs {
        self.octets
    }

    /// Returns the current parse position as an index into the octets.
    pub fn pos(&self) -> usize {
        self.pos
    }

    /// Returns the length of the underlying octet sequence.
    ///
    /// This is _not_ the number of octets left for parsing. Use
    /// [`remaining`] for that.
    ///
    /// [`remaining`]: #method.remaining
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns whether the underlying octets sequence is empty.
    ///
    /// This does _not_ return whether there are no more octets left to parse.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }
}

impl Parser<'static, [u8]> {
    /// Creates a new parser atop a static byte slice.
    ///
    /// This function is most useful for testing.
    pub fn from_static(slice: &'static [u8]) -> Self {
        Self::from_ref(slice)
    }
}

impl<'a, Octs: AsRef<[u8]> + ?Sized> Parser<'a, Octs> {
    /// Returns an octets slice of the underlying sequence.
    ///
    /// The slice covers the entire sequence, not just the remaining data. You
    /// can use [`peek`] for that.
    ///
    /// [`peek`]: #method.peek
    pub fn as_slice(&self) -> &[u8] {
        &self.octets.as_ref()[..self.len]
    }

    /// Returns the number of remaining octets to parse.
    pub fn remaining(&self) -> usize {
        self.len - self.pos
    }

    /// Returns a slice for the next `len` octets.
    ///
    /// If less than `len` octets are left, returns an error.
    pub fn peek(&self, len: usize) -> Result<&[u8], ShortInput> {
        self.check_len(len)?;
        Ok(&self.peek_all()[..len])
    }

    /// Returns a slice of the data left to parse.
    pub fn peek_all(&self) -> &[u8] {
        &self.octets.as_ref()[self.pos..self.len]
    }

    /// Repositions the parser to the given index.
    ///
    /// It is okay to reposition anywhere within the sequence. However,
    /// if `pos` is larger than the length of the sequence, an error is
    /// returned.
    pub fn seek(&mut self, pos: usize) -> Result<(), ShortInput> {
        if pos > self.len {
            Err(ShortInput(()))
        } else {
            self.pos = pos;
            Ok(())
        }
    }

    /// Advances the parser‘s position by `len` octets.
    ///
    /// If this would take the parser beyond its end, an error is returned.
    pub fn advance(&mut self, len: usize) -> Result<(), ShortInput> {
        if len > self.remaining() {
            Err(ShortInput(()))
        } else {
            self.pos += len;
            Ok(())
        }
    }

    /// Advances to the end of the parser.
    pub fn advance_to_end(&mut self) {
        self.pos = self.len
    }

    /// Checks that there are `len` octets left to parse.
    ///
    /// If there aren’t, returns an error.
    pub fn check_len(&self, len: usize) -> Result<(), ShortInput> {
        if self.remaining() < len {
            Err(ShortInput(()))
        } else {
            Ok(())
        }
    }
}

impl<'a, Octs: AsRef<[u8]> + ?Sized> Parser<'a, Octs> {
    /// Takes and returns the next `len` octets.
    ///
    /// Advances the parser by `len` octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_octets(
        &mut self,
        len: usize,
    ) -> Result<Octs::Range<'a>, ShortInput>
    where
        Octs: Octets,
    {
        let end = self.pos + len;
        if end > self.len {
            return Err(ShortInput(()));
        }
        let res = self.octets.range(self.pos..end);
        self.pos = end;
        Ok(res)
    }

    /// Fills the provided buffer by taking octets from the parser.
    ///
    /// Copies as many octets as the buffer is long from the parser into the
    /// buffer and advances the parser by that many octets.
    ///
    /// If there aren’t enough octets left in the parser to fill the buffer
    /// completely, returns an error and leaves the parser untouched.
    pub fn parse_buf(&mut self, buf: &mut [u8]) -> Result<(), ShortInput> {
        let pos = self.pos;
        self.advance(buf.len())?;
        buf.copy_from_slice(&self.octets.as_ref()[pos..self.pos]);
        Ok(())
    }

    /// Takes as many octets as requested and returns a parser for them.
    ///
    /// If enough octets are remaining, the method clones `self`, limits
    /// its length to the requested number of octets, and returns it. The
    /// returned parser will be positioned at wherever `self` was positioned.
    /// The `self` parser will be advanced by the requested amount of octets.
    ///
    /// If there aren’t enough octets left in the parser to fill the buffer
    /// completely, returns an error and leaves the parser untouched.
    pub fn parse_parser(&mut self, len: usize) -> Result<Self, ShortInput> {
        self.check_len(len)?;
        let mut res = *self;
        res.len = res.pos + len;
        self.pos += len;
        Ok(res)
    }

    /// Takes an `i8` from the beginning of the parser.
    ///
    /// Advances the parser by one octet. If there aren’t enough octets left,
    /// leaves the parser untouched and returns an error instead.
    pub fn parse_i8(&mut self) -> Result<i8, ShortInput> {
        let res = self.peek(1)?[0] as i8;
        self.pos += 1;
        Ok(res)
    }

    /// Takes a `u8` from the beginning of the parser.
    ///
    /// Advances the parser by one octet. If there aren’t enough octets left,
    /// leaves the parser untouched and returns an error instead.
    pub fn parse_u8(&mut self) -> Result<u8, ShortInput> {
        let res = self.peek(1)?[0];
        self.pos += 1;
        Ok(res)
    }
}

impl<'a, Octs: AsRef<[u8]> + ?Sized> Parser<'a, Octs> {
    /// Takes a big-endian `i16` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by two octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i16_be(&mut self) -> Result<i16, ShortInput> {
        let mut res = [0; 2];
        self.parse_buf(&mut res)?;
        Ok(i16::from_be_bytes(res))
    }

    /// Takes a little-endian `i16` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by two octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i16_le(&mut self) -> Result<i16, ShortInput> {
        let mut res = [0; 2];
        self.parse_buf(&mut res)?;
        Ok(i16::from_le_bytes(res))
    }

    /// Takes a big-endian `u16` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by two octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u16_be(&mut self) -> Result<u16, ShortInput> {
        let mut res = [0; 2];
        self.parse_buf(&mut res)?;
        Ok(u16::from_be_bytes(res))
    }

    /// Takes a little-endian `u16` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by two octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u16_le(&mut self) -> Result<u16, ShortInput> {
        let mut res = [0; 2];
        self.parse_buf(&mut res)?;
        Ok(u16::from_le_bytes(res))
    }

    /// Takes a big-endian `i32` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by four octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i32_be(&mut self) -> Result<i32, ShortInput> {
        let mut res = [0; 4];
        self.parse_buf(&mut res)?;
        Ok(i32::from_be_bytes(res))
    }

    /// Takes a little-endian `i32` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by four octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i32_le(&mut self) -> Result<i32, ShortInput> {
        let mut res = [0; 4];
        self.parse_buf(&mut res)?;
        Ok(i32::from_le_bytes(res))
    }

    /// Takes a big-endian `u32` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by four octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u32_be(&mut self) -> Result<u32, ShortInput> {
        let mut res = [0; 4];
        self.parse_buf(&mut res)?;
        Ok(u32::from_be_bytes(res))
    }

    /// Takes a little-endian `u32` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by four octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u32_le(&mut self) -> Result<u32, ShortInput> {
        let mut res = [0; 4];
        self.parse_buf(&mut res)?;
        Ok(u32::from_le_bytes(res))
    }

    /// Takes a big-endian `i64` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by eight octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i64_be(&mut self) -> Result<i64, ShortInput> {
        let mut res = [0; 8];
        self.parse_buf(&mut res)?;
        Ok(i64::from_be_bytes(res))
    }

    /// Takes a little-endian `i64` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by eight octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i64_le(&mut self) -> Result<i64, ShortInput> {
        let mut res = [0; 8];
        self.parse_buf(&mut res)?;
        Ok(i64::from_le_bytes(res))
    }

    /// Takes a big-endian `u64` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by eight octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u64_be(&mut self) -> Result<u64, ShortInput> {
        let mut res = [0; 8];
        self.parse_buf(&mut res)?;
        Ok(u64::from_be_bytes(res))
    }

    /// Takes a little-endian `u64` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by eight octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u64_le(&mut self) -> Result<u64, ShortInput> {
        let mut res = [0; 8];
        self.parse_buf(&mut res)?;
        Ok(u64::from_le_bytes(res))
    }

    /// Takes a big-endian `i128` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by 16 octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i128_be(&mut self) -> Result<i128, ShortInput> {
        let mut res = [0; 16];
        self.parse_buf(&mut res)?;
        Ok(i128::from_be_bytes(res))
    }

    /// Takes a little-endian `i128` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by 16 octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_i128_le(&mut self) -> Result<i128, ShortInput> {
        let mut res = [0; 16];
        self.parse_buf(&mut res)?;
        Ok(i128::from_le_bytes(res))
    }

    /// Takes a big-endian `u128` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by 16 octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u128_be(&mut self) -> Result<u128, ShortInput> {
        let mut res = [0; 16];
        self.parse_buf(&mut res)?;
        Ok(u128::from_be_bytes(res))
    }

    /// Takes a little-endian `u128` from the beginning of the parser.
    ///
    /// The value is converted into the system’s own byte order if necessary.
    /// The parser is advanced by 16 octets. If there aren’t enough octets
    /// left, leaves the parser untouched and returns an error instead.
    pub fn parse_u128_le(&mut self) -> Result<u128, ShortInput> {
        let mut res = [0; 16];
        self.parse_buf(&mut res)?;
        Ok(u128::from_le_bytes(res))
    }
}


//--- Clone and Copy

impl<'a, Octs: ?Sized> Clone for Parser<'a, Octs> {
    fn clone(&self) -> Self {
        Parser {
            octets: self.octets,
            pos: self.pos,
            len: self.len
        }
    }
}

impl<'a, Octs: ?Sized> Copy for Parser<'a, Octs> { }


//--------- ShortInput -------------------------------------------------------

/// An attempt was made to go beyond the end of the parser.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ShortInput(());

//--- Display and Error

impl fmt::Display for ShortInput {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("unexpected end of input")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ShortInput {}


//============ Testing =======================================================

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

    #[test]
    fn pos_seek_remaining() {
        let mut parser = Parser::from_static(b"0123456789");
        assert_eq!(parser.peek(1).unwrap(), b"0");
        assert_eq!(parser.pos(), 0);
        assert_eq!(parser.remaining(), 10);
        assert_eq!(parser.seek(2), Ok(()));
        assert_eq!(parser.pos(), 2);
        assert_eq!(parser.remaining(), 8);
        assert_eq!(parser.peek(1).unwrap(), b"2");
        assert_eq!(parser.seek(10), Ok(()));
        assert_eq!(parser.pos(), 10);
        assert_eq!(parser.remaining(), 0);
        assert_eq!(parser.peek_all(), b"");
        assert!(parser.seek(11).is_err());
        assert_eq!(parser.pos(), 10);
        assert_eq!(parser.remaining(), 0);
    }

    #[test]
    fn peek_check_len() {
        let mut parser = Parser::from_static(b"0123456789");
        assert_eq!(parser.peek(2), Ok(b"01".as_ref()));
        assert_eq!(parser.check_len(2), Ok(()));
        assert_eq!(parser.peek(10), Ok(b"0123456789".as_ref()));
        assert_eq!(parser.check_len(10), Ok(()));
        assert!(parser.peek(11).is_err());
        assert!(parser.check_len(11).is_err());
        parser.advance(2).unwrap();
        assert_eq!(parser.peek(2), Ok(b"23".as_ref()));
        assert_eq!(parser.check_len(2), Ok(()));
        assert_eq!(parser.peek(8), Ok(b"23456789".as_ref()));
        assert_eq!(parser.check_len(8), Ok(()));
        assert!(parser.peek(9).is_err());
        assert!(parser.check_len(9).is_err());
    }

    #[test]
    fn peek_all() {
        let mut parser = Parser::from_static(b"0123456789");
        assert_eq!(parser.peek_all(), b"0123456789");
        parser.advance(2).unwrap();
        assert_eq!(parser.peek_all(), b"23456789");

        let mut pp = parser.parse_parser(4).unwrap();
        assert_eq!(pp.peek_all(), b"2345");
        pp.advance(2).unwrap();
        assert_eq!(pp.peek_all(), b"45");

        assert_eq!(parser.peek_all(), b"6789");
    }

    #[test]
    fn advance() {
        let mut parser = Parser::from_static(b"0123456789");
        assert_eq!(parser.pos(), 0);
        assert_eq!(parser.peek(1).unwrap(), b"0");
        assert_eq!(parser.advance(2), Ok(()));
        assert_eq!(parser.pos(), 2);
        assert_eq!(parser.peek(1).unwrap(), b"2");
        assert!(parser.advance(9).is_err());
        assert_eq!(parser.advance(8), Ok(()));
        assert_eq!(parser.pos(), 10);
        assert_eq!(parser.peek_all(), b"");
    }

    #[test]
    fn parse_octets() {
        let mut parser = Parser::from_static(b"0123456789");
        assert_eq!(parser.parse_octets(2).unwrap(), b"01");
        assert_eq!(parser.parse_octets(2).unwrap(), b"23");
        assert!(parser.parse_octets(7).is_err());
        assert_eq!(parser.parse_octets(6).unwrap(), b"456789");
    }

    #[test]
    fn parse_buf() {
        let mut parser = Parser::from_static(b"0123456789");
        let mut buf = [0u8; 2];
        assert_eq!(parser.parse_buf(&mut buf), Ok(()));
        assert_eq!(&buf, b"01");
        assert_eq!(parser.parse_buf(&mut buf), Ok(()));
        assert_eq!(&buf, b"23");
        let mut buf = [0u8; 7];
        assert!(parser.parse_buf(&mut buf).is_err());
        let mut buf = [0u8; 6];
        assert_eq!(parser.parse_buf(&mut buf), Ok(()));
        assert_eq!(&buf, b"456789");
    }

    #[test]
    fn parse_i8() {
        let mut parser = Parser::from_static(b"\x12\xd6");
        assert_eq!(parser.parse_i8(), Ok(0x12_i8));
        assert_eq!(parser.parse_i8(), Ok(-42_i8));
        assert!(parser.parse_i8().is_err());
    }

    #[test]
    fn parse_u8() {
        let mut parser = Parser::from_static(b"\x12\xd6");
        assert_eq!(parser.parse_u8(), Ok(0x12_u8));
        assert_eq!(parser.parse_u8(), Ok(0xd6_u8));
        assert!(parser.parse_u8().is_err());
    }

    #[test]
    fn parse_i16_be() {
        let mut parser = Parser::from_static(b"\x12\x34\xef\x6e\0");
        assert_eq!(parser.parse_i16_be(), Ok(0x1234_i16));
        assert_eq!(parser.parse_i16_be(), Ok(-4242_i16));
        assert!(parser.parse_i16_be().is_err());
    }

    #[test]
    fn parse_i16_le() {
        let mut parser = Parser::from_static(b"\x34\x12\x6e\xef\0");
        assert_eq!(parser.parse_i16_le(), Ok(0x1234_i16));
        assert_eq!(parser.parse_i16_le(), Ok(-4242_i16));
        assert!(parser.parse_i16_le().is_err());
    }

    #[test]
    fn parse_u16_be() {
        let mut parser = Parser::from_static(b"\x12\x34\xef\x6e\0");
        assert_eq!(parser.parse_u16_be(), Ok(0x1234_u16));
        assert_eq!(parser.parse_u16_be(), Ok(0xef6e_u16));
        assert!(parser.parse_u16_be().is_err());
    }

    #[test]
    fn parse_u16_le() {
        let mut parser = Parser::from_static(b"\x34\x12\x6e\xef\0");
        assert_eq!(parser.parse_u16_le(), Ok(0x1234_u16));
        assert_eq!(parser.parse_u16_le(), Ok(0xef6e_u16));
        assert!(parser.parse_u16_le().is_err());
    }

    #[test]
    fn parse_i32_be() {
        let mut parser =
            Parser::from_static(b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\0\0\0");
        assert_eq!(parser.parse_i32_be(), Ok(0x12345678_i32));
        assert_eq!(parser.parse_i32_be(), Ok(-42424242_i32));
        assert!(parser.parse_i32_be().is_err());
    }

    #[test]
    fn parse_i32_le() {
        let mut parser =
            Parser::from_static(b"\x78\x56\x34\x12\x4e\xa8\x78\xfd\0\0\0");
        assert_eq!(parser.parse_i32_le(), Ok(0x12345678_i32));
        assert_eq!(parser.parse_i32_le(), Ok(-42424242_i32));
        assert!(parser.parse_i32_le().is_err());
    }

    #[test]
    fn parse_u32_be() {
        let mut parser =
            Parser::from_static(b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\0\0\0");
        assert_eq!(parser.parse_u32_be(), Ok(0x12345678_u32));
        assert_eq!(parser.parse_u32_be(), Ok(0xfd78a84e_u32));
        assert!(parser.parse_u32_be().is_err());
    }

    #[test]
    fn parse_u32_le() {
        let mut parser =
            Parser::from_static(b"\x78\x56\x34\x12\x4e\xa8\x78\xfd\0\0\0");
        assert_eq!(parser.parse_u32_le(), Ok(0x12345678_u32));
        assert_eq!(parser.parse_u32_le(), Ok(0xfd78a84e_u32));
        assert!(parser.parse_u32_le().is_err());
    }

    #[test]
    fn parse_i64_be() {
        let mut parser = Parser::from_static(
            b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\
              \xce\x7a\xba\x26\xdd\x0f\x29\x99\
              \0\0\0"
        );
        assert_eq!(parser.parse_i64_be(), Ok(0x12345678fd78a84e_i64));
        assert_eq!(parser.parse_i64_be(), Ok(-3568335078657414759_i64));
        assert!(parser.parse_i64_be().is_err());
    }

    #[test]
    fn parse_i64_le() {
        let mut parser = Parser::from_static(
            b"\x4e\xa8\x78\xfd\x78\x56\x34\x12\
              \x99\x29\x0f\xdd\x26\xba\x7a\xce\
              \0\0\0"
        );
        assert_eq!(parser.parse_i64_le(), Ok(0x12345678fd78a84e_i64));
        assert_eq!(parser.parse_i64_le(), Ok(-3568335078657414759_i64));
        assert!(parser.parse_i64_le().is_err());
    }

    #[test]
    fn parse_u64_be() {
        let mut parser = Parser::from_static(
            b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\
              \xce\x7a\xba\x26\xdd\x0f\x29\x99\
              \0\0\0"
        );
        assert_eq!(parser.parse_u64_be(), Ok(0x12345678fd78a84e_u64));
        assert_eq!(parser.parse_u64_be(), Ok(0xce7aba26dd0f2999_u64));
        assert!(parser.parse_u64_be().is_err());
    }

    #[test]
    fn parse_u64_le() {
        let mut parser = Parser::from_static(
            b"\x4e\xa8\x78\xfd\x78\x56\x34\x12\
              \x99\x29\x0f\xdd\x26\xba\x7a\xce\
              \0\0\0"
        );
        assert_eq!(parser.parse_u64_le(), Ok(0x12345678fd78a84e_u64));
        assert_eq!(parser.parse_u64_le(), Ok(0xce7aba26dd0f2999_u64));
        assert!(parser.parse_u64_le().is_err());
    }

    #[test]
    fn parse_i128_be() {
        let mut parser = Parser::from_static(
            b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\
              \xce\x7a\xba\x26\xdd\x0f\x29\x99\
              \xf8\xc6\x0e\x5d\x3f\x5e\x3a\x74\
              \x38\x38\x8f\x3f\x57\xa7\x94\xa0\
              \0\0\0\0\0"
        );
        assert_eq!(parser.parse_i128_be(),
            Ok(0x12345678fd78a84ece7aba26dd0f2999_i128)
        );
        assert_eq!(parser.parse_i128_be(),
            Ok(-9605457846724395475894107919101750112_i128)
        );
        assert!(parser.parse_i128_be().is_err());
    }

    #[test]
    fn parse_i128_le() {
        let mut parser = Parser::from_static(
            b"\x99\x29\x0f\xdd\x26\xba\x7a\xce\
              \x4e\xa8\x78\xfd\x78\x56\x34\x12\
              \xa0\x94\xa7\x57\x3f\x8f\x38\x38\
              \x74\x3a\x5e\x3f\x5d\x0e\xc6\xf8\
              \0\0\0\0\0"
        );
        assert_eq!(parser.parse_i128_le(),
            Ok(0x12345678fd78a84ece7aba26dd0f2999_i128)
        );
        assert_eq!(parser.parse_i128_le(),
            Ok(-9605457846724395475894107919101750112_i128)
        );
        assert!(parser.parse_i128_le().is_err());
    }

    #[test]
    fn parse_u128_be() {
        let mut parser = Parser::from_static(
            b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\
              \xce\x7a\xba\x26\xdd\x0f\x29\x99\
              \xf8\xc6\x0e\x5d\x3f\x5e\x3a\x74\
              \x38\x38\x8f\x3f\x57\xa7\x94\xa0\
              \0\0\0\0\0"
        );
        assert_eq!(parser.parse_u128_be(),
            Ok(0x12345678fd78a84ece7aba26dd0f2999_u128)
        );
        assert_eq!(parser.parse_u128_be(),
            Ok(0xf8c60e5d3f5e3a7438388f3f57a794a0_u128)
        );
        assert!(parser.parse_u128_be().is_err());
    }

    #[test]
    fn parse_u128_le() {
        let mut parser = Parser::from_static(
            b"\x99\x29\x0f\xdd\x26\xba\x7a\xce\
              \x4e\xa8\x78\xfd\x78\x56\x34\x12\
              \xa0\x94\xa7\x57\x3f\x8f\x38\x38\
              \x74\x3a\x5e\x3f\x5d\x0e\xc6\xf8\
              \0\0\0\0\0"
        );
        assert_eq!(parser.parse_u128_le(),
            Ok(0x12345678fd78a84ece7aba26dd0f2999_u128)
        );
        assert_eq!(parser.parse_u128_le(),
            Ok(0xf8c60e5d3f5e3a7438388f3f57a794a0_u128)
        );
        assert!(parser.parse_u128_le().is_err());
    }

    #[test]
    fn with_range() {
        let range = [0, 1, 2, 3, 4, 5_usize];
        let slice = &[1, 2, 3];

        for start in range {
            for end in range {
                for start in [
                    Bound::Unbounded,
                    Bound::Included(start),
                    Bound::Excluded(start)
                ] {
                    for end in [
                        Bound::Unbounded,
                        Bound::Included(end),
                        Bound::Excluded(end)
                    ] {
                        let bounds = (start, end);
                        assert_eq!(
                            slice.get(bounds),
                            Parser::try_with_range(
                                slice, bounds
                            ).as_ref().map(|p| p.peek_all()),
                            "{:?}", bounds
                        );
                    }
                }
            }
        }
    }
}