rust/hedgewars-engine-messages/src/parser.rs
author unC0Rr
Wed, 14 Nov 2018 16:32:42 +0100
changeset 14215 1c55c54013c4
parent 14214 ab1ef7074e07
child 14216 0f7b92397b9e
permissions -rw-r--r--
Recognize timestamps
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
     1
use nom::{*};
14214
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
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
     6
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     7
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     8
named!(unrecognized_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     9
    do_parse!(rest >> (Unknown))
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!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    13
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    14
named!(length_without_timestamp<&[u8], usize>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    15
    map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } )
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    16
);
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    17
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    18
named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    19
      do_parse!(tag!("L") >> (Left(Press)))
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    20
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    21
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    22
named!(timestamped_message<&[u8], (SyncedEngineMessage, u16)>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    23
    do_parse!(msg: length_value!(length_without_timestamp, synced_message)
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    24
        >> timestamp: be_u16
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    25
        >> ((msg, timestamp))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    26
    )
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    27
);
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    28
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    29
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    30
      do_parse!(tag!("?") >> (Ping))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    31
    | do_parse!(tag!("!") >> (Ping))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    32
    | do_parse!(tag!("esay ") >> s: string_tail  >> (Say(s)))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    33
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    34
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    35
named!(config_message<&[u8], ConfigEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    36
    do_parse!(tag!("C") >> (ConfigRequest))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    37
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    38
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    39
named!(unwrapped_message<&[u8], EngineMessage>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    40
    alt!(
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    41
        map!(timestamped_message, |(m, t)| Synced(m, t as u32))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    42
        | do_parse!(tag!("#") >> (Synced(TimeWrap, 65535)))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    43
        | map!(unsynced_message, |m| Unsynced(m))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    44
        | map!(config_message, |m| Config(m))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    45
        | unrecognized_message
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    46
));
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    47
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    48
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    49
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    50
named!(length_specifier<&[u8], u16>, alt!(
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    51
    verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    52
    | map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    53
    )
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    54
);
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    55
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    56
named!(empty_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    57
    do_parse!(tag!("\0") >> (Empty))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    58
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    59
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    60
named!(non_empty_message<&[u8], EngineMessage>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    61
    length_value!(length_specifier, unwrapped_message));
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    62
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    63
named!(message<&[u8], EngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    64
      empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    65
    | non_empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    66
    )
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    67
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    68
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    69
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    70
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    71
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    72
fn parse_length() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    73
    assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    74
    assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    75
    assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    76
    assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    77
    assert_eq!(length_specifier(b"\xff\xff"), Ok((&b""[..], 49215)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    78
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    79
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    80
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    81
fn parse_synced_messages() {
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    82
    assert_eq!(message(b"\x03L\x01\x02"), Ok((&b""[..], Synced(Left(Press), 258))));
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    83
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    84
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    85
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    86
fn parse_unsynced_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    87
    assert_eq!(message(b"\x0aesay hello"), Ok((&b""[..], Unsynced(Say(String::from("hello"))))));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    88
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    89
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    90
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    91
fn parse_incorrect_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    92
    assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    93
    assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    94
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    95
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    96
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    97
fn parse_config_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    98
    assert_eq!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    99
        message(b"\x01C"),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   100
        Ok((
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   101
            &b""[..],
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   102
            Config(ConfigRequest)
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   103
        ))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   104
    );
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   105
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   106
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   107
fn parse_test_general() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   108
    assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   109
}