mavryk_data_encoding 0.6.0

Utilities for encoding/decoding data compatible with mavryk data encoding.
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
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
// Copyright (c) SimpleStaking, Viable Systems, Nomadic Labs and Tezedge Contributors
// SPDX-CopyrightText: 2022-2024 TriliTech <contact@trili.tech>
//
// SPDX-License-Identifier: MIT

use bitvec::slice::BitSlice;
use bitvec::{bitvec, order::Msb0, view::BitView};
pub use mavryk_data_encoding_derive::NomReader;
use nom::{
    branch::*,
    bytes::complete::*,
    combinator::*,
    error::ErrorKind,
    multi::*,
    number::{complete::*, Endianness},
    sequence::*,
    Err, InputLength, Parser, Slice,
};
use num_bigint::{BigInt, BigUint, Sign};

use crate::types::{Mumav, Zarith};

use self::error::{BoundedEncodingKind, DecodeError, DecodeErrorKind};

pub mod error {
    use std::{fmt::Write, str::Utf8Error};

    use nom::{
        error::{ErrorKind, FromExternalError},
        Offset,
    };

    use crate::bit_utils::BitsError;

    use super::NomInput;

    /// Decoding error
    #[derive(Debug, PartialEq)]
    pub struct DecodeError<I> {
        /// Input causing the error.
        pub(crate) input: I,
        /// Kind of the error.
        pub(crate) kind: DecodeErrorKind,
        /// Subsequent error, if any.
        pub(crate) other: Option<Box<DecodeError<I>>>,
    }

    /// Decoding error kind.
    #[derive(Debug, PartialEq, Eq)]
    pub enum DecodeErrorKind {
        /// Nom-specific error.
        Nom(ErrorKind),
        /// Error converting bytes to a UTF-8 string.
        Utf8(ErrorKind, Utf8Error),
        /// Boundary violation.
        Boundary(BoundedEncodingKind),
        /// Bits error
        Bits(BitsError),
        /// Field name
        Field(&'static str),
        /// Field name
        Variant(&'static str),
        /// Unknown/unsupported tag
        UnknownTag(String),
        /// Invalid tag
        InvalidTag(String),
    }

    /// Specific bounded encoding kind.
    #[derive(Debug, PartialEq, Eq, Clone)]
    pub enum BoundedEncodingKind {
        String,
        List,
        Dynamic,
        Bounded,
        Signature,
    }

    impl<'a> DecodeError<NomInput<'a>> {
        pub(crate) fn add_field(self, name: &'static str) -> Self {
            Self {
                input: <&[u8]>::clone(&self.input),
                kind: DecodeErrorKind::Field(name),
                other: Some(Box::new(self)),
            }
        }

        pub(crate) fn add_variant(self, name: &'static str) -> Self {
            Self {
                input: <&[u8]>::clone(&self.input),
                kind: DecodeErrorKind::Variant(name),
                other: Some(Box::new(self)),
            }
        }

        pub fn limit(input: NomInput<'a>, kind: BoundedEncodingKind) -> Self {
            Self {
                input,
                kind: DecodeErrorKind::Boundary(kind),
                other: None,
            }
        }

        pub fn unknown_tag(input: NomInput<'a>, tag: String) -> Self {
            Self {
                input,
                kind: DecodeErrorKind::UnknownTag(tag),
                other: None,
            }
        }

        pub fn invalid_tag(input: NomInput<'a>, tag: String) -> Self {
            Self {
                input,
                kind: DecodeErrorKind::InvalidTag(tag),
                other: None,
            }
        }

        pub fn get_unknown_tag(&self) -> Option<&String> {
            match self.kind {
                DecodeErrorKind::UnknownTag(ref tag) => Some(tag),
                _ => None,
            }
        }
    }

    impl<I> nom::error::ParseError<I> for DecodeError<I> {
        fn from_error_kind(input: I, kind: ErrorKind) -> Self {
            Self {
                input,
                kind: DecodeErrorKind::Nom(kind),
                other: None,
            }
        }

        fn append(input: I, kind: ErrorKind, other: Self) -> Self {
            Self {
                input,
                kind: DecodeErrorKind::Nom(kind),
                other: Some(Box::new(other)),
            }
        }
    }

    impl<I> nom::error::ParseError<(I, usize)> for DecodeError<I> {
        fn from_error_kind(input: (I, usize), kind: ErrorKind) -> Self {
            Self {
                input: input.0,
                kind: DecodeErrorKind::Nom(kind),
                other: None,
            }
        }

        fn append(input: (I, usize), kind: ErrorKind, other: Self) -> Self {
            Self {
                input: input.0,
                kind: DecodeErrorKind::Nom(kind),
                other: Some(Box::new(other)),
            }
        }
    }

    impl<I> FromExternalError<I, Utf8Error> for DecodeError<I> {
        fn from_external_error(input: I, kind: ErrorKind, e: Utf8Error) -> Self {
            Self {
                input,
                kind: DecodeErrorKind::Utf8(kind, e),
                other: None,
            }
        }
    }

    pub fn convert_error(input: NomInput, error: DecodeError<NomInput>) -> String {
        let mut res = String::new();
        let start = input.offset(error.input);
        let end = start + error.input.len();
        let _ = write!(res, "Error decoding bytes [{}..{}]", start, end);
        let _ = match error.kind {
            DecodeErrorKind::Nom(kind) => write!(res, " by nom parser `{:?}`", kind),
            DecodeErrorKind::Utf8(kind, e) => write!(res, " by nom parser `{:?}`: {}", kind, e),
            DecodeErrorKind::Boundary(kind) => {
                write!(
                    res,
                    " caused by boundary violation of encoding `{:?}`",
                    kind
                )
            }
            DecodeErrorKind::Field(name) => {
                write!(res, " while decoding field `{}`", name)
            }
            DecodeErrorKind::Variant(name) => {
                write!(res, " while decoding variant `{}`", name)
            }
            DecodeErrorKind::Bits(e) => write!(res, " while performing bits operation: {}", e),
            DecodeErrorKind::UnknownTag(tag) => write!(res, " caused by unsupported tag `{}`", tag),
            DecodeErrorKind::InvalidTag(tag) => write!(res, " caused by invalid tag `{}`", tag),
        };

        if let Some(other) = error.other {
            let _ = write!(res, "\n\nNext error:\n{}", convert_error(input, *other));
        }

        res
    }
}

/// Input for decoding.
pub type NomInput<'a> = &'a [u8];

/// Error type used to parameterize `nom`.
pub type NomError<'a> = error::DecodeError<NomInput<'a>>;

/// Nom result used in Tezedge (`&[u8]` as input, [NomError] as error type).
pub type NomResult<'a, T> = nom::IResult<NomInput<'a>, T, NomError<'a>>;

/// Traits defining message decoding using `nom` primitives.
pub trait NomReader<'a>: Sized {
    fn nom_read(input: &'a [u8]) -> NomResult<'a, Self>;
}

impl<'a> NomReader<'a> for Zarith {
    fn nom_read(bytes: &[u8]) -> NomResult<Self> {
        map(z_bignum, |big_int| big_int.into())(bytes)
    }
}

impl<'a> NomReader<'a> for Mumav {
    fn nom_read(bytes: &[u8]) -> NomResult<Self> {
        map(n_bignum, |big_uint| {
            BigInt::from_biguint(Sign::Plus, big_uint).into()
        })(bytes)
    }
}

/// Reads a boolean value.
#[inline(always)]
pub fn boolean(input: NomInput) -> NomResult<bool> {
    alt((
        map(tag(&[crate::types::BYTE_VAL_TRUE][..]), |_| true),
        map(tag(&[crate::types::BYTE_VAL_FALSE][..]), |_| false),
    ))(input)
}

/// Reads all available bytes into a [Vec]. Used in conjunction with [sized].
#[inline(always)]
pub fn bytes(input: NomInput) -> NomResult<Vec<u8>> {
    map(rest, Vec::from)(input)
}

/// Reads size encoded as 4-bytes big-endian unsigned.
#[inline(always)]
pub fn size(input: NomInput) -> NomResult<u32> {
    u32(Endianness::Big)(input)
}

/// Reads size encoded as 1-byte unsigned.
#[inline(always)]
pub fn short_size(input: NomInput) -> NomResult<u8> {
    u8(input)
}

/// Reads size encoded as 4-bytes big-endian unsigned, checking that it does not exceed the `max` value.
#[inline(always)]
fn bounded_size(kind: BoundedEncodingKind, max: usize) -> impl FnMut(NomInput) -> NomResult<u32> {
    move |input| {
        let i = <&[u8]>::clone(&input);
        let (input, size) = size(input)?;
        if size as usize <= max {
            Ok((input, size))
        } else {
            Err(Err::Error(DecodeError::limit(i, kind.clone())))
        }
    }
}

/// Reads Tesoz string encoded as a 32-bit length followed by the string bytes.
#[inline(always)]
pub fn string(input: NomInput) -> NomResult<String> {
    map_res(complete(length_data(size)), |bytes| {
        std::str::from_utf8(bytes).map(str::to_string)
    })(input)
}

/// Returns parser that reads Tesoz string encoded as a 32-bit length followed by the string bytes,
/// checking that the lengh of the string does not exceed `max`.
#[inline(always)]
pub fn bounded_string<'a>(max: usize) -> impl FnMut(NomInput<'a>) -> NomResult<'a, String> {
    map_res(
        complete(length_data(bounded_size(BoundedEncodingKind::String, max))),
        |bytes| std::str::from_utf8(bytes).map(str::to_string),
    )
}

/// Parser that applies specified parser to the fixed length slice of input.
#[inline(always)]
pub fn sized<'a, O, F>(size: usize, f: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
{
    map_parser(take(size), f)
}

/// Parses optional field. Byte `0x00` indicates absence of the field,
/// byte `0xff` preceedes encoding of the existing field.
#[inline(always)]
pub fn optional_field<'a, O, F>(parser: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, Option<O>>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
    O: Clone,
{
    alt((
        preceded(tag(0x00u8.to_be_bytes()), success(None)),
        preceded(tag(0xffu8.to_be_bytes()), map(parser, Some)),
    ))
}

/// Parses input by applying parser `f` to it.
#[inline(always)]
pub fn list<'a, O, F>(f: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, Vec<O>>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
{
    many0(f)
}

/// Parses input by applying parser `f` to it no more than `max` times.
#[inline(always)]
pub fn bounded_list<'a, O, F>(
    max: usize,
    mut f: F,
) -> impl FnMut(NomInput<'a>) -> NomResult<'a, Vec<O>>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
    O: Clone,
{
    move |input| {
        let (input, list) = fold_many_m_n(
            0,
            max,
            |i| f.parse(i),
            Vec::new,
            |mut list, item| {
                list.push(item);
                list
            },
        )(input)?;
        if input.input_len() > 0 {
            Err(Err::Error(DecodeError {
                input,
                kind: DecodeErrorKind::Boundary(BoundedEncodingKind::List),
                other: None,
            }))
        } else {
            Ok((input, list))
        }
    }
}

/// Parses dynamic block by reading 4-bytes size and applying the parser `f` to the following sequence of bytes of that size.
#[inline(always)]
pub fn dynamic<'a, O, F>(f: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
{
    length_value(size, all_consuming(f))
}

/// Parses short dynamic block by reading 1-byte size and applying the parser `f` to the following sequence of bytes of that size.
#[inline(always)]
pub fn short_dynamic<'a, O, F>(f: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
    O: Clone,
{
    length_value(short_size, all_consuming(f))
}

/// Parses dynamic block by reading 4-bytes size and applying the parser `f`
/// to the following sequence of bytes of that size. It also checks that the size
/// does not exceed the `max` value.
#[inline(always)]
pub fn bounded_dynamic<'a, O, F>(max: usize, f: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
    O: Clone,
{
    length_value(
        bounded_size(BoundedEncodingKind::Dynamic, max),
        all_consuming(f),
    )
}

/// Applies the parser `f` to the input, limiting it to `max` bytes at most.
#[inline(always)]
pub fn bounded<'a, O, F>(max: usize, mut f: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
    O: Clone,
{
    move |input: NomInput| {
        let max = std::cmp::min(max, input.input_len());
        let bounded = input.slice(std::ops::RangeTo { end: max });
        match f.parse(bounded) {
            Ok((rest, parsed)) => Ok((
                input.slice(std::ops::RangeFrom {
                    start: max - rest.input_len(),
                }),
                parsed,
            )),
            Err(Err::Error(DecodeError {
                input,
                kind: error::DecodeErrorKind::Nom(ErrorKind::Eof),
                other,
            })) => Err(Err::Error(DecodeError {
                input,
                kind: error::DecodeErrorKind::Boundary(BoundedEncodingKind::Bounded),
                other,
            })),
            e => e,
        }
    }
}

/// Reserves `size` trailing bytes of the input and applies parser to the rest of the input.
#[inline(always)]
pub fn reserve<'a, O, F>(size: usize, mut parser: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
{
    move |input| {
        let input_len = input.len();
        let reserved_len = input_len - std::cmp::min(input_len, size);
        let reserved_input = &input[..reserved_len];
        let (reserved_input, out) = parser(reserved_input)?;
        Ok((&input[reserved_len - reserved_input.len()..], out))
    }
}

/// Applies the `parser` to the input, addin field context to the error.
#[inline(always)]
pub fn field<'a, O, F>(
    name: &'static str,
    mut parser: F,
) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
{
    move |input| parser(input).map_err(|e| e.map(|e| e.add_field(name)))
}

/// Applies the `parser` to the input, adding enum variant context to the error.
#[inline(always)]
pub fn variant<'a, O, F>(
    name: &'static str,
    mut parser: F,
) -> impl FnMut(NomInput<'a>) -> NomResult<'a, O>
where
    F: FnMut(NomInput<'a>) -> NomResult<'a, O>,
{
    move |input| parser(input).map_err(|e| e.map(|e| e.add_variant(name)))
}

pub fn z_bignum(mut input: NomInput) -> NomResult<BigInt> {
    let mut bitslice_vec: Vec<&BitSlice<u8, Msb0>> = Vec::new();
    let mut has_next = true;
    let mut missing_bits = 0;
    let mut first = true;
    let mut neg = false;
    while has_next {
        let (new_input, byte) = take(1_u8)(input)?;
        input = new_input;
        let bits = byte.view_bits();
        has_next = bits[0];
        let skip_bits = if first {
            neg = bits[1];
            2
        } else {
            1
        };
        first = false;
        bitslice_vec.push(&bits[skip_bits..]);
        missing_bits += skip_bits;
    }
    let mut bitvec = bitvec![u8, Msb0; 0; missing_bits % 8];
    for bitslice in bitslice_vec.into_iter().rev() {
        bitvec.extend_from_bitslice(bitslice);
    }
    let sign = if neg { Sign::Minus } else { Sign::Plus };
    Ok((input, BigInt::from_bytes_be(sign, &bitvec.into_vec())))
}

pub fn n_bignum(mut input: NomInput) -> NomResult<BigUint> {
    let mut bitslice_vec: Vec<&BitSlice<u8, Msb0>> = Vec::new();
    let mut has_next = true;
    let mut missing_bits = 0;
    while has_next {
        let (new_input, byte) = take(1_u8)(input)?;
        input = new_input;
        let bits = byte.view_bits();
        has_next = bits[0];
        bitslice_vec.push(&bits[1..]);
        missing_bits += 1;
    }
    let mut bitvec = bitvec![u8, Msb0; 0; missing_bits % 8];
    for bitslice in bitslice_vec.into_iter().rev() {
        bitvec.extend_from_bitslice(bitslice);
    }
    Ok((input, BigUint::from_bytes_be(&bitvec.into_vec())))
}

#[cfg(test)]
mod test {
    use num_bigint::BigInt;
    use num_traits::FromPrimitive;

    use super::error::*;
    use super::*;

    #[test]
    fn test_boolean() {
        let res: NomResult<bool> = boolean(&[0xff]);
        assert_eq!(res, Ok((&[][..], true)));

        let res: NomResult<bool> = boolean(&[0x00]);
        assert_eq!(res, Ok((&[][..], false)));

        let res: NomResult<bool> = boolean(&[0x01]);
        res.expect_err("Error is expected");
    }

    #[test]
    fn test_size() {
        let input = &[0xff, 0xff, 0xff, 0xff];
        let res: NomResult<u32> = size(input);
        assert_eq!(res, Ok((&[][..], 0xffffffff)))
    }

    #[test]
    fn test_bounded_size() {
        let input = &[0x00, 0x00, 0x00, 0x10];

        let res: NomResult<u32> = bounded_size(BoundedEncodingKind::String, 100)(input);
        assert_eq!(res, Ok((&[][..], 0x10)));

        let res: NomResult<u32> = bounded_size(BoundedEncodingKind::String, 0x10)(input);
        assert_eq!(res, Ok((&[][..], 0x10)));

        let res: NomResult<u32> = bounded_size(BoundedEncodingKind::String, 0xf)(input);
        let err = res.expect_err("Error is expected");
        assert_eq!(err, limit_error(input, BoundedEncodingKind::String));
    }

    #[test]
    fn test_bytes() {
        let input = &[0, 1, 2, 3];
        let res: NomResult<Vec<u8>> = bytes(input);
        assert_eq!(res, Ok((&[][..], vec![0, 1, 2, 3])))
    }

    #[test]
    fn test_optional_field() {
        let res: NomResult<Option<u8>> = optional_field(u8)(&[0x00, 0x01][..]);
        assert_eq!(res, Ok((&[0x01][..], None)));

        let res: NomResult<Option<u8>> = optional_field(u8)(&[0xff, 0x01][..]);
        assert_eq!(res, Ok((&[][..], Some(0x01))));

        let res: NomResult<Option<u8>> = optional_field(u8)(&[0x01, 0x01][..]);
        res.expect_err("Error is expected");
    }

    #[test]
    fn test_string() {
        let input = &[0, 0, 0, 3, 0x78, 0x78, 0x78, 0xff];
        let res: NomResult<String> = string(input);
        assert_eq!(res, Ok((&[0xffu8][..], "xxx".to_string())))
    }

    #[test]
    fn test_bounded_string() {
        let input = &[0, 0, 0, 3, 0x78, 0x78, 0x78, 0xff];

        let res: NomResult<String> = bounded_string(3)(input);
        assert_eq!(res, Ok((&[0xffu8][..], "xxx".to_string())));

        let res: NomResult<String> = bounded_string(4)(input);
        assert_eq!(res, Ok((&[0xffu8][..], "xxx".to_string())));

        let res: NomResult<String> = bounded_string(2)(input);
        let err = res.expect_err("Error is expected");
        assert_eq!(err, limit_error(input, BoundedEncodingKind::String));
    }

    #[test]
    fn test_sized_bytes() {
        let input = &[0, 1, 2, 3, 4, 5, 6];
        let res: NomResult<Vec<u8>> = sized(4, bytes)(input);
        assert_eq!(res, Ok((&[4, 5, 6][..], vec![0, 1, 2, 3])))
    }

    #[test]
    fn test_list() {
        let input = &[0, 1, 2, 3, 4, 5];
        let res: NomResult<Vec<u16>> = list(u16(Endianness::Big))(input);
        assert_eq!(res, Ok((&[][..], vec![0x0001, 0x0203, 0x0405])));
    }

    #[test]
    fn test_bounded_list() {
        let input = &[0, 1, 2, 3, 4, 5];

        let res: NomResult<Vec<u16>> = bounded_list(4, u16(Endianness::Big))(input);
        assert_eq!(res, Ok((&[][..], vec![0x0001, 0x0203, 0x0405])));

        let res: NomResult<Vec<u16>> = bounded_list(3, u16(Endianness::Big))(input);
        assert_eq!(res, Ok((&[][..], vec![0x0001, 0x0203, 0x0405])));

        let res: NomResult<Vec<u16>> = bounded_list(2, u16(Endianness::Big))(input);
        let err = res.expect_err("Error is expected");
        assert_eq!(err, limit_error(&input[4..], BoundedEncodingKind::List));
    }

    #[test]
    fn test_dynamic() {
        let input = &[0, 0, 0, 3, 0x78, 0x78, 0x78, 0xff];

        let res: NomResult<Vec<u8>> = dynamic(bytes)(input);
        assert_eq!(res, Ok((&[0xffu8][..], vec![0x78; 3])));

        let res: NomResult<u8> = dynamic(u8)(input);
        res.expect_err("Error is expected");
    }

    #[test]
    fn test_short_dynamic() {
        let input = &[3, 0x78, 0x78, 0x78, 0xff];

        let res: NomResult<Vec<u8>> = short_dynamic(bytes)(input);
        assert_eq!(res, Ok((&[0xff_u8][..], vec![0x78_u8; 3])));

        let res: NomResult<u8> = short_dynamic(u8)(input);
        res.expect_err("Error is expected");
    }

    #[test]
    fn test_bounded_dynamic() {
        let input = &[0, 0, 0, 3, 0x78, 0x78, 0x78, 0xff];

        let res: NomResult<Vec<u8>> = bounded_dynamic(4, bytes)(input);
        assert_eq!(res, Ok((&[0xffu8][..], vec![0x78; 3])));

        let res: NomResult<Vec<u8>> = bounded_dynamic(3, bytes)(input);
        assert_eq!(res, Ok((&[0xffu8][..], vec![0x78; 3])));

        let res: NomResult<Vec<u8>> = bounded_dynamic(2, bytes)(input);
        let err = res.expect_err("Error is expected");
        assert_eq!(err, limit_error(input, BoundedEncodingKind::Dynamic));
    }

    #[test]
    fn test_bounded() {
        let input = &[1, 2, 3, 4, 5];

        let res: NomResult<Vec<u8>> = bounded(4, bytes)(input);
        assert_eq!(res, Ok((&[5][..], vec![1, 2, 3, 4])));

        let res: NomResult<Vec<u8>> = bounded(3, bytes)(input);
        assert_eq!(res, Ok((&[4, 5][..], vec![1, 2, 3])));

        let res: NomResult<Vec<u8>> = bounded(10, bytes)(input);
        assert_eq!(res, Ok((&[][..], vec![1, 2, 3, 4, 5])));

        let res: NomResult<u32> = bounded(3, u32(Endianness::Big))(input);
        let err = res.expect_err("Error is expected");
        assert_eq!(err, limit_error(&input[..3], BoundedEncodingKind::Bounded));
    }

    #[test]
    fn test_reserved() {
        let input = &[0, 0, 0, 1];
        let mut parser = tuple((reserve(1, list(u8)), u8));
        let (input, res) = parser(input).unwrap();
        assert_eq!(input.len(), 0);
        assert_eq!(res, (vec![0; 3], 1));
    }

    #[test]
    fn test_n_bignum() {
        let data = [
            ("0", "00"),
            ("1", "01"),
            ("7f", "7f"),
            ("80", "8001"),
            ("81", "8101"),
            ("ff", "ff01"),
            ("100", "8002"),
            ("101", "8102"),
            ("7fff", "ffff01"),
            ("8000", "808002"),
            ("8001", "818002"),
            ("ffff", "ffff03"),
            ("10000", "808004"),
            ("10001", "818004"),
        ];

        for (hex, enc) in data {
            let num = hex_to_biguint(hex);
            let input = hex::decode(enc).unwrap();
            let (input, dec) = n_bignum(&input).unwrap();
            assert!(input.is_empty());
            assert_eq!(dec, num);
        }
    }

    #[test]
    fn test_z_bignum() {
        let data = [
            ("0", "00"),
            ("1", "01"),
            ("7f", "bf01"),
            ("80", "8002"),
            ("81", "8102"),
            ("ff", "bf03"),
            ("100", "8004"),
            ("101", "8104"),
            ("7fff", "bfff03"),
            ("8000", "808004"),
            ("8001", "818004"),
            ("ffff", "bfff07"),
            ("10000", "808008"),
            ("10001", "818008"),
            ("9da879e", "9e9ed49d01"),
        ];

        for (hex, enc) in data {
            let num = hex_to_bigint(hex);
            let input = hex::decode(enc).unwrap();
            let (input, dec) = z_bignum(&input).unwrap();
            assert!(input.is_empty());
            assert_eq!(dec, num);
        }
    }

    #[test]
    fn test_neg_z_bignum() {
        let data = [
            ("-0", "00"),
            ("-1", "41"),
            ("-7f", "ff01"),
            ("-80", "c002"),
            ("-81", "c102"),
            ("-ff", "ff03"),
            ("-100", "c004"),
            ("-101", "c104"),
            ("-7fff", "ffff03"),
            ("-8000", "c08004"),
            ("-8001", "c18004"),
            ("-ffff", "ffff07"),
            ("-10000", "c08008"),
            ("-10001", "c18008"),
        ];

        for (hex, enc) in data {
            let num = hex_to_bigint(hex);
            let input = hex::decode(enc).unwrap();
            let (input, dec) = z_bignum(&input).unwrap();
            assert!(input.is_empty());
            assert_eq!(dec, num);
        }
    }

    fn hex_to_bigint(s: &str) -> BigInt {
        num_bigint::BigInt::from_i64(i64::from_str_radix(s, 16).unwrap()).unwrap()
    }

    fn hex_to_biguint(s: &str) -> BigUint {
        num_bigint::BigUint::from_u64(u64::from_str_radix(s, 16).unwrap()).unwrap()
    }

    fn limit_error(input: NomInput, kind: BoundedEncodingKind) -> Err<NomError> {
        Err::Error(DecodeError {
            input,
            kind: DecodeErrorKind::Boundary(kind),
            other: None,
        })
    }
}