12138
|
1 |
use nom::*;
|
|
2 |
|
|
3 |
use std::str;
|
|
4 |
use std::str::FromStr;
|
|
5 |
use super::messages::HWProtocolMessage;
|
|
6 |
use super::messages::HWProtocolMessage::*;
|
|
7 |
|
|
8 |
named!(end_of_message, tag!("\n\n"));
|
|
9 |
named!(a_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8));
|
12139
|
10 |
named!(opt_param<&[u8], Option<&str> >, opt!(flat_map!(preceded!(eol, a_line), non_empty)));
|
|
11 |
|
12138
|
12 |
named!(basic_message<&[u8], HWProtocolMessage>, alt!(
|
|
13 |
do_parse!(tag!("PING") >> (Ping))
|
|
14 |
| do_parse!(tag!("PONG") >> (Pong))
|
|
15 |
| do_parse!(tag!("LIST") >> (List))
|
|
16 |
| do_parse!(tag!("BANLIST") >> (BanList))
|
|
17 |
| do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar))
|
|
18 |
| do_parse!(tag!("TOGGLE_READY") >> (ToggleReady))
|
|
19 |
| do_parse!(tag!("START_GAME") >> (StartGame))
|
|
20 |
| do_parse!(tag!("ROUNDFINISHED") >> (RoundFinished))
|
|
21 |
| do_parse!(tag!("TOGGLE_RESTRICT_JOINS") >> (ToggleRestrictJoin))
|
|
22 |
| do_parse!(tag!("TOGGLE_RESTRICT_TEAMS") >> (ToggleRestrictTeams))
|
|
23 |
| do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly))
|
|
24 |
));
|
|
25 |
|
|
26 |
named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
|
|
27 |
do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n)))
|
|
28 |
| do_parse!(tag!("PROTO") >> eol >> d: map_res!(a_line, FromStr::from_str) >> (Proto(d)))
|
12139
|
29 |
| do_parse!(tag!("QUIT") >> msg: opt_param >> (Quit(msg)))
|
12138
|
30 |
));
|
|
31 |
|
|
32 |
named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
|
|
33 |
basic_message
|
|
34 |
| one_param_message
|
|
35 |
), end_of_message));
|
|
36 |
|
|
37 |
|
|
38 |
#[test]
|
|
39 |
fn parse_test() {
|
12139
|
40 |
assert_eq!(message(b"PING\n\n"), IResult::Done(&b""[..], Ping));
|
|
41 |
assert_eq!(message(b"START_GAME\n\n"), IResult::Done(&b""[..], StartGame));
|
12138
|
42 |
assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me")));
|
12139
|
43 |
assert_eq!(message(b"PROTO\n51\n\n"), IResult::Done(&b""[..], Proto(51)));
|
|
44 |
assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye"))));
|
|
45 |
assert_eq!(message(b"QUIT\n\n"), IResult::Done(&b""[..], Quit(None)));
|
12138
|
46 |
}
|