gameServer2/src/protocol/parser.rs
author unc0rr
Sun, 08 Jan 2017 23:57:45 +0300
changeset 12133 81df2e1f9ae9
child 12134 109e6765b1fc
permissions -rw-r--r--
Some parsing using nom
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12133
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));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    10
//fn number_line<T>(input: &[u8]) -> IResult<&[u8], T> {
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    11
//}
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)))
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    29
));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    30
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    31
named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    32
    basic_message
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    33
    | one_param_message
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    34
), end_of_message));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    35
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    36
named!(messages<&[u8], Vec<HWProtocolMessage> >, many0!(message));
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() {
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    40
    assert_eq!(message(b"PING\n\n"), IResult::Done(&b""[..], Ping));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    41
    assert_eq!(message(b"START_GAME\n\n"), IResult::Done(&b""[..], StartGame));
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")));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    43
    assert_eq!(message(b"PROTO\n51\n\n"), IResult::Done(&b""[..], Proto(51)));
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    44
}