gameServer2/src/protocol/parser.rs
author alfadur
Fri, 02 Nov 2018 19:37:47 +0300
changeset 14109 f483f844da98
parent 13810 0463a4221327
child 14371 31717e1436cd
permissions -rw-r--r--
component-wise division is actually useful sometimes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     1
/** The parsers for the chat and multiplayer protocol. The main parser is `message`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     2
 * # Protocol
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     3
 * All messages consist of `\n`-separated strings. The end of a message is
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     4
 * indicated by a double newline - `\n\n`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     5
 *
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     6
 * For example, a nullary command like PING will be actually sent as `PING\n\n`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     7
 * A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     8
 */
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     9
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    10
use nom::*;
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    11
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    12
use std::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    13
    str, str::FromStr,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    14
    ops::Range
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    15
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    16
use super::{
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13709
diff changeset
    17
    messages::{HWProtocolMessage, HWProtocolMessage::*}
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    18
};
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13709
diff changeset
    19
#[cfg(test)]
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13775
diff changeset
    20
use {
0463a4221327 cleanup crate imports
alfadur
parents: 13775
diff changeset
    21
    super::test::gen_proto_msg,
0463a4221327 cleanup crate imports
alfadur
parents: 13775
diff changeset
    22
    proptest::{proptest, proptest_helper}
0463a4221327 cleanup crate imports
alfadur
parents: 13775
diff changeset
    23
};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    24
use crate::server::coretypes::{
13709
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
    25
    HedgehogInfo, TeamInfo, GameCfg, VoteType, MAX_HEDGEHOGS_PER_TEAM
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    26
};
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
    27
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    28
named!(end_of_message, tag!("\n\n"));
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12140
diff changeset
    29
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: 12140
diff changeset
    30
named!(  a_line<&[u8], String>, map!(str_line, String::from));
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13500
diff changeset
    31
named!(cmd_arg<&[u8], String>,
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13500
diff changeset
    32
    map!(map_res!(take_until_either!(" \n"), str::from_utf8), String::from));
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12140
diff changeset
    33
named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13450
diff changeset
    34
named!(u16_line<&[u8],    u16>, map_res!(str_line, FromStr::from_str));
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12140
diff changeset
    35
named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    36
named!(yes_no_line<&[u8], bool>, alt!(
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    37
      do_parse!(tag_no_case!("YES") >> (true))
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    38
    | do_parse!(tag_no_case!("NO") >> (false))));
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
    39
named!(opt_param<&[u8], Option<String> >, alt!(
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
    40
      do_parse!(peek!(tag!("\n\n")) >> (None))
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
    41
    | do_parse!(tag!("\n") >> s: str_line >> (Some(s.to_string())))));
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
    42
named!(spaces<&[u8], &[u8]>, preceded!(tag!(" "), eat_separator!(" ")));
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
    43
named!(opt_space_param<&[u8], Option<String> >, alt!(
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
    44
      do_parse!(peek!(tag!("\n\n")) >> (None))
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
    45
    | do_parse!(spaces >> s: str_line >> (Some(s.to_string())))));
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    46
named!(hog_line<&[u8], HedgehogInfo>,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    47
    do_parse!(name: str_line >> eol >> hat: str_line >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    48
        (HedgehogInfo{name: name.to_string(), hat: hat.to_string()})));
13709
e335daaa77a9 Add hogs per team named constant that absolutely no one asked for
alfadur
parents: 13666
diff changeset
    49
named!(_8_hogs<&[u8], [HedgehogInfo; MAX_HEDGEHOGS_PER_TEAM as usize]>,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    50
    do_parse!(h1: hog_line >> eol >> h2: hog_line >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    51
              h3: hog_line >> eol >> h4: hog_line >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    52
              h5: hog_line >> eol >> h6: hog_line >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    53
              h7: hog_line >> eol >> h8: hog_line >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    54
              ([h1, h2, h3, h4, h5, h6, h7, h8])));
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    55
named!(voting<&[u8], VoteType>, alt!(
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    56
      do_parse!(tag_no_case!("KICK") >> spaces >> n: a_line >>
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    57
        (VoteType::Kick(n)))
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    58
    | do_parse!(tag_no_case!("MAP") >>
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    59
        n: opt!(preceded!(spaces, a_line)) >>
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    60
        (VoteType::Map(n)))
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    61
    | do_parse!(tag_no_case!("PAUSE") >>
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    62
        (VoteType::Pause))
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    63
    | do_parse!(tag_no_case!("NEWSEED") >>
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    64
        (VoteType::NewSeed))
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    65
    | do_parse!(tag_no_case!("HEDGEHOGS") >> spaces >> n: u8_line >>
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
    66
        (VoteType::HedgehogsPerTeam(n)))));
12134
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12133
diff changeset
    67
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
    68
/** Recognizes messages which do not take any parameters */
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    69
named!(basic_message<&[u8], HWProtocolMessage>, alt!(
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    70
      do_parse!(tag!("PING") >> (Ping))
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    71
    | do_parse!(tag!("PONG") >> (Pong))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    72
    | do_parse!(tag!("LIST") >> (List))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    73
    | do_parse!(tag!("BANLIST")        >> (BanList))
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    74
    | do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    75
    | do_parse!(tag!("TOGGLE_READY")   >> (ToggleReady))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    76
    | do_parse!(tag!("START_GAME")     >> (StartGame))
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    77
    | do_parse!(tag!("ROUNDFINISHED")  >> _m: opt_param >> (RoundFinished))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    78
    | do_parse!(tag!("TOGGLE_RESTRICT_JOINS")  >> (ToggleRestrictJoin))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    79
    | do_parse!(tag!("TOGGLE_RESTRICT_TEAMS")  >> (ToggleRestrictTeams))
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    80
    | do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    81
));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    82
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
    83
/** Recognizes messages which take exactly one parameter */
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    84
named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    85
      do_parse!(tag!("NICK")    >> eol >> n: a_line >> (Nick(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    86
    | do_parse!(tag!("INFO")    >> eol >> n: a_line >> (Info(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    87
    | do_parse!(tag!("CHAT")    >> eol >> m: a_line >> (Chat(m)))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    88
    | do_parse!(tag!("PART")    >> msg: opt_param   >> (Part(msg)))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    89
    | do_parse!(tag!("FOLLOW")  >> eol >> n: a_line >> (Follow(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    90
    | do_parse!(tag!("KICK")    >> eol >> n: a_line >> (Kick(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    91
    | do_parse!(tag!("UNBAN")   >> eol >> n: a_line >> (Unban(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    92
    | do_parse!(tag!("EM")      >> eol >> m: a_line >> (EngineMessage(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    93
    | do_parse!(tag!("TEAMCHAT")    >> eol >> m: a_line >> (TeamChat(m)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    94
    | do_parse!(tag!("ROOM_NAME")   >> eol >> n: a_line >> (RoomName(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    95
    | do_parse!(tag!("REMOVE_TEAM") >> eol >> n: a_line >> (RemoveTeam(n)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    96
13486
1ee192f13456 Better packing for clients
alfadur
parents: 13450
diff changeset
    97
    | do_parse!(tag!("PROTO")   >> eol >> d: u16_line >> (Proto(d)))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    98
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
    99
    | do_parse!(tag!("QUIT")   >> msg: opt_param >> (Quit(msg)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   100
));
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   101
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   102
/** Recognizes messages preceded with CMD */
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   103
named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!(
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   104
      do_parse!(tag_no_case!("STATS") >> (Stats))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   105
    | do_parse!(tag_no_case!("FIX")   >> (Fix))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   106
    | do_parse!(tag_no_case!("UNFIX") >> (Unfix))
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   107
    | do_parse!(tag_no_case!("RESTART_SERVER") >> spaces >> tag!("YES") >> (RestartServer))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   108
    | do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   109
    | do_parse!(tag_no_case!("SUPER_POWER")     >> (SuperPower))
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   110
    | do_parse!(tag_no_case!("PART")     >> m: opt_space_param >> (Part(m)))
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   111
    | do_parse!(tag_no_case!("QUIT")     >> m: opt_space_param >> (Quit(m)))
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   112
    | do_parse!(tag_no_case!("DELEGATE") >> spaces >> n: a_line  >> (Delegate(n)))
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13500
diff changeset
   113
    | do_parse!(tag_no_case!("SAVE")     >> spaces >> n: cmd_arg >> spaces >> l: cmd_arg >> (Save(n, l)))
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13500
diff changeset
   114
    | do_parse!(tag_no_case!("DELETE")   >> spaces >> n: a_line  >> (Delete(n)))
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   115
    | do_parse!(tag_no_case!("SAVEROOM") >> spaces >> r: a_line  >> (SaveRoom(r)))
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   116
    | do_parse!(tag_no_case!("LOADROOM") >> spaces >> r: a_line  >> (LoadRoom(r)))
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   117
    | do_parse!(tag_no_case!("GLOBAL")   >> spaces >> m: a_line  >> (Global(m)))
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   118
    | do_parse!(tag_no_case!("WATCH")    >> spaces >> i: a_line  >> (Watch(i)))
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   119
    | do_parse!(tag_no_case!("GREETING") >> spaces >> m: a_line  >> (Greeting(m)))
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
   120
    | do_parse!(tag_no_case!("VOTE")     >> spaces >> m: yes_no_line >> (Vote(m)))
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
   121
    | do_parse!(tag_no_case!("FORCE")    >> spaces >> m: yes_no_line >> (ForceVote(m)))
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   122
    | do_parse!(tag_no_case!("INFO")     >> spaces >> n: a_line  >> (Info(n)))
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   123
    | do_parse!(tag_no_case!("MAXTEAMS") >> spaces >> n: u8_line >> (MaxTeams(n)))
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
   124
    | do_parse!(tag_no_case!("CALLVOTE") >>
d79795acaa73 Mostly implement voting
alfadur
parents: 13441
diff changeset
   125
        v: opt!(preceded!(spaces, voting)) >> (CallVote(v)))
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   126
    | do_parse!(
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13431
diff changeset
   127
        tag_no_case!("RND") >> alt!(spaces | peek!(end_of_message)) >>
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   128
        v: str_line >>
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   129
        (Rnd(v.split_whitespace().map(String::from).collect())))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   130
)));
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   131
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   132
named!(complex_message<&[u8], HWProtocolMessage>, alt!(
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   133
      do_parse!(tag!("PASSWORD")  >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   134
                    p: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   135
                    s: a_line     >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   136
                    (Password(p, s)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   137
    | do_parse!(tag!("CHECKER")   >> eol >>
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13713
diff changeset
   138
                    i: u16_line   >> eol >>
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   139
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   140
                    p: a_line     >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   141
                    (Checker(i, n, p)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   142
    | do_parse!(tag!("CREATE_ROOM") >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   143
                    n: a_line       >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   144
                    p: opt_param    >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   145
                    (CreateRoom(n, p)))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   146
    | do_parse!(tag!("JOIN_ROOM")   >> eol >>
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   147
                    n: a_line       >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   148
                    p: opt_param    >>
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   149
                    (JoinRoom(n, p)))
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   150
    | do_parse!(tag!("ADD_TEAM")    >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   151
                    name: a_line    >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   152
                    color: u8_line  >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   153
                    grave: a_line   >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   154
                    fort: a_line    >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   155
                    voice_pack: a_line >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   156
                    flag: a_line    >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   157
                    difficulty: u8_line >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   158
                    hedgehogs: _8_hogs >>
13500
5359ff75da3a indulge clippy
alfadur
parents: 13486
diff changeset
   159
                    (AddTeam(Box::new(TeamInfo{
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   160
                        name, color, grave, fort,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   161
                        voice_pack, flag, difficulty,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   162
                        hedgehogs, hedgehogs_number: 0
13500
5359ff75da3a indulge clippy
alfadur
parents: 13486
diff changeset
   163
                     }))))
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   164
    | do_parse!(tag!("HH_NUM")    >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   165
                    n: a_line     >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   166
                    c: u8_line    >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   167
                    (SetHedgehogsNumber(n, c)))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   168
    | do_parse!(tag!("TEAM_COLOR")    >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   169
                    n: a_line     >> eol >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   170
                    c: u8_line    >>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   171
                    (SetTeamColor(n, c)))
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   172
    | do_parse!(tag!("BAN")    >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   173
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   174
                    r: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   175
                    t: u32_line   >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   176
                    (Ban(n, r, t)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   177
    | do_parse!(tag!("BAN_IP")    >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   178
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   179
                    r: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   180
                    t: u32_line   >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   181
                    (BanIP(n, r, t)))
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   182
    | do_parse!(tag!("BAN_NICK")    >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   183
                    n: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   184
                    r: a_line     >> eol >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   185
                    t: u32_line   >>
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   186
                    (BanNick(n, r, t)))
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   187
));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   188
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   189
named!(cfg_message<&[u8], HWProtocolMessage>, preceded!(tag!("CFG\n"), map!(alt!(
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   190
      do_parse!(tag!("THEME")    >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   191
                name: a_line     >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   192
                (GameCfg::Theme(name)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   193
    | do_parse!(tag!("SCRIPT")   >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   194
                name: a_line     >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   195
                (GameCfg::Script(name)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   196
    | do_parse!(tag!("AMMO")     >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   197
                name: a_line     >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   198
                value: opt_param >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   199
                (GameCfg::Ammo(name, value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   200
    | do_parse!(tag!("SCHEME")   >> eol >>
13441
d47514fd6c6a Fix scheme config parsing
alfadur
parents: 13438
diff changeset
   201
                name: a_line     >>
d47514fd6c6a Fix scheme config parsing
alfadur
parents: 13438
diff changeset
   202
                values: opt!(preceded!(eol, separated_list!(eol, a_line))) >>
13775
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13771
diff changeset
   203
                (GameCfg::Scheme(name, values.unwrap_or(Vec::new()))))
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   204
    | do_parse!(tag!("FEATURE_SIZE") >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   205
                value: u32_line    >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   206
                (GameCfg::FeatureSize(value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   207
    | do_parse!(tag!("MAP")      >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   208
                value: a_line    >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   209
                (GameCfg::MapType(value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   210
    | do_parse!(tag!("MAPGEN")   >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   211
                value: u32_line  >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   212
                (GameCfg::MapGenerator(value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   213
    | do_parse!(tag!("MAZE_SIZE") >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   214
                value: u32_line   >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   215
                (GameCfg::MazeSize(value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   216
    | do_parse!(tag!("SEED")     >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   217
                value: a_line    >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   218
                (GameCfg::Seed(value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   219
    | do_parse!(tag!("TEMPLATE") >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   220
                value: u32_line  >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   221
                (GameCfg::Template(value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   222
    | do_parse!(tag!("DRAWNMAP") >> eol >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   223
                value: a_line    >>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   224
                (GameCfg::DrawnMap(value)))
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   225
), Cfg)));
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   226
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   227
named!(malformed_message<&[u8], HWProtocolMessage>,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   228
    do_parse!(separated_list!(eol, a_line) >> (Malformed)));
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   229
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   230
named!(empty_message<&[u8], HWProtocolMessage>,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   231
    do_parse!(alt!(end_of_message | eol) >> (Empty)));
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   232
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   233
named!(message<&[u8], HWProtocolMessage>, alt!(terminated!(
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   234
    alt!(
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   235
          basic_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   236
        | one_param_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   237
        | cmd_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   238
        | complex_message
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   239
        | cfg_message
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   240
        ), end_of_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   241
    )
12140
7e259e47eb0d Fix malformed messages parsing
unc0rr
parents: 12137
diff changeset
   242
    | terminated!(malformed_message, end_of_message)
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   243
    | empty_message
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   244
    )
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   245
);
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   246
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   247
named!(pub extract_messages<&[u8], Vec<HWProtocolMessage> >, many0!(complete!(message)));
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   248
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13709
diff changeset
   249
#[cfg(test)]
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   250
proptest! {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   251
    #[test]
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   252
    fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   253
        println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   254
        assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   255
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   256
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   257
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   258
#[test]
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   259
fn parse_test() {
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   260
    assert_eq!(message(b"PING\n\n"),          Ok((&b""[..], Ping)));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   261
    assert_eq!(message(b"START_GAME\n\n"),    Ok((&b""[..], StartGame)));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   262
    assert_eq!(message(b"NICK\nit's me\n\n"), Ok((&b""[..], Nick("it's me".to_string()))));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   263
    assert_eq!(message(b"PROTO\n51\n\n"),     Ok((&b""[..], Proto(51))));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   264
    assert_eq!(message(b"QUIT\nbye-bye\n\n"), Ok((&b""[..], Quit(Some("bye-bye".to_string())))));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   265
    assert_eq!(message(b"QUIT\n\n"),          Ok((&b""[..], Quit(None))));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   266
    assert_eq!(message(b"CMD\nwatch demo\n\n"), Ok((&b""[..], Watch("demo".to_string()))));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   267
    assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77))));
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   268
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   269
    assert_eq!(message(b"CMD\nPART\n\n"),      Ok((&b""[..], Part(None))));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   270
    assert_eq!(message(b"CMD\nPART _msg_\n\n"), Ok((&b""[..], Part(Some("_msg_".to_string())))));
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   271
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   272
    assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   273
    assert_eq!(
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   274
        message(b"CMD\nRND A B\n\n"),
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   275
        Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   276
    );
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   277
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   278
    assert_eq!(extract_messages(b"QUIT\n1\n2\n\n"),    Ok((&b""[..], vec![Malformed])));
12140
7e259e47eb0d Fix malformed messages parsing
unc0rr
parents: 12137
diff changeset
   279
13438
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   280
    assert_eq!(extract_messages(b"PING\n\nPING\n\nP"), Ok((&b"P"[..], vec![Ping, Ping])));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   281
    assert_eq!(extract_messages(b"SING\n\nPING\n\n"),  Ok((&b""[..],  vec![Malformed, Ping])));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   282
    assert_eq!(extract_messages(b"\n\n\n\nPING\n\n"),  Ok((&b""[..],  vec![Empty, Empty, Ping])));
da71e0d88a1c update nom
alfadur
parents: 13432
diff changeset
   283
    assert_eq!(extract_messages(b"\n\n\nPING\n\n"),    Ok((&b""[..],  vec![Empty, Empty, Ping])));
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
   284
}