gameServer2/src/protocol/parser.rs
author Wuzzy <Wuzzy2@mail.ru>
Sat, 24 Mar 2018 02:24:02 +0100
changeset 13280 7ed4ab32f351
parent 13124 1e39b8749072
child 13421 cdf69667593b
permissions -rw-r--r--
Highlander: Rename “mode=orig” to “multiuse=true” and explain the meaning of this scriptparam in mision panel Don't worry, the gameplay of this script param is NOT changed, only its name and documentation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     1
use nom::*;
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     2
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     3
use std::str;
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     4
use std::str::FromStr;
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     5
use super::messages::HWProtocolMessage;
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     6
use super::messages::HWProtocolMessage::*;
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     7
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
     8
use proptest::test_runner::{TestRunner, Reason};
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
     9
use proptest::arbitrary::{any, any_with, Arbitrary, StrategyFor};
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
    10
use proptest::strategy::{Strategy, BoxedStrategy, Just, Filter, ValueTree};
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
    11
use proptest::string::RegexGeneratorValueTree;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
    12
use std::ops::Range;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
    13
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    14
named!(end_of_message, tag!("\n\n"));
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
    15
named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
    16
named!(  a_line<&[u8], String>, map!(str_line, String::from));
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
    17
named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
    18
named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
    19
named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from)));
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    20
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    21
named!(basic_message<&[u8], HWProtocolMessage>, alt!(
12140
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    22
      do_parse!(tag!("PING") >> (Ping))
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    23
    | do_parse!(tag!("PONG") >> (Pong))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    24
    | do_parse!(tag!("LIST") >> (List))
12140
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    25
    | do_parse!(tag!("BANLIST")        >> (BanList))
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    26
    | do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar))
12140
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    27
    | do_parse!(tag!("TOGGLE_READY")   >> (ToggleReady))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    28
    | do_parse!(tag!("START_GAME")     >> (StartGame))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    29
    | do_parse!(tag!("ROUNDFINISHED")  >> (RoundFinished))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    30
    | do_parse!(tag!("TOGGLE_RESTRICT_JOINS")  >> (ToggleRestrictJoin))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    31
    | do_parse!(tag!("TOGGLE_RESTRICT_TEAMS")  >> (ToggleRestrictTeams))
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    32
    | do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    33
));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    34
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    35
named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
12140
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    36
      do_parse!(tag!("NICK")    >> eol >> n: a_line >> (Nick(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    37
    | do_parse!(tag!("INFO")    >> eol >> n: a_line >> (Info(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    38
    | do_parse!(tag!("CHAT")    >> eol >> m: a_line >> (Chat(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    39
    | do_parse!(tag!("FOLLOW")  >> eol >> n: a_line >> (Follow(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    40
    | do_parse!(tag!("KICK")    >> eol >> n: a_line >> (Kick(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    41
    | do_parse!(tag!("UNBAN")   >> eol >> n: a_line >> (Unban(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    42
    | do_parse!(tag!("EM")      >> eol >> m: a_line >> (EngineMessage(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    43
    | do_parse!(tag!("TEAMCHAT")    >> eol >> m: a_line >> (TeamChat(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    44
    | do_parse!(tag!("ROOM_NAME")   >> eol >> n: a_line >> (RoomName(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    45
    | do_parse!(tag!("REMOVE_TEAM") >> eol >> n: a_line >> (RemoveTeam(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    46
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    47
    | do_parse!(tag!("PROTO")   >> eol >> d: u32_line >> (Proto(d)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    48
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    49
    | do_parse!(tag!("QUIT")   >> msg: opt_param >> (Quit(msg)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    50
));
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    51
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    52
named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!(
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    53
      do_parse!(tag_no_case!("STATS") >> (Stats))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    54
    | do_parse!(tag_no_case!("FIX")   >> (Fix))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    55
    | do_parse!(tag_no_case!("UNFIX") >> (Unfix))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    56
    | do_parse!(tag_no_case!("RESTART_SERVER") >> eol >> tag!("YES") >> (RestartServer))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    57
    | do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    58
    | do_parse!(tag_no_case!("SUPER_POWER")     >> (SuperPower))
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
    59
    | do_parse!(tag_no_case!("PART")     >> m: opt_param >> (Part(m)))
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
    60
    | do_parse!(tag_no_case!("QUIT")     >> m: opt_param >> (Quit(m)))
12140
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    61
    | do_parse!(tag_no_case!("DELEGATE") >> eol >> n: a_line  >> (Delegate(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    62
    | do_parse!(tag_no_case!("SAVEROOM") >> eol >> r: a_line  >> (SaveRoom(r)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    63
    | do_parse!(tag_no_case!("LOADROOM") >> eol >> r: a_line  >> (LoadRoom(r)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    64
    | do_parse!(tag_no_case!("DELETE")   >> eol >> r: a_line  >> (Delete(r)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    65
    | do_parse!(tag_no_case!("GLOBAL")   >> eol >> m: a_line  >> (Global(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    66
    | do_parse!(tag_no_case!("WATCH")    >> eol >> i: a_line  >> (Watch(i)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    67
    | do_parse!(tag_no_case!("GREETING") >> eol >> m: a_line  >> (Greeting(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    68
    | do_parse!(tag_no_case!("VOTE")     >> eol >> m: a_line  >> (Vote(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    69
    | do_parse!(tag_no_case!("FORCE")    >> eol >> m: a_line  >> (ForceVote(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    70
    | do_parse!(tag_no_case!("INFO")     >> eol >> n: a_line  >> (Info(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    71
    | do_parse!(tag_no_case!("MAXTEAMS") >> eol >> n: u8_line >> (MaxTeams(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    72
)));
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    73
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    74
named!(complex_message<&[u8], HWProtocolMessage>, alt!(
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    75
      do_parse!(tag!("PASSWORD")  >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    76
                    p: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    77
                    s: a_line     >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    78
                    (Password(p, s)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    79
    | do_parse!(tag!("CHECKER")   >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    80
                    i: u32_line   >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    81
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    82
                    p: a_line     >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    83
                    (Checker(i, n, p)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    84
    | do_parse!(tag!("CREATE_ROOM") >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    85
                    n: a_line       >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    86
                    p: opt_param    >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    87
                    (CreateRoom(n, p)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    88
    | do_parse!(tag!("JOIN")        >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    89
                    n: a_line       >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    90
                    p: opt_param    >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    91
                    (Join(n, p)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    92
    | do_parse!(tag!("BAN")    >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    93
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    94
                    r: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    95
                    t: u32_line   >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    96
                    (Ban(n, r, t)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    97
    | do_parse!(tag!("BAN_IP")    >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    98
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
    99
                    r: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
   100
                    t: u32_line   >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
   101
                    (BanIP(n, r, t)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
   102
    | do_parse!(tag!("BAN_NICK")    >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
   103
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
   104
                    r: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
   105
                    t: u32_line   >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12139
diff changeset
   106
                    (BanNick(n, r, t)))
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   107
));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   108
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   109
named!(malformed_message<&[u8], HWProtocolMessage>,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   110
    do_parse!(separated_list!(eol, a_line) >> (Malformed)));
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   111
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   112
named!(empty_message<&[u8], HWProtocolMessage>,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   113
    do_parse!(alt!(end_of_message | eol) >> (Empty)));
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   114
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   115
named!(message<&[u8], HWProtocolMessage>, alt!(terminated!(
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   116
    alt!(
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   117
          basic_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   118
        | one_param_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   119
        | cmd_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   120
        | complex_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   121
        ), end_of_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   122
    )
12145
7e259e47eb0d Fix malformed messages parsing
unc0rr
parents: 12142
diff changeset
   123
    | terminated!(malformed_message, end_of_message)
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   124
    | empty_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   125
    )
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   126
);
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   127
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   128
named!(pub extract_messages<&[u8], Vec<HWProtocolMessage> >, many0!(complete!(message)));
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   129
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   130
// Due to inability to define From between Options
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   131
trait Into2<T>: Sized { fn into2(self) -> T; }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   132
impl <T> Into2<T> for T { fn into2(self) -> T { self } }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   133
impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   134
impl Into2<Option<String>> for Option<Ascii>{
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   135
    fn into2(self) -> Option<String> { self.map(|x| {x.0}) }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   136
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   137
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   138
macro_rules! proto_msg_case {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   139
    ($val: ident()) =>
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   140
        (Just($val));
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   141
    ($val: ident($arg: ty)) =>
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   142
        (any::<$arg>().prop_map(|v| {$val(v.into2())}));
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   143
    ($val: ident($arg1: ty, $arg2: ty)) =>
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   144
        (any::<($arg1, $arg2)>().prop_map(|v| {$val(v.0.into2(), v.1.into2())}));
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   145
    ($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) =>
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   146
        (any::<($arg1, $arg2, $arg3)>().prop_map(|v| {$val(v.0.into2(), v.1.into2(), v.2.into2())}));
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   147
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   148
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   149
macro_rules! proto_msg_match {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   150
    ($var: expr, def = $default: ident, $($num: expr => $constr: ident $res: tt),*) => (
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   151
        match $var {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   152
            $($num => (proto_msg_case!($constr $res)).boxed()),*,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   153
            _ => Just($default).boxed()
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   154
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   155
    )
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   156
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   157
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   158
#[derive(Debug)]
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   159
struct Ascii(String);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   160
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   161
struct AsciiValueTree(RegexGeneratorValueTree<String>);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   162
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   163
impl ValueTree for AsciiValueTree {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   164
    type Value = Ascii;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   165
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   166
    fn current(&self) -> Self::Value { Ascii(self.0.current()) }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   167
    fn simplify(&mut self) -> bool { self.0.simplify() }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   168
    fn complicate(&mut self) -> bool { self.0.complicate() }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   169
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   170
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   171
impl Arbitrary for Ascii {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   172
    type Parameters = <String as Arbitrary>::Parameters;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   173
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   174
    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   175
        any_with::<String>(args)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   176
            .prop_filter("not ascii", |s| {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   177
                s.len() > 0 && s.is_ascii() &&
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   178
                s.find(|c| {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   179
                    ['\0', '\n', '\x20'].contains(&c)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   180
                }).is_none()})
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   181
            .prop_map(Ascii)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   182
            .boxed()
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   183
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   184
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   185
    type Strategy = BoxedStrategy<Ascii>;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   186
    type ValueTree = Box<ValueTree<Value = Ascii>>;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   187
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   188
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   189
fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   190
    let res = (0..58).no_shrink().prop_flat_map(|i| {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   191
        proto_msg_match!(i, def = Malformed,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   192
        0 => Ping(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   193
        1 => Pong(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   194
        2 => Quit(Option<Ascii>),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   195
        //3 => Cmd
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   196
        4 => Global(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   197
        5 => Watch(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   198
        6 => ToggleServerRegisteredOnly(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   199
        7 => SuperPower(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   200
        8 => Info(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   201
        9 => Nick(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   202
        10 => Proto(u32),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   203
        11 => Password(Ascii, Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   204
        12 => Checker(u32, Ascii, Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   205
        13 => List(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   206
        14 => Chat(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   207
        15 => CreateRoom(Ascii, Option<Ascii>),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   208
        16 => Join(Ascii, Option<Ascii>),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   209
        17 => Follow(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   210
        //18 => Rnd(Vec<String>),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   211
        19 => Kick(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   212
        20 => Ban(Ascii, Ascii, u32),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   213
        21 => BanIP(Ascii, Ascii, u32),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   214
        22 => BanNick(Ascii, Ascii, u32),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   215
        23 => BanList(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   216
        24 => Unban(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   217
        //25 => SetServerVar(ServerVar),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   218
        26 => GetServerVar(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   219
        27 => RestartServer(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   220
        28 => Stats(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   221
        29 => Part(Option<Ascii>),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   222
        //30 => Cfg(GameCfg),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   223
        //31 => AddTeam(TeamInfo),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   224
        32 => RemoveTeam(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   225
        //33 => SetHedgehogsNumber(String, u8),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   226
        //34 => SetTeamColor(String, u8),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   227
        35 => ToggleReady(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   228
        36 => StartGame(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   229
        37 => EngineMessage(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   230
        38 => RoundFinished(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   231
        39 => ToggleRestrictJoin(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   232
        40 => ToggleRestrictTeams(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   233
        41 => ToggleRegisteredOnly(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   234
        42 => RoomName(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   235
        43 => Delegate(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   236
        44 => TeamChat(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   237
        45 => MaxTeams(u8),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   238
        46 => Fix(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   239
        47 => Unfix(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   240
        48 => Greeting(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   241
        //49 => CallVote(Option<(String, Option<String>)>),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   242
        50 => Vote(String),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   243
        51 => ForceVote(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   244
        //52 => Save(String, String),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   245
        53 => Delete(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   246
        54 => SaveRoom(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   247
        55 => LoadRoom(Ascii),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   248
        56 => Malformed(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   249
        57 => Empty()
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   250
    )});
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   251
    res.boxed()
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   252
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   253
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   254
proptest! {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   255
    #[test]
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   256
    fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   257
        println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   258
        assert_eq!(message(msg.to_raw_protocol().as_bytes()), IResult::Done(&b""[..], msg.clone()))
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   259
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   260
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   261
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   262
#[test]
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   263
fn parse_test() {
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
   264
    assert_eq!(message(b"PING\n\n"),          IResult::Done(&b""[..], Ping));
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
   265
    assert_eq!(message(b"START_GAME\n\n"),    IResult::Done(&b""[..], StartGame));
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
   266
    assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me".to_string())));
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
   267
    assert_eq!(message(b"PROTO\n51\n\n"),     IResult::Done(&b""[..], Proto(51)));
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
   268
    assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye".to_string()))));
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
   269
    assert_eq!(message(b"QUIT\n\n"),          IResult::Done(&b""[..], Quit(None)));
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
   270
    assert_eq!(message(b"CMD\nwatch\ndemo\n\n"), IResult::Done(&b""[..], Watch("demo".to_string())));
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12145
diff changeset
   271
    assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me".to_string(), "bad".to_string(), 77)));
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   272
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   273
    assert_eq!(message(b"CMD\nPART\n\n"),      IResult::Done(&b""[..], Part(None)));
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   274
    assert_eq!(message(b"CMD\nPART\n_msg_\n\n"), IResult::Done(&b""[..], Part(Some("_msg_".to_string()))));
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   275
12145
7e259e47eb0d Fix malformed messages parsing
unc0rr
parents: 12142
diff changeset
   276
    assert_eq!(extract_messages(b"QUIT\n1\n2\n\n"),    IResult::Done(&b""[..], vec![Malformed]));
7e259e47eb0d Fix malformed messages parsing
unc0rr
parents: 12142
diff changeset
   277
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   278
    assert_eq!(extract_messages(b"PING\n\nPING\n\nP"), IResult::Done(&b"P"[..], vec![Ping, Ping]));
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   279
    assert_eq!(extract_messages(b"SING\n\nPING\n\n"),  IResult::Done(&b""[..],  vec![Malformed, Ping]));
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   280
    assert_eq!(extract_messages(b"\n\n\n\nPING\n\n"),  IResult::Done(&b""[..],  vec![Empty, Empty, Ping]));
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
   281
    assert_eq!(extract_messages(b"\n\n\nPING\n\n"),    IResult::Done(&b""[..],  vec![Empty, Empty, Ping]));
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   282
}