gameServer2/src/protocol/parser.rs
changeset 12134 109e6765b1fc
parent 12133 81df2e1f9ae9
child 12135 23ee939ba66a
equal deleted inserted replaced
12133:81df2e1f9ae9 12134:109e6765b1fc
     5 use super::messages::HWProtocolMessage;
     5 use super::messages::HWProtocolMessage;
     6 use super::messages::HWProtocolMessage::*;
     6 use super::messages::HWProtocolMessage::*;
     7 
     7 
     8 named!(end_of_message, tag!("\n\n"));
     8 named!(end_of_message, tag!("\n\n"));
     9 named!(a_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8));
     9 named!(a_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8));
    10 //fn number_line<T>(input: &[u8]) -> IResult<&[u8], T> {
    10 named!(opt_param<&[u8], Option<&str> >, opt!(flat_map!(preceded!(eol, a_line), non_empty)));
    11 //}
    11 
    12 named!(basic_message<&[u8], HWProtocolMessage>, alt!(
    12 named!(basic_message<&[u8], HWProtocolMessage>, alt!(
    13     do_parse!(tag!("PING") >> (Ping))
    13     do_parse!(tag!("PING") >> (Ping))
    14     | do_parse!(tag!("PONG") >> (Pong))
    14     | do_parse!(tag!("PONG") >> (Pong))
    15     | do_parse!(tag!("LIST") >> (List))
    15     | do_parse!(tag!("LIST") >> (List))
    16     | do_parse!(tag!("BANLIST") >> (BanList))
    16     | do_parse!(tag!("BANLIST") >> (BanList))
    24 ));
    24 ));
    25 
    25 
    26 named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
    26 named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
    27     do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n)))
    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)))
    28     | do_parse!(tag!("PROTO") >> eol >> d: map_res!(a_line, FromStr::from_str) >> (Proto(d)))
       
    29     | do_parse!(tag!("QUIT") >> msg: opt_param >> (Quit(msg)))
    29 ));
    30 ));
    30 
    31 
    31 named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
    32 named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
    32     basic_message
    33     basic_message
    33     | one_param_message
    34     | one_param_message
    34 ), end_of_message));
    35 ), end_of_message));
    35 
    36 
    36 named!(messages<&[u8], Vec<HWProtocolMessage> >, many0!(message));
       
    37 
    37 
    38 #[test]
    38 #[test]
    39 fn parse_test() {
    39 fn parse_test() {
    40     assert_eq!(message(b"PING\n\n"), IResult::Done(&b""[..], Ping));
    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));
    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")));
    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)));
    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)));
    44 }
    46 }