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
158
159
160
161
162
163
164
165
166
167
168
//! # Parser methods for the general types
use super::types::{ObjectID, ObjectTemplate, Quaternion, Vector3f, WorldID};
//use encoding::{all::UTF_16LE, DecoderTrap, Encoding};
use nom::{
    bytes::complete::take,
    combinator::{map, map_res},
    error::FromExternalError,
    multi::length_data,
    sequence::tuple,
};
use nom::{
    error::ParseError,
    number::complete::{le_f32, le_u32, le_u8},
    IResult,
};
use std::{char::decode_utf16, string::FromUtf8Error};

/// Helper method to dump some values
#[allow(dead_code)]
pub fn dump<T>(val: T) -> T
where
    T: std::fmt::Debug,
{
    println!("{:?}", val);
    val
}

type Res<'a, T, E> = IResult<&'a [u8], T, E>;

/// Parse a Vector3f
pub fn parse_vec3f<'a, E>(input: &'a [u8]) -> Res<'a, Vector3f, E>
where
    E: ParseError<&'a [u8]>,
{
    map(tuple((le_f32, le_f32, le_f32)), |(x, y, z)| {
        Vector3f::new(x, y, z)
    })(input)
}

/// Parse a Quaternion
pub fn parse_quat<'a, E>(input: &'a [u8]) -> Res<'a, Quaternion, E>
where
    E: ParseError<&'a [u8]> + 'a,
{
    map(tuple((le_f32, le_f32, le_f32, le_f32)), |(x, y, z, w)| {
        Quaternion::new(x, y, z, w)
    })(input)
}

/// Parse a Quaternion
pub fn parse_quat_wxyz<'a, E>(input: &'a [u8]) -> Res<'a, Quaternion, E>
where
    E: ParseError<&'a [u8]>,
{
    map(tuple((le_f32, le_f32, le_f32, le_f32)), |(w, x, y, z)| {
        Quaternion::new(x, y, z, w)
    })(input)
}

/// Parse a WorldID
pub fn parse_world_id<'a, E>(input: &'a [u8]) -> Res<'a, WorldID, E>
where
    E: ParseError<&'a [u8]>,
{
    map(le_u32, WorldID::new)(input)
}

/// Parse an ObjectTemplate
pub fn parse_object_template<'a, E>(input: &'a [u8]) -> Res<'a, ObjectTemplate, E>
where
    E: ParseError<&'a [u8]>,
{
    map(le_u32, ObjectTemplate::new)(input)
}

/// Parse an ObjectID
pub fn parse_object_id<'a, E>(input: &'a [u8]) -> Res<'a, ObjectID, E>
where
    E: ParseError<&'a [u8]>,
{
    map(tuple((le_u32, le_u32)), |(a, b)| ObjectID::new(b, a))(input)
}

fn map_wstring(val: &[u8]) -> Result<String, ()> {
    let iter = val.chunks_exact(2);
    if iter.remainder().is_empty() {
        let iter = iter.map(|s| (s[0] as u16) + ((s[1] as u16) << 8));
        decode_utf16(iter).map(|r| r.or(Err(()))).collect()
    } else {
        Err(())
    }
}

/// Parse a u8 wstring
pub fn parse_u8_wstring<'a, E>(input: &'a [u8]) -> Res<'a, String, E>
where
    E: ParseError<&'a [u8]> + FromExternalError<&'a [u8], ()>,
{
    le_u8(input).and_then(|(input, count)| {
        let len = usize::from(count) * 2;
        map_res(take(len), map_wstring)(input)
    })
}

/// Parse a u32 wstring
pub fn parse_u32_wstring<'a, E>(input: &'a [u8]) -> Res<'a, String, E>
where
    E: ParseError<&'a [u8]> + FromExternalError<&'a [u8], ()>,
{
    le_u32(input).and_then(|(input, count)| {
        let len = count * 2;
        map_res(take(len), map_wstring)(input)
    })
}

/// Parse a string with u16 length specifier
pub fn parse_string_u16<'a, E>(input: &'a [u8], i: u16) -> Res<'a, String, E>
where
    E: ParseError<&'a [u8]> + FromExternalError<&'a [u8], FromUtf8Error>,
{
    map_res(map(take(i), Vec::from), String::from_utf8)(input)
}

/// Parse a string after an u8 length specifier
pub fn parse_u8_string<'a, E>(input: &'a [u8]) -> Res<'a, String, E>
where
    E: ParseError<&'a [u8]> + FromExternalError<&'a [u8], FromUtf8Error>,
{
    map_res(map(length_data(le_u8), Vec::from), String::from_utf8)(input)
}

/// Parse a string after an u32 length specifier
pub fn parse_u32_string<'a, E>(input: &'a [u8]) -> Res<'a, String, E>
where
    E: ParseError<&'a [u8]> + FromExternalError<&'a [u8], FromUtf8Error>,
{
    map_res(map(length_data(le_u32), Vec::from), String::from_utf8)(input)
}

/// Parse an u32 boolean
pub fn parse_u32_bool<'a, E>(input: &'a [u8]) -> Res<'a, bool, E>
where
    E: ParseError<&'a [u8]>,
{
    map(le_u32, |v| v > 0)(input)
}

/// Parse an u8 boolean
pub fn parse_u8_bool<'a, E>(input: &'a [u8]) -> Res<'a, bool, E>
where
    E: ParseError<&'a [u8]>,
{
    map(le_u8, |v| v > 0)(input)
}

#[cfg(test)]
mod test {
    use super::parse_u8_wstring;
    use nom::error::ErrorKind;

    #[test]
    fn test_wstring() {
        assert_eq!(
            parse_u8_wstring::<'_, (&[u8], ErrorKind)>(&[2, 65, 0, 66, 0]),
            Ok((&[][..], String::from("AB")))
        );
    }
}