14232
|
1 |
use nom::{*, Err::Error};
|
14230
|
2 |
use std::str;
|
|
3 |
|
|
4 |
use super::messages::{*, EngineMessage::*, UnsyncedEngineMessage::*, SyncedEngineMessage::*, ConfigEngineMessage::*, KeystrokeAction::*};
|
|
5 |
|
14231
|
6 |
|
14232
|
7 |
macro_rules! eof_slice (
|
|
8 |
($i:expr,) => (
|
|
9 |
{
|
|
10 |
if ($i).input_len() == 0 {
|
|
11 |
Ok(($i, $i))
|
|
12 |
} else {
|
|
13 |
Err(Error(error_position!($i, ErrorKind::Eof::<u32>)))
|
|
14 |
}
|
|
15 |
}
|
|
16 |
);
|
|
17 |
);
|
14230
|
18 |
|
|
19 |
named!(unrecognized_message<&[u8], EngineMessage>,
|
|
20 |
do_parse!(rest >> (Unknown))
|
|
21 |
);
|
|
22 |
|
|
23 |
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
|
|
24 |
|
14231
|
25 |
named!(length_without_timestamp<&[u8], usize>,
|
|
26 |
map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } )
|
|
27 |
);
|
|
28 |
|
14230
|
29 |
named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
|
14231
|
30 |
do_parse!(tag!("L") >> (Left(Press)))
|
14232
|
31 |
| do_parse!(tag!("l") >> ( Left(Release) ))
|
|
32 |
| do_parse!(tag!("R") >> ( Right(Press) ))
|
|
33 |
| do_parse!(tag!("r") >> ( Right(Release) ))
|
|
34 |
| do_parse!(tag!("U") >> ( Up(Press) ))
|
|
35 |
| do_parse!(tag!("u") >> ( Up(Release) ))
|
|
36 |
| do_parse!(tag!("D") >> ( Down(Press) ))
|
|
37 |
| do_parse!(tag!("d") >> ( Down(Release) ))
|
|
38 |
| do_parse!(tag!("Z") >> ( Precise(Press) ))
|
|
39 |
| do_parse!(tag!("z") >> ( Precise(Release) ))
|
|
40 |
| do_parse!(tag!("A") >> ( Attack(Press) ))
|
|
41 |
| do_parse!(tag!("a") >> ( Attack(Release) ))
|
|
42 |
| do_parse!(tag!("N") >> ( NextTurn ))
|
|
43 |
| do_parse!(tag!("j") >> ( LongJump ))
|
|
44 |
| do_parse!(tag!("J") >> ( HighJump ))
|
|
45 |
| do_parse!(tag!("S") >> ( Switch ))
|
|
46 |
| do_parse!(tag!(",") >> ( Skip ))
|
|
47 |
| do_parse!(tag!("1") >> ( Timer(1) ))
|
|
48 |
| do_parse!(tag!("2") >> ( Timer(2) ))
|
|
49 |
| do_parse!(tag!("3") >> ( Timer(3) ))
|
|
50 |
| do_parse!(tag!("4") >> ( Timer(4) ))
|
|
51 |
| do_parse!(tag!("5") >> ( Timer(5) ))
|
|
52 |
| do_parse!(tag!("p") >> x: be_i24 >> y: be_i24 >> ( Put(x, y) ))
|
|
53 |
| do_parse!(tag!("P") >> x: be_i24 >> y: be_i24 >> ( CursorMove(x, y) ))
|
|
54 |
| do_parse!(tag!("f") >> s: string_tail >> ( SyncedEngineMessage::TeamControlLost(s) ))
|
|
55 |
| do_parse!(tag!("g") >> s: string_tail >> ( SyncedEngineMessage::TeamControlGained(s) ))
|
|
56 |
/*
|
|
57 |
Slot(u8),
|
|
58 |
SetWeapon(u8),
|
|
59 |
*/
|
14230
|
60 |
));
|
|
61 |
|
|
62 |
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
|
|
63 |
do_parse!(tag!("?") >> (Ping))
|
|
64 |
| do_parse!(tag!("!") >> (Ping))
|
14232
|
65 |
| do_parse!(tag!("esay ") >> s: string_tail >> (Say(s)))
|
14230
|
66 |
));
|
|
67 |
|
|
68 |
named!(config_message<&[u8], ConfigEngineMessage>, alt!(
|
|
69 |
do_parse!(tag!("C") >> (ConfigRequest))
|
|
70 |
));
|
|
71 |
|
14232
|
72 |
named!(timestamped_message<&[u8], (SyncedEngineMessage, u16)>,
|
|
73 |
do_parse!(msg: length_value!(length_without_timestamp, terminated!(synced_message, eof_slice!()))
|
|
74 |
>> timestamp: be_u16
|
|
75 |
>> ((msg, timestamp))
|
|
76 |
)
|
|
77 |
);
|
|
78 |
|
14231
|
79 |
named!(unwrapped_message<&[u8], EngineMessage>,
|
|
80 |
alt!(
|
|
81 |
map!(timestamped_message, |(m, t)| Synced(m, t as u32))
|
|
82 |
| do_parse!(tag!("#") >> (Synced(TimeWrap, 65535)))
|
|
83 |
| map!(unsynced_message, |m| Unsynced(m))
|
|
84 |
| map!(config_message, |m| Config(m))
|
|
85 |
| unrecognized_message
|
|
86 |
));
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
named!(length_specifier<&[u8], u16>, alt!(
|
|
91 |
verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
|
|
92 |
| map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
|
|
93 |
)
|
|
94 |
);
|
|
95 |
|
14230
|
96 |
named!(empty_message<&[u8], EngineMessage>,
|
|
97 |
do_parse!(tag!("\0") >> (Empty))
|
|
98 |
);
|
|
99 |
|
14231
|
100 |
named!(non_empty_message<&[u8], EngineMessage>,
|
14232
|
101 |
length_value!(length_specifier, terminated!(unwrapped_message, eof_slice!())));
|
14230
|
102 |
|
|
103 |
named!(message<&[u8], EngineMessage>, alt!(
|
|
104 |
empty_message
|
|
105 |
| non_empty_message
|
|
106 |
)
|
|
107 |
);
|
|
108 |
|
|
109 |
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message)));
|
|
110 |
|
|
111 |
#[test]
|
|
112 |
fn parse_length() {
|
|
113 |
assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
|
|
114 |
assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
|
|
115 |
assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
|
|
116 |
assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
|
|
117 |
assert_eq!(length_specifier(b"\xff\xff"), Ok((&b""[..], 49215)));
|
|
118 |
}
|
|
119 |
|
|
120 |
#[test]
|
|
121 |
fn parse_synced_messages() {
|
14231
|
122 |
assert_eq!(message(b"\x03L\x01\x02"), Ok((&b""[..], Synced(Left(Press), 258))));
|
14232
|
123 |
assert_eq!(message(b"\x01#"), Ok((&b""[..], Synced(TimeWrap, 65535))));
|
14230
|
124 |
}
|
|
125 |
|
|
126 |
#[test]
|
|
127 |
fn parse_unsynced_messages() {
|
|
128 |
assert_eq!(message(b"\x0aesay hello"), Ok((&b""[..], Unsynced(Say(String::from("hello"))))));
|
|
129 |
}
|
|
130 |
|
|
131 |
#[test]
|
|
132 |
fn parse_incorrect_messages() {
|
|
133 |
assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
|
|
134 |
assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
|
14232
|
135 |
|
|
136 |
// garbage after correct message
|
|
137 |
assert_eq!(message(b"\x04La\x01\x02"), Ok((&b""[..], Unknown)));
|
14230
|
138 |
}
|
|
139 |
|
|
140 |
#[test]
|
|
141 |
fn parse_config_messages() {
|
|
142 |
assert_eq!(
|
|
143 |
message(b"\x01C"),
|
|
144 |
Ok((
|
|
145 |
&b""[..],
|
|
146 |
Config(ConfigRequest)
|
|
147 |
))
|
|
148 |
);
|
|
149 |
}
|
|
150 |
#[test]
|
|
151 |
fn parse_test_general() {
|
|
152 |
assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
|
|
153 |
}
|