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
use std::io::{Read,Seek};

use crate::nom::{
    IResult,
    many0,
    many_till,
    all_consuming,
};

use crate::{
    Decode,
    Error,
    ScriptField,
};

use super::{TngThing, TngSection, Tng};

impl Decode for Tng {
    fn decode<Source>(source: &mut Source) -> Result<Self, Error> where
        Source: Read + Seek
    {
        let mut input = Vec::new();
        source.read_to_end(&mut input)?;
        let (_, tng) = all_consuming(Tng::decode_tng)(&input)?;
        Ok(tng)
    }
}

impl Tng {
    pub fn decode_tng(input: &[u8]) -> IResult<&[u8], Tng, Error> {
        let (input, version) = ScriptField::decode_field_named("Version")(input)?;
        let (input, sections) = many0(Self::decode_tng_section)(input)?;

        Ok(
            (
                input,
                Tng {
                    version: version,
                    sections: sections,
                }
            )
        )
    }

    pub fn decode_tng_section(input: &[u8]) -> IResult<&[u8], TngSection, Error> {
        let (input, section_start) = ScriptField::decode_field_named("XXXSectionStart")(input)?;
        let (input, (things, _end)) = many_till(Self::decode_tng_thing, ScriptField::decode_field_named("XXXSectionEnd"))(input)?;

        Ok(
            (
                input,
                TngSection {
                    section_start: section_start,
                    things: things,
                }
            )
        )
    }

    pub fn decode_tng_thing(input: &[u8]) -> IResult<&[u8], TngThing, Error> {
        let (input, new_thing) = ScriptField::decode_field_named("NewThing")(input)?;
        let (input, (fields, _end)) = many_till(ScriptField::decode_field, ScriptField::decode_field_named("EndThing"))(input)?;

        Ok(
            (
                input,
                TngThing {
                    new_thing: new_thing,
                    fields: fields
                }
            )
        )
    }
}