about summary refs log tree commit diff
path: root/users/Profpatsch/netencode/netencode.rs
blob: 34a8fcef0990f720f87ed1b4ce25d052350e242b (plain) (blame)
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
extern crate exec_helpers;
extern crate nom;

use std::collections::HashMap;
use std::fmt::{Debug, Display};
use std::io::{Read, Write};

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum T {
    // Unit
    Unit,
    // Boolean
    N1(bool),
    // Naturals
    N3(u8),
    N6(u64),
    N7(u128),
    // Integers
    I3(i8),
    I6(i64),
    I7(i128),
    // Text
    // TODO: make into &str
    Text(String),
    // TODO: rename to Bytes
    Binary(Vec<u8>),
    // Tags
    // TODO: make into &str
    // TODO: rename to Tag
    Sum(Tag<String, T>),
    // TODO: make into &str
    Record(HashMap<String, T>),
    List(Vec<T>),
}

impl T {
    pub fn to_u<'a>(&'a self) -> U<'a> {
        match self {
            T::Unit => U::Unit,
            T::N1(b) => U::N1(*b),
            T::N3(u) => U::N3(*u),
            T::N6(u) => U::N6(*u),
            T::N7(u) => U::N7(*u),
            T::I3(i) => U::I3(*i),
            T::I6(i) => U::I6(*i),
            T::I7(i) => U::I7(*i),
            T::Text(t) => U::Text(t.as_str()),
            T::Binary(v) => U::Binary(v),
            T::Sum(Tag { tag, val }) => U::Sum(Tag {
                tag: tag.as_str(),
                val: Box::new(val.to_u()),
            }),
            T::Record(map) => U::Record(map.iter().map(|(k, v)| (k.as_str(), v.to_u())).collect()),
            T::List(l) => U::List(l.iter().map(|v| v.to_u()).collect::<Vec<U<'a>>>()),
        }
    }

    pub fn encode<'a>(&'a self) -> Vec<u8> {
        match self {
            // TODO: don’t go via U, inefficient
            o => o.to_u().encode(),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum U<'a> {
    Unit,
    // Boolean
    N1(bool),
    // Naturals
    N3(u8),
    N6(u64),
    N7(u128),
    // Integers
    I3(i8),
    I6(i64),
    I7(i128),
    // Text
    Text(&'a str),
    Binary(&'a [u8]),
    // TODO: the U-recursion we do here means we can’t be breadth-lazy anymore
    // like we originally planned; maybe we want to go `U<'a>` → `&'a [u8]` again?
    // Tags
    // TODO: rename to Tag
    Sum(Tag<&'a str, U<'a>>),
    Record(HashMap<&'a str, U<'a>>),
    List(Vec<U<'a>>),
}

impl<'a> U<'a> {
    pub fn encode(&self) -> Vec<u8> {
        let mut c = std::io::Cursor::new(vec![]);
        encode(&mut c, self);
        c.into_inner()
    }

    pub fn to_t(&self) -> T {
        match self {
            U::Unit => T::Unit,
            U::N1(b) => T::N1(*b),
            U::N3(u) => T::N3(*u),
            U::N6(u) => T::N6(*u),
            U::N7(u) => T::N7(*u),
            U::I3(i) => T::I3(*i),
            U::I6(i) => T::I6(*i),
            U::I7(i) => T::I7(*i),
            U::Text(t) => T::Text((*t).to_owned()),
            U::Binary(v) => T::Binary((*v).to_owned()),
            U::Sum(Tag { tag, val }) => T::Sum(Tag {
                tag: (*tag).to_owned(),
                val: Box::new(val.to_t()),
            }),
            U::Record(map) => T::Record(
                map.iter()
                    .map(|(k, v)| ((*k).to_owned(), v.to_t()))
                    .collect::<HashMap<String, T>>(),
            ),
            U::List(l) => T::List(l.iter().map(|v| v.to_t()).collect::<Vec<T>>()),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Tag<S, A> {
    // TODO: make into &str
    pub tag: S,
    pub val: Box<A>,
}

impl<S, A> Tag<S, A> {
    fn map<F, B>(self, f: F) -> Tag<S, B>
    where
        F: Fn(A) -> B,
    {
        Tag {
            tag: self.tag,
            val: Box::new(f(*self.val)),
        }
    }
}

fn encode_tag<W: Write>(w: &mut W, tag: &str, val: &U) -> std::io::Result<()> {
    write!(w, "<{}:{}|", tag.len(), tag)?;
    encode(w, val)?;
    Ok(())
}

pub fn encode<W: Write>(w: &mut W, u: &U) -> std::io::Result<()> {
    match u {
        U::Unit => write!(w, "u,"),
        U::N1(b) => {
            if *b {
                write!(w, "n1:1,")
            } else {
                write!(w, "n1:0,")
            }
        }
        U::N3(n) => write!(w, "n3:{},", n),
        U::N6(n) => write!(w, "n6:{},", n),
        U::N7(n) => write!(w, "n7:{},", n),
        U::I3(i) => write!(w, "i3:{},", i),
        U::I6(i) => write!(w, "i6:{},", i),
        U::I7(i) => write!(w, "i7:{},", i),
        U::Text(s) => {
            write!(w, "t{}:", s.len());
            w.write_all(s.as_bytes());
            write!(w, ",")
        }
        U::Binary(s) => {
            write!(w, "b{}:", s.len());
            w.write_all(&s);
            write!(w, ",")
        }
        U::Sum(Tag { tag, val }) => encode_tag(w, tag, val),
        U::Record(m) => {
            let mut c = std::io::Cursor::new(vec![]);
            for (k, v) in m {
                encode_tag(&mut c, k, v)?;
            }
            write!(w, "{{{}:", c.get_ref().len())?;
            w.write_all(c.get_ref())?;
            write!(w, "}}")
        }
        U::List(l) => {
            let mut c = std::io::Cursor::new(vec![]);
            for u in l {
                encode(&mut c, u)?;
            }
            write!(w, "[{}:", c.get_ref().len())?;
            w.write_all(c.get_ref())?;
            write!(w, "]")
        }
    }
}

pub fn text(s: String) -> T {
    T::Text(s)
}

pub fn t_from_stdin_or_die_user_error<'a>(prog_name: &'_ str) -> T {
    match t_from_stdin_or_die_user_error_with_rest(prog_name, &vec![]) {
        None => exec_helpers::die_user_error(prog_name, "stdin was empty"),
        Some((rest, t)) => {
            if rest.is_empty() {
                t
            } else {
                exec_helpers::die_user_error(
                    prog_name,
                    format!(
                        "stdin contained some soup after netencode value: {:?}",
                        String::from_utf8_lossy(&rest)
                    ),
                )
            }
        }
    }
}

/// Read a netencode value from stdin incrementally, return bytes that could not be read.
/// Nothing if there was nothing to read from stdin & no initial_bytes were provided.
/// These can be passed back as `initial_bytes` if more values should be read.
pub fn t_from_stdin_or_die_user_error_with_rest<'a>(
    prog_name: &'_ str,
    initial_bytes: &[u8],
) -> Option<(Vec<u8>, T)> {
    let mut chonker = Chunkyboi::new(std::io::stdin().lock(), 4096);
    // The vec to pass to the parser on each step
    let mut parser_vec: Vec<u8> = initial_bytes.to_vec();
    // whether stdin was already empty
    let mut was_empty: bool = false;
    loop {
        match chonker.next() {
            None => {
                if parser_vec.is_empty() {
                    return None;
                } else {
                    was_empty = true
                }
            }
            Some(Err(err)) => exec_helpers::die_temporary(
                prog_name,
                &format!("could not read from stdin: {:?}", err),
            ),
            Some(Ok(mut new_bytes)) => parser_vec.append(&mut new_bytes),
        }

        match parse::t_t(&parser_vec) {
            Ok((rest, t)) => return Some((rest.to_owned(), t)),
            Err(nom::Err::Incomplete(Needed)) => {
                if was_empty {
                    exec_helpers::die_user_error(
                        prog_name,
                        &format!(
                            "unable to parse netencode from stdin, input incomplete: {:?}",
                            parser_vec
                        ),
                    );
                }
                // read more from stdin and try parsing again
                continue;
            }
            Err(err) => exec_helpers::die_user_error(
                prog_name,
                &format!("unable to parse netencode from stdin: {:?}", err),
            ),
        }
    }
}

// iter helper
// TODO: put into its own module
struct Chunkyboi<T> {
    inner: T,
    buf: Vec<u8>,
}

impl<R: Read> Chunkyboi<R> {
    fn new(inner: R, chunksize: usize) -> Self {
        let buf = vec![0; chunksize];
        Chunkyboi { inner, buf }
    }
}

impl<R: Read> Iterator for Chunkyboi<R> {
    type Item = std::io::Result<Vec<u8>>;

    fn next(&mut self) -> Option<std::io::Result<Vec<u8>>> {
        match self.inner.read(&mut self.buf) {
            Ok(0) => None,
            Ok(read) => {
                // clone a new buffer so we can reuse the internal one
                Some(Ok(self.buf[..read].to_owned()))
            }
            Err(err) => Some(Err(err)),
        }
    }
}

pub mod parse {
    use super::{Tag, T, U};

    use std::collections::HashMap;
    use std::ops::Neg;
    use std::str::FromStr;

    use nom::branch::alt;
    use nom::bytes::streaming::{tag, take};
    use nom::character::streaming::{char, digit1};
    use nom::combinator::{flat_map, map, map_parser, map_res, opt};
    use nom::error::{context, ErrorKind, ParseError};
    use nom::sequence::tuple;
    use nom::IResult;

    fn unit_t(s: &[u8]) -> IResult<&[u8], ()> {
        let (s, _) = context("unit", tag("u,"))(s)?;
        Ok((s, ()))
    }

    fn usize_t(s: &[u8]) -> IResult<&[u8], usize> {
        context(
            "usize",
            map_res(map_res(digit1, |n| std::str::from_utf8(n)), |s| {
                s.parse::<usize>()
            }),
        )(s)
    }

    fn sized(begin: char, end: char) -> impl Fn(&[u8]) -> IResult<&[u8], &[u8]> {
        move |s: &[u8]| {
            // This is the point where we check the descriminator;
            // if the beginning char does not match, we can immediately return.
            let (s, _) = char(begin)(s)?;
            let (s, (len, _)) = tuple((usize_t, char(':')))(s)?;
            let (s, (res, _)) = tuple((take(len), char(end)))(s)?;
            Ok((s, res))
        }
    }

    fn uint_t<'a, I: FromStr + 'a>(t: &'static str) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], I> {
        move |s: &'a [u8]| {
            let (s, (_, _, int, _)) = tuple((
                tag(t.as_bytes()),
                char(':'),
                map_res(map_res(digit1, |n: &[u8]| std::str::from_utf8(n)), |s| {
                    s.parse::<I>()
                }),
                char(','),
            ))(s)?;
            Ok((s, int))
        }
    }

    fn bool_t<'a>() -> impl Fn(&'a [u8]) -> IResult<&'a [u8], bool> {
        context(
            "bool",
            alt((map(tag("n1:0,"), |_| false), map(tag("n1:1,"), |_| true))),
        )
    }

    fn int_t<'a, I: FromStr + Neg<Output = I>>(
        t: &'static str,
    ) -> impl Fn(&'a [u8]) -> IResult<&[u8], I> {
        context(t, move |s: &'a [u8]| {
            let (s, (_, _, neg, int, _)) = tuple((
                tag(t.as_bytes()),
                char(':'),
                opt(char('-')),
                map_res(map_res(digit1, |n: &[u8]| std::str::from_utf8(n)), |s| {
                    s.parse::<I>()
                }),
                char(','),
            ))(s)?;
            let res = match neg {
                Some(_) => -int,
                None => int,
            };
            Ok((s, res))
        })
    }

    fn tag_t(s: &[u8]) -> IResult<&[u8], Tag<String, T>> {
        // recurses into the main parser
        map(tag_g(t_t), |Tag { tag, val }| Tag {
            tag: tag.to_string(),
            val,
        })(s)
    }

    fn tag_g<'a, P, O>(inner: P) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], Tag<&'a str, O>>
    where
        P: Fn(&'a [u8]) -> IResult<&'a [u8], O>,
    {
        move |s: &[u8]| {
            let (s, tag) = sized('<', '|')(s)?;
            let (s, val) = inner(s)?;
            Ok((
                s,
                Tag {
                    tag: std::str::from_utf8(tag)
                        .map_err(|_| nom::Err::Failure((s, ErrorKind::Char)))?,
                    val: Box::new(val),
                },
            ))
        }
    }

    /// parse text scalar (`t5:hello,`)
    fn text(s: &[u8]) -> IResult<&[u8], T> {
        let (s, res) = text_g(s)?;
        Ok((s, T::Text(res.to_string())))
    }

    fn text_g(s: &[u8]) -> IResult<&[u8], &str> {
        let (s, res) = sized('t', ',')(s)?;
        Ok((
            s,
            std::str::from_utf8(res).map_err(|_| nom::Err::Failure((s, ErrorKind::Char)))?,
        ))
    }

    fn binary<'a>() -> impl Fn(&'a [u8]) -> IResult<&'a [u8], T> {
        map(binary_g(), |b| T::Binary(b.to_owned()))
    }

    fn binary_g() -> impl Fn(&[u8]) -> IResult<&[u8], &[u8]> {
        sized('b', ',')
    }

    fn list_t(s: &[u8]) -> IResult<&[u8], Vec<T>> {
        list_g(t_t)(s)
    }

    /// Wrap the inner parser of an `many0`/`fold_many0`, so that the parser
    /// is not called when the `s` is empty already, preventing it from
    /// returning `Incomplete` on streaming parsing.
    fn inner_no_empty_string<'a, P, O>(inner: P) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], O>
    where
        O: Clone,
        P: Fn(&'a [u8]) -> IResult<&'a [u8], O>,
    {
        move |s: &'a [u8]| {
            if s.is_empty() {
                // This is a bit hacky, `many0` considers the inside done
                // when a parser returns `Err::Error`, ignoring the actual error content
                Err(nom::Err::Error((s, nom::error::ErrorKind::Many0)))
            } else {
                inner(s)
            }
        }
    }

    fn list_g<'a, P, O>(inner: P) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], Vec<O>>
    where
        O: Clone,
        P: Fn(&'a [u8]) -> IResult<&'a [u8], O>,
    {
        map_parser(
            sized('[', ']'),
            nom::multi::many0(inner_no_empty_string(inner)),
        )
    }

    fn record_t<'a>(s: &'a [u8]) -> IResult<&'a [u8], HashMap<String, T>> {
        let (s, r) = record_g(t_t)(s)?;
        Ok((
            s,
            r.into_iter()
                .map(|(k, v)| (k.to_string(), v))
                .collect::<HashMap<_, _>>(),
        ))
    }

    fn record_g<'a, P, O>(inner: P) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], HashMap<&'a str, O>>
    where
        O: Clone,
        P: Fn(&'a [u8]) -> IResult<&'a [u8], O>,
    {
        move |s: &'a [u8]| {
            let (s, map) = map_parser(
                sized('{', '}'),
                nom::multi::fold_many0(
                    inner_no_empty_string(tag_g(&inner)),
                    HashMap::new(),
                    |mut acc: HashMap<_, _>, Tag { tag, mut val }| {
                        // ignore earlier tags with the same name
                        // according to netencode spec
                        let _ = acc.insert(tag, *val);
                        acc
                    },
                ),
            )(s)?;
            if map.is_empty() {
                // records must not be empty, according to the spec
                Err(nom::Err::Failure((s, nom::error::ErrorKind::Many1)))
            } else {
                Ok((s, map))
            }
        }
    }

    pub fn u_u(s: &[u8]) -> IResult<&[u8], U> {
        alt((
            map(text_g, U::Text),
            map(binary_g(), U::Binary),
            map(unit_t, |()| U::Unit),
            map(tag_g(u_u), |t| U::Sum(t)),
            map(list_g(u_u), U::List),
            map(record_g(u_u), U::Record),
            map(bool_t(), |u| U::N1(u)),
            map(uint_t("n3"), |u| U::N3(u)),
            map(uint_t("n6"), |u| U::N6(u)),
            map(uint_t("n7"), |u| U::N7(u)),
            map(int_t("i3"), |u| U::I3(u)),
            map(int_t("i6"), |u| U::I6(u)),
            map(int_t("i7"), |u| U::I7(u)),
            // less common
            map(uint_t("n2"), |u| U::N3(u)),
            map(uint_t("n4"), |u| U::N6(u)),
            map(uint_t("n5"), |u| U::N6(u)),
            map(int_t("i1"), |u| U::I3(u)),
            map(int_t("i2"), |u| U::I3(u)),
            map(int_t("i4"), |u| U::I6(u)),
            map(int_t("i5"), |u| U::I6(u)),
            // TODO: 8, 9 not supported
        ))(s)
    }

    pub fn t_t(s: &[u8]) -> IResult<&[u8], T> {
        alt((
            text,
            binary(),
            map(unit_t, |_| T::Unit),
            map(tag_t, |t| T::Sum(t)),
            map(list_t, |l| T::List(l)),
            map(record_t, |p| T::Record(p)),
            map(bool_t(), |u| T::N1(u)),
            // 8, 64 and 128 bit
            map(uint_t("n3"), |u| T::N3(u)),
            map(uint_t("n6"), |u| T::N6(u)),
            map(uint_t("n7"), |u| T::N7(u)),
            map(int_t("i3"), |u| T::I3(u)),
            map(int_t("i6"), |u| T::I6(u)),
            map(int_t("i7"), |u| T::I7(u)),
            // less common
            map(uint_t("n2"), |u| T::N3(u)),
            map(uint_t("n4"), |u| T::N6(u)),
            map(uint_t("n5"), |u| T::N6(u)),
            map(int_t("i1"), |u| T::I3(u)),
            map(int_t("i2"), |u| T::I3(u)),
            map(int_t("i4"), |u| T::I6(u)),
            map(int_t("i5"), |u| T::I6(u)),
            // TODO: 8, 9 not supported
        ))(s)
    }

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

        #[test]
        fn test_parse_unit_t() {
            assert_eq!(unit_t("u,".as_bytes()), Ok(("".as_bytes(), ())));
        }

        #[test]
        fn test_parse_bool_t() {
            assert_eq!(bool_t()("n1:0,".as_bytes()), Ok(("".as_bytes(), false)));
            assert_eq!(bool_t()("n1:1,".as_bytes()), Ok(("".as_bytes(), true)));
        }

        #[test]
        fn test_parse_usize_t() {
            assert_eq!(usize_t("32foo".as_bytes()), Ok(("foo".as_bytes(), 32)));
        }

        #[test]
        fn test_parse_int_t() {
            assert_eq!(
                uint_t::<u8>("n3")("n3:42,abc".as_bytes()),
                Ok(("abc".as_bytes(), 42))
            );
            assert_eq!(
                uint_t::<u8>("n3")("n3:1024,abc".as_bytes()),
                Err(nom::Err::Error((
                    "1024,abc".as_bytes(),
                    nom::error::ErrorKind::MapRes
                )))
            );
            assert_eq!(
                int_t::<i64>("i6")("i6:-23,abc".as_bytes()),
                Ok(("abc".as_bytes(), -23))
            );
            assert_eq!(
                int_t::<i128>("i3")("i3:0,:abc".as_bytes()),
                Ok((":abc".as_bytes(), 0))
            );
            assert_eq!(
                uint_t::<u8>("n7")("n7:09,".as_bytes()),
                Ok(("".as_bytes(), 9))
            );
            // assert_eq!(
            //     length("c"),
            //     Err(nom::Err::Error(("c", nom::error::ErrorKind::Digit)))
            // );
            // assert_eq!(
            //     length(":"),
            //     Err(nom::Err::Error((":", nom::error::ErrorKind::Digit)))
            // );
        }

        #[test]
        fn test_parse_text() {
            assert_eq!(
                text("t5:hello,".as_bytes()),
                Ok(("".as_bytes(), T::Text("hello".to_owned()))),
                "{}",
                r"t5:hello,"
            );
            assert_eq!(
                text("t4:fo".as_bytes()),
                // The content of the text should be 4 long
                Err(nom::Err::Incomplete(nom::Needed::Size(4))),
                "{}",
                r"t4:fo,"
            );
            assert_eq!(
                text("t9:今日は,".as_bytes()),
                Ok(("".as_bytes(), T::Text("今日は".to_owned()))),
                "{}",
                r"t9:今日は,"
            );
        }

        #[test]
        fn test_parse_binary() {
            assert_eq!(
                binary()("b5:hello,".as_bytes()),
                Ok(("".as_bytes(), T::Binary(Vec::from("hello".to_owned())))),
                "{}",
                r"b5:hello,"
            );
            assert_eq!(
                binary()("b4:fo".as_bytes()),
                // The content of the byte should be 4 long
                Err(nom::Err::Incomplete(nom::Needed::Size(4))),
                "{}",
                r"b4:fo,"
            );
            assert_eq!(
                binary()("b4:foob".as_bytes()),
                // The content is 4 bytes now, but the finishing , is missing
                Err(nom::Err::Incomplete(nom::Needed::Size(1))),
                "{}",
                r"b4:fo,"
            );
            assert_eq!(
                binary()("b9:今日は,".as_bytes()),
                Ok(("".as_bytes(), T::Binary(Vec::from("今日は".as_bytes())))),
                "{}",
                r"b9:今日は,"
            );
        }

        #[test]
        fn test_list() {
            assert_eq!(
                list_t("[0:]".as_bytes()),
                Ok(("".as_bytes(), vec![])),
                "{}",
                r"[0:]"
            );
            assert_eq!(
                list_t("[6:u,u,u,]".as_bytes()),
                Ok(("".as_bytes(), vec![T::Unit, T::Unit, T::Unit,])),
                "{}",
                r"[6:u,u,u,]"
            );
            assert_eq!(
                list_t("[15:u,[7:t3:foo,]u,]".as_bytes()),
                Ok((
                    "".as_bytes(),
                    vec![T::Unit, T::List(vec![T::Text("foo".to_owned())]), T::Unit,]
                )),
                "{}",
                r"[15:u,[7:t3:foo,]u,]"
            );
        }

        #[test]
        fn test_record() {
            assert_eq!(
                record_t("{21:<1:a|u,<1:b|u,<1:c|u,}".as_bytes()),
                Ok((
                    "".as_bytes(),
                    vec![
                        ("a".to_owned(), T::Unit),
                        ("b".to_owned(), T::Unit),
                        ("c".to_owned(), T::Unit),
                    ]
                    .into_iter()
                    .collect::<HashMap<String, T>>()
                )),
                "{}",
                r"{21:<1:a|u,<1:b|u,<1:c|u,}"
            );
            // duplicated keys are ignored (first is taken)
            assert_eq!(
                record_t("{25:<1:a|u,<1:b|u,<1:a|i1:-1,}".as_bytes()),
                Ok((
                    "".as_bytes(),
                    vec![("a".to_owned(), T::I3(-1)), ("b".to_owned(), T::Unit),]
                        .into_iter()
                        .collect::<HashMap<_, _>>()
                )),
                "{}",
                r"{25:<1:a|u,<1:b|u,<1:a|i1:-1,}"
            );
            // empty records are not allowed
            assert_eq!(
                record_t("{0:}".as_bytes()),
                Err(nom::Err::Failure((
                    "".as_bytes(),
                    nom::error::ErrorKind::Many1
                ))),
                "{}",
                r"{0:}"
            );
        }

        #[test]
        fn test_parse() {
            assert_eq!(
                t_t("n3:255,".as_bytes()),
                Ok(("".as_bytes(), T::N3(255))),
                "{}",
                r"n3:255,"
            );
            assert_eq!(
                t_t("t6:halloo,".as_bytes()),
                Ok(("".as_bytes(), T::Text("halloo".to_owned()))),
                "{}",
                r"t6:halloo,"
            );
            assert_eq!(
                t_t("<3:foo|t6:halloo,".as_bytes()),
                Ok((
                    "".as_bytes(),
                    T::Sum(Tag {
                        tag: "foo".to_owned(),
                        val: Box::new(T::Text("halloo".to_owned()))
                    })
                )),
                "{}",
                r"<3:foo|t6:halloo,"
            );
            // { a: Unit
            // , foo: List <A: Unit | B: List i3> }
            assert_eq!(
                t_t("{52:<1:a|u,<3:foo|[33:<1:A|u,<1:A|n1:1,<1:B|[7:i3:127,]]}".as_bytes()),
                Ok((
                    "".as_bytes(),
                    T::Record(
                        vec![
                            ("a".to_owned(), T::Unit),
                            (
                                "foo".to_owned(),
                                T::List(vec![
                                    T::Sum(Tag {
                                        tag: "A".to_owned(),
                                        val: Box::new(T::Unit)
                                    }),
                                    T::Sum(Tag {
                                        tag: "A".to_owned(),
                                        val: Box::new(T::N1(true))
                                    }),
                                    T::Sum(Tag {
                                        tag: "B".to_owned(),
                                        val: Box::new(T::List(vec![T::I3(127)]))
                                    }),
                                ])
                            )
                        ]
                        .into_iter()
                        .collect::<HashMap<String, T>>()
                    )
                )),
                "{}",
                r"{52:<1:a|u,<3:foo|[33:<1:A|u,<1:A|n1:1,<1:B|[7:i3:127,]]}"
            );
        }
    }
}

pub mod dec {
    use super::*;
    use std::collections::HashMap;

    pub struct DecodeError(pub String);

    pub trait Decoder<'a> {
        type A;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError>;
    }

    /// Any netencode, as `T`.
    #[derive(Clone, Copy)]
    pub struct AnyT;
    /// Any netencode, as `U`.
    #[derive(Clone, Copy)]
    pub struct AnyU;

    impl<'a> Decoder<'a> for AnyT {
        type A = T;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            Ok(u.to_t())
        }
    }

    impl<'a> Decoder<'a> for AnyU {
        type A = U<'a>;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            Ok(u)
        }
    }

    /// A text
    #[derive(Clone, Copy)]
    pub struct Text;

    /// A bytestring
    // TODO: rename to Bytes
    #[derive(Clone, Copy)]
    pub struct Binary;

    impl<'a> Decoder<'a> for Text {
        type A = &'a str;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            match u {
                U::Text(t) => Ok(t),
                other => Err(DecodeError(format!("Cannot decode {:?} into Text", other))),
            }
        }
    }

    impl<'a> Decoder<'a> for Binary {
        type A = &'a [u8];
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            match u {
                U::Binary(b) => Ok(b),
                other => Err(DecodeError(format!(
                    "Cannot decode {:?} into Binary",
                    other
                ))),
            }
        }
    }

    /// Any scalar, converted to bytes.
    #[derive(Clone, Copy)]
    pub struct ScalarAsBytes;

    impl<'a> Decoder<'a> for ScalarAsBytes {
        type A = Vec<u8>;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            match u {
                U::N3(u) => Ok(format!("{}", u).into_bytes()),
                U::N6(u) => Ok(format!("{}", u).into_bytes()),
                U::N7(u) => Ok(format!("{}", u).into_bytes()),
                U::I3(i) => Ok(format!("{}", i).into_bytes()),
                U::I6(i) => Ok(format!("{}", i).into_bytes()),
                U::I7(i) => Ok(format!("{}", i).into_bytes()),
                U::Text(t) => Ok(t.as_bytes().to_owned()),
                U::Binary(b) => Ok(b.to_owned()),
                o => Err(DecodeError(format!("Cannot decode {:?} into scalar", o))),
            }
        }
    }

    /// A map of Ts (TODO: rename to map)
    #[derive(Clone, Copy)]
    pub struct Record<T>(pub T);

    impl<'a, Inner> Decoder<'a> for Record<Inner>
    where
        Inner: Decoder<'a>,
    {
        type A = HashMap<&'a str, Inner::A>;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            match u {
                U::Record(map) => map
                    .into_iter()
                    .map(|(k, v)| self.0.dec(v).map(|v2| (k, v2)))
                    .collect::<Result<Self::A, _>>(),
                o => Err(DecodeError(format!("Cannot decode {:?} into record", o))),
            }
        }
    }

    /// Assume a record and project out the field with the given name and type.
    #[derive(Clone, Copy)]
    pub struct RecordDot<'a, T> {
        pub field: &'a str,
        pub inner: T,
    }

    impl<'a, Inner> Decoder<'a> for RecordDot<'_, Inner>
    where
        Inner: Decoder<'a> + Clone,
    {
        type A = Inner::A;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            match Record(self.inner.clone()).dec(u) {
                Ok(mut map) => match map.remove(self.field) {
                    Some(inner) => Ok(inner),
                    None => Err(DecodeError(format!(
                        "Cannot find `{}` in record map",
                        self.field
                    ))),
                },
                Err(err) => Err(err),
            }
        }
    }

    /// Equals one of the listed `A`s exactly, after decoding.
    #[derive(Clone)]
    pub struct OneOf<T, A> {
        pub inner: T,
        pub list: Vec<A>,
    }

    impl<'a, Inner> Decoder<'a> for OneOf<Inner, Inner::A>
    where
        Inner: Decoder<'a>,
        Inner::A: Display + Debug + PartialEq,
    {
        type A = Inner::A;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            match self.inner.dec(u) {
                Ok(inner) => match self.list.iter().any(|x| x.eq(&inner)) {
                    true => Ok(inner),
                    false => Err(DecodeError(format!(
                        "{} is not one of {:?}",
                        inner, self.list
                    ))),
                },
                Err(err) => Err(err),
            }
        }
    }

    /// Try decoding as `T`.
    #[derive(Clone)]
    pub struct Try<T>(pub T);

    impl<'a, Inner> Decoder<'a> for Try<Inner>
    where
        Inner: Decoder<'a>,
    {
        type A = Option<Inner::A>;
        fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> {
            match self.0.dec(u) {
                Ok(inner) => Ok(Some(inner)),
                Err(err) => Ok(None),
            }
        }
    }
}