rust/hedgewars-server/src/protocol/parser.rs
changeset 14478 98ef2913ec73
parent 14436 06672690d71b
child 14796 09d46ab83361
equal deleted inserted replaced
14477:a077aac9df01 14478:98ef2913ec73
     4  * indicated by a double newline - `\n\n`.
     4  * indicated by a double newline - `\n\n`.
     5  *
     5  *
     6  * For example, a nullary command like PING will be actually sent as `PING\n\n`.
     6  * For example, a nullary command like PING will be actually sent as `PING\n\n`.
     7  * A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`.
     7  * A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`.
     8  */
     8  */
     9 
       
    10 use nom::*;
     9 use nom::*;
    11 
    10 
    12 use std::{
    11 use super::messages::{HWProtocolMessage, HWProtocolMessage::*};
    13     str, str::FromStr,
    12 use crate::server::coretypes::{GameCfg, HedgehogInfo, TeamInfo, VoteType, MAX_HEDGEHOGS_PER_TEAM};
    14     ops::Range
    13 use std::{ops::Range, str, str::FromStr};
    15 };
       
    16 use super::{
       
    17     messages::{HWProtocolMessage, HWProtocolMessage::*}
       
    18 };
       
    19 #[cfg(test)]
    14 #[cfg(test)]
    20 use {
    15 use {
    21     super::test::gen_proto_msg,
    16     super::test::gen_proto_msg,
    22     proptest::{proptest, proptest_helper}
    17     proptest::{proptest, proptest_helper},
    23 };
       
    24 use crate::server::coretypes::{
       
    25     HedgehogInfo, TeamInfo, GameCfg, VoteType, MAX_HEDGEHOGS_PER_TEAM
       
    26 };
    18 };
    27 
    19 
    28 named!(end_of_message, tag!("\n\n"));
    20 named!(end_of_message, tag!("\n\n"));
    29 named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
    21 named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
    30 named!(  a_line<&[u8], String>, map!(str_line, String::from));
    22 named!(  a_line<&[u8], String>, map!(str_line, String::from));
   255     }
   247     }
   256 }
   248 }
   257 
   249 
   258 #[test]
   250 #[test]
   259 fn parse_test() {
   251 fn parse_test() {
   260     assert_eq!(message(b"PING\n\n"),          Ok((&b""[..], Ping)));
   252     assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
   261     assert_eq!(message(b"START_GAME\n\n"),    Ok((&b""[..], StartGame)));
   253     assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
   262     assert_eq!(message(b"NICK\nit's me\n\n"), Ok((&b""[..], Nick("it's me".to_string()))));
   254     assert_eq!(
   263     assert_eq!(message(b"PROTO\n51\n\n"),     Ok((&b""[..], Proto(51))));
   255         message(b"NICK\nit's me\n\n"),
   264     assert_eq!(message(b"QUIT\nbye-bye\n\n"), Ok((&b""[..], Quit(Some("bye-bye".to_string())))));
   256         Ok((&b""[..], Nick("it's me".to_string())))
   265     assert_eq!(message(b"QUIT\n\n"),          Ok((&b""[..], Quit(None))));
   257     );
   266     assert_eq!(message(b"CMD\nwatch demo\n\n"), Ok((&b""[..], Watch("demo".to_string()))));
   258     assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
   267     assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77))));
   259     assert_eq!(
   268 
   260         message(b"QUIT\nbye-bye\n\n"),
   269     assert_eq!(message(b"CMD\nPART\n\n"),      Ok((&b""[..], Part(None))));
   261         Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
   270     assert_eq!(message(b"CMD\nPART _msg_\n\n"), Ok((&b""[..], Part(Some("_msg_".to_string())))));
   262     );
       
   263     assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
       
   264     assert_eq!(
       
   265         message(b"CMD\nwatch demo\n\n"),
       
   266         Ok((&b""[..], Watch("demo".to_string())))
       
   267     );
       
   268     assert_eq!(
       
   269         message(b"BAN\nme\nbad\n77\n\n"),
       
   270         Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
       
   271     );
       
   272 
       
   273     assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
       
   274     assert_eq!(
       
   275         message(b"CMD\nPART _msg_\n\n"),
       
   276         Ok((&b""[..], Part(Some("_msg_".to_string()))))
       
   277     );
   271 
   278 
   272     assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
   279     assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
   273     assert_eq!(
   280     assert_eq!(
   274         message(b"CMD\nRND A B\n\n"),
   281         message(b"CMD\nRND A B\n\n"),
   275         Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
   282         Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
   276     );
   283     );
   277 
   284 
   278     assert_eq!(extract_messages(b"QUIT\n1\n2\n\n"),    Ok((&b""[..], vec![Malformed])));
   285     assert_eq!(
   279 
   286         extract_messages(b"QUIT\n1\n2\n\n"),
   280     assert_eq!(extract_messages(b"PING\n\nPING\n\nP"), Ok((&b"P"[..], vec![Ping, Ping])));
   287         Ok((&b""[..], vec![Malformed]))
   281     assert_eq!(extract_messages(b"SING\n\nPING\n\n"),  Ok((&b""[..],  vec![Malformed, Ping])));
   288     );
   282     assert_eq!(extract_messages(b"\n\n\n\nPING\n\n"),  Ok((&b""[..],  vec![Empty, Empty, Ping])));
   289 
   283     assert_eq!(extract_messages(b"\n\n\nPING\n\n"),    Ok((&b""[..],  vec![Empty, Empty, Ping])));
   290     assert_eq!(
       
   291         extract_messages(b"PING\n\nPING\n\nP"),
       
   292         Ok((&b"P"[..], vec![Ping, Ping]))
       
   293     );
       
   294     assert_eq!(
       
   295         extract_messages(b"SING\n\nPING\n\n"),
       
   296         Ok((&b""[..], vec![Malformed, Ping]))
       
   297     );
       
   298     assert_eq!(
       
   299         extract_messages(b"\n\n\n\nPING\n\n"),
       
   300         Ok((&b""[..], vec![Empty, Empty, Ping]))
       
   301     );
       
   302     assert_eq!(
       
   303         extract_messages(b"\n\n\nPING\n\n"),
       
   304         Ok((&b""[..], vec![Empty, Empty, Ping]))
       
   305     );
   284 }
   306 }