gameServer2/src/protocol/parser.rs
author unc0rr
Wed, 11 Jan 2017 17:39:21 +0300
changeset 12139 109e6765b1fc
parent 12138 81df2e1f9ae9
child 12140 23ee939ba66a
permissions -rw-r--r--
Optional parameters parsing function
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
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     8
named!(end_of_message, tag!("\n\n"));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     9
named!(a_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8));
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    10
named!(opt_param<&[u8], Option<&str> >, opt!(flat_map!(preceded!(eol, a_line), non_empty)));
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    11
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    12
named!(basic_message<&[u8], HWProtocolMessage>, alt!(
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    13
    do_parse!(tag!("PING") >> (Ping))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    14
    | do_parse!(tag!("PONG") >> (Pong))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    15
    | do_parse!(tag!("LIST") >> (List))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    16
    | do_parse!(tag!("BANLIST") >> (BanList))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    17
    | do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    18
    | do_parse!(tag!("TOGGLE_READY") >> (ToggleReady))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    19
    | do_parse!(tag!("START_GAME") >> (StartGame))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    20
    | do_parse!(tag!("ROUNDFINISHED") >> (RoundFinished))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    21
    | do_parse!(tag!("TOGGLE_RESTRICT_JOINS") >> (ToggleRestrictJoin))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    22
    | do_parse!(tag!("TOGGLE_RESTRICT_TEAMS") >> (ToggleRestrictTeams))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    23
    | do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    24
));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    25
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    26
named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    27
    do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n)))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    28
    | do_parse!(tag!("PROTO") >> eol >> d: map_res!(a_line, FromStr::from_str) >> (Proto(d)))
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    29
    | do_parse!(tag!("QUIT") >> msg: opt_param >> (Quit(msg)))
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    30
));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    31
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    32
named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    33
    basic_message
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    34
    | one_param_message
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    35
), end_of_message));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    36
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    37
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    38
#[test]
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    39
fn parse_test() {
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    40
    assert_eq!(message(b"PING\n\n"),          IResult::Done(&b""[..], Ping));
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    41
    assert_eq!(message(b"START_GAME\n\n"),    IResult::Done(&b""[..], StartGame));
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    42
    assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me")));
12139
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    43
    assert_eq!(message(b"PROTO\n51\n\n"),     IResult::Done(&b""[..], Proto(51)));
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    44
    assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye"))));
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12138
diff changeset
    45
    assert_eq!(message(b"QUIT\n\n"),          IResult::Done(&b""[..], Quit(None)));
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    46
}