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
mod context;
mod data;
mod expr;
mod parser;
pub mod prelude;
mod pvec;
mod render;
pub mod samples;

pub use context::Context;
pub use context::Scope;
pub use data::Data;
pub use expr::Expr;
pub use parser::ParseError;
pub use parser::Pattern;
pub use pvec::PatternVec;
pub use render::render;
pub use render::Render;
pub use render::Renderable;

fn err<T, S: Into<String>>(s: S) -> Result<T, ParseError> {
    Err(ParseError::Other(s.into()))
}

pub enum Endian {
    Little,
    Big,
}

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

    #[test]
    fn sample() {
        let bytes = render((1234u64, 50000u16));
        let parser = {
            use prelude::*;

            all_of((magic_u64(1234), U8, U8))
        };

        let data = parser.parse(&bytes).unwrap();

        assert_eq!(
            data,
            Data::fseq(vec![Data::Int(1234), Data::Int(80), Data::Int(195),])
        );
        assert_eq!(50000, (195 << 8) + 80);
    }

    #[test]
    fn alternatives() {
        let bytes = render((1234u64, 50000u16));
        let parser = {
            use prelude::*;
            any_of((
                all_of((all_of(()).mapval("big-endian"), be_magic_u64(1234), BE_U16)),
                all_of((
                    all_of(()).mapval("little-endian"),
                    le_magic_u64(1234),
                    U8,
                    U8,
                )),
            ))
        };

        let data = parser.parse(&bytes).unwrap();

        assert_eq!(
            data,
            Data::fseq(vec![
                "little-endian".into(),
                Data::Int(1234),
                Data::Int(80),
                Data::Int(195),
            ])
        );
        assert_eq!(50000, (195 << 8) + 80);
    }

    #[test]
    fn list() {
        let bytes = render((
            1234u32, // magic
            3u32,    // length - 1
            (
                777u64, 888u64, 999u64, 444u64, // data to be parsed
                555u64, 666u64, // just some extra data
            ),
        ));

        enum Key {
            LENGTH,
        }

        let parser = {
            use prelude::*;
            all_of((
                le_magic_u32(1234),
                // after magic, the header specifies length of
                // upcoming array - 1
                // Store the computed length to 'Key::LENGTH'
                U32.add(1).store(Key::LENGTH as i64),
                // finally, specify an array of u64,
                // whose length is determined by the Key::LENGTH
                // value stored above
                array_of(LE_U64, getvar(Key::LENGTH as i64)),
            ))
        };

        let data = parser.parse(&bytes).unwrap();

        assert_eq!(
            data,
            Data::fseq(vec![
                Data::Int(1234), // magic,
                Data::Int(4),    // length
                // the actual array
                Data::fseq(vec![
                    Data::Int(777),
                    Data::Int(888),
                    Data::Int(999),
                    Data::Int(444),
                ]),
            ])
        );

        // try again, but with lenght a little longer
        let bytes = render((
            1234u32, // magic
            4u32,    // length - 1
            (
                777u64, 888u64, 999u64, 444u64, // data to be parsed
                555u64, 666u64, // just some extra data
            ),
        ));

        let data = parser.parse(&bytes).unwrap();

        assert_eq!(
            data,
            Data::fseq(vec![
                Data::Int(1234), // magic,
                Data::Int(5),    // length
                // the actual array
                Data::fseq(vec![
                    Data::Int(777),
                    Data::Int(888),
                    Data::Int(999),
                    Data::Int(444),
                    Data::Int(555),
                ]),
            ])
        );
    }
}