rust/hedgewars-engine-messages/src/parser.rs
author Wuzzy <Wuzzy2@mail.ru>
Sun, 26 May 2019 17:40:38 +0200
changeset 15058 0358a907b8f4
parent 14273 645e4591361f
child 15264 7515ae6010bb
permissions -rw-r--r--
ACF6: Protect goal crate from damage
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
     1
use nom::{Err::Error, *};
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     2
use std::str;
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     3
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
     4
use super::messages::{
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
     5
    ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*,
14260
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
     6
    UnorderedEngineMessage::*, *,
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
     7
};
14210
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
     8
14211
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
     9
macro_rules! eof_slice (
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    10
  ($i:expr,) => (
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    11
    {
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    12
      if ($i).input_len() == 0 {
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    13
        Ok(($i, $i))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    14
      } else {
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    15
        Err(Error(error_position!($i, ErrorKind::Eof::<u32>)))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    16
      }
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    17
    }
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    18
  );
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    19
);
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    20
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    21
named!(unrecognized_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    22
    do_parse!(rest >> (Unknown))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    23
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    24
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    25
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    26
14210
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    27
named!(length_without_timestamp<&[u8], usize>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    28
    map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } )
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    29
);
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    30
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    31
named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    32
        do_parse!(tag!("L") >> (Left(Press)))
14211
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    33
      | do_parse!(tag!("l") >> ( Left(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    34
      | do_parse!(tag!("R") >> ( Right(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    35
      | do_parse!(tag!("r") >> ( Right(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    36
      | do_parse!(tag!("U") >> ( Up(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    37
      | do_parse!(tag!("u") >> ( Up(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    38
      | do_parse!(tag!("D") >> ( Down(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    39
      | do_parse!(tag!("d") >> ( Down(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    40
      | do_parse!(tag!("Z") >> ( Precise(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    41
      | do_parse!(tag!("z") >> ( Precise(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    42
      | do_parse!(tag!("A") >> ( Attack(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    43
      | do_parse!(tag!("a") >> ( Attack(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    44
      | do_parse!(tag!("N") >> ( NextTurn ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    45
      | do_parse!(tag!("j") >> ( LongJump ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    46
      | do_parse!(tag!("J") >> ( HighJump ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    47
      | do_parse!(tag!("S") >> ( Switch ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    48
      | do_parse!(tag!(",") >> ( Skip ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    49
      | do_parse!(tag!("1") >> ( Timer(1) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    50
      | do_parse!(tag!("2") >> ( Timer(2) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    51
      | do_parse!(tag!("3") >> ( Timer(3) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    52
      | do_parse!(tag!("4") >> ( Timer(4) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    53
      | do_parse!(tag!("5") >> ( Timer(5) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    54
      | do_parse!(tag!("p") >> x: be_i24 >> y: be_i24 >> ( Put(x, y) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    55
      | do_parse!(tag!("P") >> x: be_i24 >> y: be_i24 >> ( CursorMove(x, y) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    56
      | do_parse!(tag!("f") >> s: string_tail >> ( SyncedEngineMessage::TeamControlLost(s) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    57
      | do_parse!(tag!("g") >> s: string_tail >> ( SyncedEngineMessage::TeamControlGained(s) ))
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    58
      | do_parse!(tag!("h") >> s: string_tail >> ( HogSay(s) ))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    59
      | do_parse!(tag!("t") >> t: be_u8 >> ( Taunt(t) ))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    60
      | do_parse!(tag!("w") >> w: be_u8 >> ( SetWeapon(w) ))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    61
      | do_parse!(tag!("~") >> s: be_u8 >> ( Slot(s) ))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    62
      | do_parse!(tag!("+") >> ( Heartbeat ))
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    63
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    64
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    65
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    66
        do_parse!(tag!("F") >> s: string_tail >> ( UnsyncedEngineMessage::TeamControlLost(s) ))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    67
      | do_parse!(tag!("G") >> s: string_tail >> ( UnsyncedEngineMessage::TeamControlGained(s) ))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    68
));
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    69
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    70
named!(unordered_message<&[u8], UnorderedEngineMessage>, alt!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    71
      do_parse!(tag!("?") >> ( Ping ))
14273
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14260
diff changeset
    72
    | do_parse!(tag!("!") >> ( Pong ))
14255
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents: 14254
diff changeset
    73
    | do_parse!(tag!("E") >> s: string_tail >> ( UnorderedEngineMessage::Error(s)) )
14254
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14214
diff changeset
    74
    | do_parse!(tag!("W") >> s: string_tail >> ( Warning(s)) )
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    75
    | do_parse!(tag!("s") >> s: string_tail >> ( ChatMessage(s)) )
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    76
    | do_parse!(tag!("b") >> s: string_tail >> ( TeamMessage(s)) ) // TODO: wtf is the format
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    77
    | do_parse!(tag!("M") >> s: string_tail >> ( GameSetupChecksum(s)) )
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    78
    | do_parse!(tag!("o") >> ( StopSyncing ))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    79
    | do_parse!(tag!("I") >> ( PauseToggled ))
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    80
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    81
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    82
named!(config_message<&[u8], ConfigEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    83
    do_parse!(tag!("C") >> (ConfigRequest))
14273
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14260
diff changeset
    84
    | do_parse!(tag!("eseed ") >> s: string_tail >> ( SetSeed(s)) )
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    85
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    86
14211
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    87
named!(timestamped_message<&[u8], (SyncedEngineMessage, u16)>,
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    88
    do_parse!(msg: length_value!(length_without_timestamp, terminated!(synced_message, eof_slice!()))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    89
        >> timestamp: be_u16
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    90
        >> ((msg, timestamp))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    91
    )
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    92
);
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
    93
14210
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    94
named!(unwrapped_message<&[u8], EngineMessage>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    95
    alt!(
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    96
        map!(timestamped_message, |(m, t)| Synced(m, t as u32))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    97
        | do_parse!(tag!("#") >> (Synced(TimeWrap, 65535)))
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
    98
        | map!(unordered_message, |m| Unordered(m))
14210
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
    99
        | map!(unsynced_message, |m| Unsynced(m))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   100
        | map!(config_message, |m| Config(m))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   101
        | unrecognized_message
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   102
));
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   103
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   104
named!(length_specifier<&[u8], u16>, alt!(
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   105
    verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   106
    | map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   107
    )
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   108
);
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   109
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   110
named!(empty_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   111
    do_parse!(tag!("\0") >> (Empty))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   112
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   113
14210
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14209
diff changeset
   114
named!(non_empty_message<&[u8], EngineMessage>,
14211
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
   115
    length_value!(length_specifier, terminated!(unwrapped_message, eof_slice!())));
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   116
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   117
named!(message<&[u8], EngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   118
      empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   119
    | non_empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   120
    )
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   121
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   122
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   123
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   124
14260
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   125
pub fn extract_message(buf: &[u8]) -> Option<(usize, EngineMessage)> {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   126
    let parse_result = message(buf);
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   127
    match parse_result {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   128
        Ok((tail, msg)) => {
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   129
            let consumed = buf.len() - tail.len();
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   130
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   131
            Some((consumed, msg))
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   132
        },
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   133
        Err(Err::Incomplete(_)) => None,
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   134
        Err(Err::Error(_)) | Err(Err::Failure(_)) => unreachable!(),
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   135
    }
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   136
}
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   137
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   138
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   139
fn parse_length() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   140
    assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   141
    assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   142
    assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   143
    assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   144
    assert_eq!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   145
        length_specifier(b"\xff\xff"),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   146
        Ok((&b""[..], EngineMessage::MAX_LEN))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   147
    );
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   148
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   149
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   150
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   151
fn parse_synced_messages() {
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   152
    assert_eq!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   153
        message(b"\x03L\x01\x02"),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   154
        Ok((&b""[..], Synced(Left(Press), 258)))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   155
    );
14260
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   156
14211
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
   157
    assert_eq!(message(b"\x01#"), Ok((&b""[..], Synced(TimeWrap, 65535))));
14254
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14214
diff changeset
   158
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14214
diff changeset
   159
    assert_eq!(message(&vec![9, b'p', 255, 133, 151, 1, 0, 2, 0, 0]), Ok((&b""[..], Synced(Put(-31337, 65538), 0))));
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   160
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   161
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   162
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   163
fn parse_unsynced_messages() {
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   164
    assert_eq!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   165
        message(b"\x06shello"),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   166
        Ok((&b""[..], Unordered(ChatMessage(String::from("hello")))))
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   167
    );
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   168
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   169
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   170
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   171
fn parse_incorrect_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   172
    assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   173
    assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
14211
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
   174
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
   175
    // garbage after correct message
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14210
diff changeset
   176
    assert_eq!(message(b"\x04La\x01\x02"), Ok((&b""[..], Unknown)));
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   177
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   178
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   179
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   180
fn parse_config_messages() {
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   181
    assert_eq!(message(b"\x01C"), Ok((&b""[..], Config(ConfigRequest))));
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   182
}
14214
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14211
diff changeset
   183
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   184
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   185
fn parse_test_general() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   186
    assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
14260
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   187
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   188
    assert_eq!(extract_message(b"\x02#"), None);
f0c0d2d217c3 IPC implementation
unC0Rr
parents: 14255
diff changeset
   189
    assert_eq!(extract_message(b"\x01#"), Some((2, Synced(TimeWrap, 65535))));
14209
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   190
}