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
use nom::{le_f32, le_u32, le_u8};
use num_traits::FromPrimitive;
use encoding::{Encoding, DecoderTrap, all::UTF_16LE};
use super::types::{Vector3f, Quaternion, WorldID, ObjectID, ObjectTemplate};
#[allow(dead_code)]
pub fn dump<T>(val: T) -> T
where T: std::fmt::Debug {
println!("{:?}", val);
val
}
named!(pub parse_vec3f<Vector3f>,
do_parse!(
a: le_f32 >>
b: le_f32 >>
c: le_f32 >>
(Vector3f{x: a, y: b, z: c})
)
);
named!(pub parse_quat<Quaternion>,
do_parse!(
a: le_f32 >>
b: le_f32 >>
c: le_f32 >>
d: le_f32 >>
(Quaternion{x: a, y: b, z: c, w: d})
)
);
named!(pub parse_world_id<WorldID>,
map_opt!(le_u32, WorldID::from_u32)
);
named!(pub parse_object_template<ObjectTemplate>,
map_opt!(le_u32, ObjectTemplate::from_u32)
);
named!(pub parse_object_id<ObjectID>,
do_parse!(
id: le_u32 >>
scope: le_u32 >>
(ObjectID{scope, id})
)
);
fn map_wstring(val: &[u8]) -> Result<String,std::borrow::Cow<'static, str>> {
UTF_16LE.decode(val, DecoderTrap::Strict)
}
named!(pub parse_u8_wstring<String>,
do_parse!(
count: le_u8 >>
string: map_res!(take!(usize::from(count) * 2), map_wstring) >>
(string)
)
);
named!(pub parse_u32_wstring<String>,
do_parse!(
count: le_u32 >>
string: map_res!(take!(count * 2), map_wstring) >>
(string)
)
);
named!(pub parse_u8_string<String>,
map_res!(map!(length_bytes!(le_u8), Vec::from), String::from_utf8)
);
named!(pub parse_u32_bool<bool>,
alt!(value!(false, tag!([0; 4])) | value!(true, le_u32))
);
named!(pub parse_u8_bool<bool>,
alt!(value!(false, tag!([0; 1])) | value!(true, le_u8))
);
#[cfg(test)]
mod test {
use super::parse_u8_wstring;
#[test]
fn test_wstring() {
assert_eq!(parse_u8_wstring(&[2,65,0,66,0]), Ok((&[][..], String::from("AB"))));
}
}