fable_format/tng/
decode.rs

1use std::io::{Read,Seek};
2
3use crate::nom::{
4    IResult,
5    many0,
6    many_till,
7    all_consuming,
8};
9
10use crate::{
11    Decode,
12    Error,
13    ScriptField,
14};
15
16use super::{TngThing, TngSection, Tng};
17
18impl Decode for Tng {
19    fn decode<Source>(source: &mut Source) -> Result<Self, Error> where
20        Source: Read + Seek
21    {
22        let mut input = Vec::new();
23        source.read_to_end(&mut input)?;
24        let (_, tng) = all_consuming(Tng::decode_tng)(&input)?;
25        Ok(tng)
26    }
27}
28
29impl Tng {
30    pub fn decode_tng(input: &[u8]) -> IResult<&[u8], Tng, Error> {
31        let (input, version) = ScriptField::decode_field_named("Version")(input)?;
32        let (input, sections) = many0(Self::decode_tng_section)(input)?;
33
34        Ok(
35            (
36                input,
37                Tng {
38                    version: version,
39                    sections: sections,
40                }
41            )
42        )
43    }
44
45    pub fn decode_tng_section(input: &[u8]) -> IResult<&[u8], TngSection, Error> {
46        let (input, section_start) = ScriptField::decode_field_named("XXXSectionStart")(input)?;
47        let (input, (things, _end)) = many_till(Self::decode_tng_thing, ScriptField::decode_field_named("XXXSectionEnd"))(input)?;
48
49        Ok(
50            (
51                input,
52                TngSection {
53                    section_start: section_start,
54                    things: things,
55                }
56            )
57        )
58    }
59
60    pub fn decode_tng_thing(input: &[u8]) -> IResult<&[u8], TngThing, Error> {
61        let (input, new_thing) = ScriptField::decode_field_named("NewThing")(input)?;
62        let (input, (fields, _end)) = many_till(ScriptField::decode_field, ScriptField::decode_field_named("EndThing"))(input)?;
63
64        Ok(
65            (
66                input,
67                TngThing {
68                    new_thing: new_thing,
69                    fields: fields
70                }
71            )
72        )
73    }
74}