|
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)); |
|
10 //fn number_line<T>(input: &[u8]) -> IResult<&[u8], T> { |
|
11 //} |
|
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))) |
|
29 )); |
|
30 |
|
31 named!(message<&[u8],HWProtocolMessage>, terminated!(alt!( |
|
32 basic_message |
|
33 | one_param_message |
|
34 ), end_of_message)); |
|
35 |
|
36 named!(messages<&[u8], Vec<HWProtocolMessage> >, many0!(message)); |
|
37 |
|
38 #[test] |
|
39 fn parse_test() { |
|
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)); |
|
42 assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me"))); |
|
43 assert_eq!(message(b"PROTO\n51\n\n"), IResult::Done(&b""[..], Proto(51))); |
|
44 } |