rust/hedgewars-engine-messages/src/parser.rs
author unC0Rr
Wed, 14 Nov 2018 11:09:01 +0100
changeset 14209 ab1ef7074e07
child 14210 1c55c54013c4
permissions -rw-r--r--
Rearrange code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     1
use nom::*;
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     2
use std::str;
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     3
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     4
use super::messages::{*, EngineMessage::*, UnsyncedEngineMessage::*, SyncedEngineMessage::*, ConfigEngineMessage::*, KeystrokeAction::*};
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     5
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     6
named!(length_specifier<&[u8], u16>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     7
    verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     8
    | map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     9
    )
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    10
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    11
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    12
named!(unrecognized_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    13
    do_parse!(rest >> (Unknown))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    14
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    15
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    16
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    17
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    18
named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    19
      do_parse!(tag!("+l") >> (Left(Press)))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    20
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    21
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    22
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    23
      do_parse!(tag!("?") >> (Ping))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    24
    | do_parse!(tag!("!") >> (Ping))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    25
    | do_parse!(tag!("esay ") >> s: string_tail  >> (Say(s)))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    26
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    27
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    28
named!(config_message<&[u8], ConfigEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    29
    do_parse!(tag!("C") >> (ConfigRequest))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    30
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    31
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    32
named!(empty_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    33
    do_parse!(tag!("\0") >> (Empty))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    34
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    35
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    36
named!(non_empty_message<&[u8], EngineMessage>, length_value!(length_specifier,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    37
    alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    38
          map!(synced_message, |m| Synced(m, 0))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    39
        | map!(unsynced_message, |m| Unsynced(m))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    40
        | map!(config_message, |m| Config(m))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    41
        | unrecognized_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    42
    )
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    43
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    44
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    45
named!(message<&[u8], EngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    46
      empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    47
    | non_empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    48
    )
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    49
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    50
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    51
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    52
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    53
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    54
fn parse_length() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    55
    assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    56
    assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    57
    assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    58
    assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    59
    assert_eq!(length_specifier(b"\xff\xff"), Ok((&b""[..], 49215)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    60
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    61
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    62
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    63
fn parse_synced_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    64
    assert_eq!(message(b"\x04+l\x01\x01"), Ok((&b""[..], Synced(Left(Press), 0))));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    65
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    66
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    67
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    68
fn parse_unsynced_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    69
    assert_eq!(message(b"\x0aesay hello"), Ok((&b""[..], Unsynced(Say(String::from("hello"))))));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    70
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    71
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    72
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    73
fn parse_incorrect_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    74
    assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    75
    assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    76
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    77
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    78
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    79
fn parse_config_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    80
    assert_eq!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    81
        message(b"\x01C"),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    82
        Ok((
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    83
            &b""[..],
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    84
            Config(ConfigRequest)
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    85
        ))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    86
    );
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    87
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    88
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    89
fn parse_test_general() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    90
    assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    91
}