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
#![feature(box_patterns)]
#![feature(box_syntax)]

pub mod slow_parser;
pub mod parser;


#[cfg(test)]
mod tests {
    use super::slow_parser::*;
    #[test]
    fn test_parse_char() {
        assert_eq!(parse_char('a').run("abcd"), Ok('a'));
        assert_ne!(parse_char('a').run("bcd"), Ok('a'));
    }

    #[test]
    fn test_parse_str() {
        assert_eq!(parse_str("null").run("null"), Ok("null".into()));
        assert_ne!(parse_str("true").run("false"), Ok("true".into()));

        let mut p = parse_one_of([parse_str("true"), parse_str("truck")]);
        assert_eq!(p.run("truck"), Ok("truck".into()));
        assert_eq!(p.run("true"), Ok("true".into()));
    }

    #[test]
    fn test_parse_bool() {
        assert_eq!(parse_bool().run("true"), Ok(true));
        assert_eq!(parse_bool().run("false"), Ok(false));
        assert_ne!(parse_bool().run("False"), Ok(false));
    }

    #[test]
    fn test_parse_float() {
        assert_eq!(parse_float().run("123"), Ok(123f64));
        assert_eq!(parse_float().run("-123.43"), Ok(-123.43));
        assert_eq!(parse_float().run("0.23"), Ok(0.23));
        assert_eq!(parse_float().run("23."), Ok(23.));
    }

    #[test]
    fn test_parse_list_of() {
        let parse_list_of_float = || parse_list_of(|| parse_float());

        assert_eq!(parse_list_of_float().run("[1.2, 2.3, 3.4]"), Ok(vec![1.2, 2.3, 3.4]));
        assert_eq!(parse_list_of_float().run("[1.2]"), Ok(vec![1.2]));
        assert_eq!(parse_list_of_float().run("[]"), Ok(vec![]));

        let parse_list_of_list_of_float = ||
            parse_list_of(||
                parse_list_of_float())
                .run("[[1.2 , 2.2 ], [ 2.3], [3.4,1.0,2.0 ,3.0]]");
        assert_eq!(
            parse_list_of_list_of_float(),
            Ok(vec![vec![1.2, 2.2], vec![2.3], vec![3.4, 1.0, 2.0, 3.0]])
        );
    }

    use super::parser::*;

    #[test]
    fn test_char_p() {
        let cp = char_p('a');
        assert_eq!(cp.run("abcd"), Ok('a'));
        assert_ne!(cp.run("bcd"), Ok('a'));
    }

    #[test]
    fn test_str_p() {
        assert_eq!(str_p("null").run("null"), Ok("null".to_string()));
        assert_ne!(str_p("true").run("false"), Ok("true".to_string()));

        let p = one_of_p([str_p("true"), str_p("truck")]);
        assert_eq!(p.run("truck"), Ok("truck".into()));
        assert_eq!(p.run("true"), Ok("true".into()));
    }

    #[test]
    fn test_bool_p() {
        let p = bool_p();
        assert_eq!(p.run("true"), Ok(true));
        assert_eq!(p.run("false"), Ok(false));
        assert_ne!(p.run("False"), Ok(false));
    }

    #[test]
    fn test_float_p() {
        assert_eq!(float_p().run("123"), Ok(123f64));
        assert_eq!(float_p().run("-123.43"), Ok(-123.43));
        assert_eq!(float_p().run("0.23"), Ok(0.23));
        assert_eq!(float_p().run("23."), Ok(23.));
    }

    #[test]
    fn test_list_of_p() {
        let parse_list_of_float = list_square_p(float_p());

        assert_eq!(parse_list_of_float.run("[1.2, 2.3, 3.4]"), Ok(vec![1.2, 2.3, 3.4]));
        assert_eq!(parse_list_of_float.run("[1.2]"), Ok(vec![1.2]));
        assert_eq!(parse_list_of_float.run("[]"), Ok(vec![]));

        let parse_list_of_list_of_float
            = list_square_p(parse_list_of_float);
        assert_eq!(
            parse_list_of_list_of_float
                .run("[[1.2 , 2.2 ], [ 2.3], [3.4,1.0,2.0 ,3.0]]"),
            Ok(vec![vec![1.2, 2.2], vec![2.3], vec![3.4, 1.0, 2.0, 3.0]])
        );
    }
}