14231
|
1 |
use nom::{*};
|
14230
|
2 |
use std::str;
|
|
3 |
|
|
4 |
use super::messages::{*, EngineMessage::*, UnsyncedEngineMessage::*, SyncedEngineMessage::*, ConfigEngineMessage::*, KeystrokeAction::*};
|
|
5 |
|
14231
|
6 |
|
14230
|
7 |
|
|
8 |
named!(unrecognized_message<&[u8], EngineMessage>,
|
|
9 |
do_parse!(rest >> (Unknown))
|
|
10 |
);
|
|
11 |
|
|
12 |
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
|
|
13 |
|
14231
|
14 |
named!(length_without_timestamp<&[u8], usize>,
|
|
15 |
map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } )
|
|
16 |
);
|
|
17 |
|
14230
|
18 |
named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
|
14231
|
19 |
do_parse!(tag!("L") >> (Left(Press)))
|
14230
|
20 |
));
|
|
21 |
|
14231
|
22 |
named!(timestamped_message<&[u8], (SyncedEngineMessage, u16)>,
|
|
23 |
do_parse!(msg: length_value!(length_without_timestamp, synced_message)
|
|
24 |
>> timestamp: be_u16
|
|
25 |
>> ((msg, timestamp))
|
|
26 |
)
|
|
27 |
);
|
|
28 |
|
14230
|
29 |
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
|
|
30 |
do_parse!(tag!("?") >> (Ping))
|
|
31 |
| do_parse!(tag!("!") >> (Ping))
|
|
32 |
| do_parse!(tag!("esay ") >> s: string_tail >> (Say(s)))
|
|
33 |
));
|
|
34 |
|
|
35 |
named!(config_message<&[u8], ConfigEngineMessage>, alt!(
|
|
36 |
do_parse!(tag!("C") >> (ConfigRequest))
|
|
37 |
));
|
|
38 |
|
14231
|
39 |
named!(unwrapped_message<&[u8], EngineMessage>,
|
|
40 |
alt!(
|
|
41 |
map!(timestamped_message, |(m, t)| Synced(m, t as u32))
|
|
42 |
| do_parse!(tag!("#") >> (Synced(TimeWrap, 65535)))
|
|
43 |
| map!(unsynced_message, |m| Unsynced(m))
|
|
44 |
| map!(config_message, |m| Config(m))
|
|
45 |
| unrecognized_message
|
|
46 |
));
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
named!(length_specifier<&[u8], u16>, alt!(
|
|
51 |
verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
|
|
52 |
| map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
|
|
53 |
)
|
|
54 |
);
|
|
55 |
|
14230
|
56 |
named!(empty_message<&[u8], EngineMessage>,
|
|
57 |
do_parse!(tag!("\0") >> (Empty))
|
|
58 |
);
|
|
59 |
|
14231
|
60 |
named!(non_empty_message<&[u8], EngineMessage>,
|
|
61 |
length_value!(length_specifier, unwrapped_message));
|
14230
|
62 |
|
|
63 |
named!(message<&[u8], EngineMessage>, alt!(
|
|
64 |
empty_message
|
|
65 |
| non_empty_message
|
|
66 |
)
|
|
67 |
);
|
|
68 |
|
|
69 |
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message)));
|
|
70 |
|
|
71 |
#[test]
|
|
72 |
fn parse_length() {
|
|
73 |
assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
|
|
74 |
assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
|
|
75 |
assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
|
|
76 |
assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
|
|
77 |
assert_eq!(length_specifier(b"\xff\xff"), Ok((&b""[..], 49215)));
|
|
78 |
}
|
|
79 |
|
|
80 |
#[test]
|
|
81 |
fn parse_synced_messages() {
|
14231
|
82 |
assert_eq!(message(b"\x03L\x01\x02"), Ok((&b""[..], Synced(Left(Press), 258))));
|
14230
|
83 |
}
|
|
84 |
|
|
85 |
#[test]
|
|
86 |
fn parse_unsynced_messages() {
|
|
87 |
assert_eq!(message(b"\x0aesay hello"), Ok((&b""[..], Unsynced(Say(String::from("hello"))))));
|
|
88 |
}
|
|
89 |
|
|
90 |
#[test]
|
|
91 |
fn parse_incorrect_messages() {
|
|
92 |
assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
|
|
93 |
assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
|
|
94 |
}
|
|
95 |
|
|
96 |
#[test]
|
|
97 |
fn parse_config_messages() {
|
|
98 |
assert_eq!(
|
|
99 |
message(b"\x01C"),
|
|
100 |
Ok((
|
|
101 |
&b""[..],
|
|
102 |
Config(ConfigRequest)
|
|
103 |
))
|
|
104 |
);
|
|
105 |
}
|
|
106 |
#[test]
|
|
107 |
fn parse_test_general() {
|
|
108 |
assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
|
|
109 |
}
|