gameServer2/src/protocol/parser.rs
changeset 13432 ee3fa3b8809d
parent 13431 6a818f9192f4
child 13438 da71e0d88a1c
equal deleted inserted replaced
13431:6a818f9192f4 13432:ee3fa3b8809d
    25 named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
    25 named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
    26 named!(  a_line<&[u8], String>, map!(str_line, String::from));
    26 named!(  a_line<&[u8], String>, map!(str_line, String::from));
    27 named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
    27 named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
    28 named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
    28 named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
    29 named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from)));
    29 named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from)));
       
    30 named!(spaces<&[u8], &[u8]>, preceded!(tag!(" "), eat_separator!(" ")));
       
    31 named!(opt_space_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(spaces, str_line), non_empty), String::from)));
    30 named!(hog_line<&[u8], HedgehogInfo>,
    32 named!(hog_line<&[u8], HedgehogInfo>,
    31     do_parse!(name: str_line >> eol >> hat: str_line >>
    33     do_parse!(name: str_line >> eol >> hat: str_line >>
    32         (HedgehogInfo{name: name.to_string(), hat: hat.to_string()})));
    34         (HedgehogInfo{name: name.to_string(), hat: hat.to_string()})));
    33 named!(_8_hogs<&[u8], [HedgehogInfo; 8]>,
    35 named!(_8_hogs<&[u8], [HedgehogInfo; 8]>,
    34     do_parse!(h1: hog_line >> eol >> h2: hog_line >> eol >>
    36     do_parse!(h1: hog_line >> eol >> h2: hog_line >> eol >>
    74 /** Recognizes messages preceded with CMD */
    76 /** Recognizes messages preceded with CMD */
    75 named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!(
    77 named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!(
    76       do_parse!(tag_no_case!("STATS") >> (Stats))
    78       do_parse!(tag_no_case!("STATS") >> (Stats))
    77     | do_parse!(tag_no_case!("FIX")   >> (Fix))
    79     | do_parse!(tag_no_case!("FIX")   >> (Fix))
    78     | do_parse!(tag_no_case!("UNFIX") >> (Unfix))
    80     | do_parse!(tag_no_case!("UNFIX") >> (Unfix))
    79     | do_parse!(tag_no_case!("RESTART_SERVER") >> eol >> tag!("YES") >> (RestartServer))
    81     | do_parse!(tag_no_case!("RESTART_SERVER") >> spaces >> tag!("YES") >> (RestartServer))
    80     | do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly))
    82     | do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly))
    81     | do_parse!(tag_no_case!("SUPER_POWER")     >> (SuperPower))
    83     | do_parse!(tag_no_case!("SUPER_POWER")     >> (SuperPower))
    82     | do_parse!(tag_no_case!("PART")     >> m: opt_param >> (Part(m)))
    84     | do_parse!(tag_no_case!("PART")     >> m: opt_space_param >> (Part(m)))
    83     | do_parse!(tag_no_case!("QUIT")     >> m: opt_param >> (Quit(m)))
    85     | do_parse!(tag_no_case!("QUIT")     >> m: opt_space_param >> (Quit(m)))
    84     | do_parse!(tag_no_case!("DELEGATE") >> eol >> n: a_line  >> (Delegate(n)))
    86     | do_parse!(tag_no_case!("DELEGATE") >> spaces >> n: a_line  >> (Delegate(n)))
    85     | do_parse!(tag_no_case!("SAVEROOM") >> eol >> r: a_line  >> (SaveRoom(r)))
    87     | do_parse!(tag_no_case!("SAVEROOM") >> spaces >> r: a_line  >> (SaveRoom(r)))
    86     | do_parse!(tag_no_case!("LOADROOM") >> eol >> r: a_line  >> (LoadRoom(r)))
    88     | do_parse!(tag_no_case!("LOADROOM") >> spaces >> r: a_line  >> (LoadRoom(r)))
    87     | do_parse!(tag_no_case!("DELETE")   >> eol >> r: a_line  >> (Delete(r)))
    89     | do_parse!(tag_no_case!("DELETE")   >> spaces >> r: a_line  >> (Delete(r)))
    88     | do_parse!(tag_no_case!("GLOBAL")   >> eol >> m: a_line  >> (Global(m)))
    90     | do_parse!(tag_no_case!("GLOBAL")   >> spaces >> m: a_line  >> (Global(m)))
    89     | do_parse!(tag_no_case!("WATCH")    >> eol >> i: a_line  >> (Watch(i)))
    91     | do_parse!(tag_no_case!("WATCH")    >> spaces >> i: a_line  >> (Watch(i)))
    90     | do_parse!(tag_no_case!("GREETING") >> eol >> m: a_line  >> (Greeting(m)))
    92     | do_parse!(tag_no_case!("GREETING") >> spaces >> m: a_line  >> (Greeting(m)))
    91     | do_parse!(tag_no_case!("VOTE")     >> eol >> m: a_line  >> (Vote(m)))
    93     | do_parse!(tag_no_case!("VOTE")     >> spaces >> m: a_line  >> (Vote(m)))
    92     | do_parse!(tag_no_case!("FORCE")    >> eol >> m: a_line  >> (ForceVote(m)))
    94     | do_parse!(tag_no_case!("FORCE")    >> spaces >> m: a_line  >> (ForceVote(m)))
    93     | do_parse!(tag_no_case!("INFO")     >> eol >> n: a_line  >> (Info(n)))
    95     | do_parse!(tag_no_case!("INFO")     >> spaces >> n: a_line  >> (Info(n)))
    94     | do_parse!(tag_no_case!("MAXTEAMS") >> eol >> n: u8_line >> (MaxTeams(n)))
    96     | do_parse!(tag_no_case!("MAXTEAMS") >> spaces >> n: u8_line >> (MaxTeams(n)))
    95     | do_parse!(
    97     | do_parse!(
    96         tag_no_case!("RND") >>
    98         tag_no_case!("RND") >> alt!(spaces | peek!(end_of_message)) >>
    97         v: str_line >>
    99         v: str_line >>
    98         (Rnd(v.split_whitespace().map(String::from).collect())))
   100         (Rnd(v.split_whitespace().map(String::from).collect())))
    99 )));
   101 )));
   100 
   102 
   101 named!(complex_message<&[u8], HWProtocolMessage>, alt!(
   103 named!(complex_message<&[u8], HWProtocolMessage>, alt!(
   230     assert_eq!(message(b"START_GAME\n\n"),    IResult::Done(&b""[..], StartGame));
   232     assert_eq!(message(b"START_GAME\n\n"),    IResult::Done(&b""[..], StartGame));
   231     assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me".to_string())));
   233     assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me".to_string())));
   232     assert_eq!(message(b"PROTO\n51\n\n"),     IResult::Done(&b""[..], Proto(51)));
   234     assert_eq!(message(b"PROTO\n51\n\n"),     IResult::Done(&b""[..], Proto(51)));
   233     assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye".to_string()))));
   235     assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye".to_string()))));
   234     assert_eq!(message(b"QUIT\n\n"),          IResult::Done(&b""[..], Quit(None)));
   236     assert_eq!(message(b"QUIT\n\n"),          IResult::Done(&b""[..], Quit(None)));
   235     assert_eq!(message(b"CMD\nwatch\ndemo\n\n"), IResult::Done(&b""[..], Watch("demo".to_string())));
   237     assert_eq!(message(b"CMD\nwatch demo\n\n"), IResult::Done(&b""[..], Watch("demo".to_string())));
   236     assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me".to_string(), "bad".to_string(), 77)));
   238     assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me".to_string(), "bad".to_string(), 77)));
   237 
   239 
   238     assert_eq!(message(b"CMD\nPART\n\n"),      IResult::Done(&b""[..], Part(None)));
   240     assert_eq!(message(b"CMD\nPART\n\n"),      IResult::Done(&b""[..], Part(None)));
   239     assert_eq!(message(b"CMD\nPART\n_msg_\n\n"), IResult::Done(&b""[..], Part(Some("_msg_".to_string()))));
   241     assert_eq!(message(b"CMD\nPART _msg_\n\n"), IResult::Done(&b""[..], Part(Some("_msg_".to_string()))));
   240 
   242 
   241     assert_eq!(message(b"CMD\nRND\n\n"), IResult::Done(&b""[..], Rnd(vec![])));
   243     assert_eq!(message(b"CMD\nRND\n\n"), IResult::Done(&b""[..], Rnd(vec![])));
   242     assert_eq!(
   244     assert_eq!(
   243         message(b"CMD\nRND A B\n\n"),
   245         message(b"CMD\nRND A B\n\n"),
   244         IResult::Done(&b""[..], Rnd(vec![String::from("A"), String::from("B")]))
   246         IResult::Done(&b""[..], Rnd(vec![String::from("A"), String::from("B")]))