rust/hedgewars-engine-messages/src/lib.rs
author unC0Rr
Tue, 13 Nov 2018 16:54:40 +0100
changeset 14211 257b296169a8
parent 14210 a8fe9cd51154
child 14214 ab1ef7074e07
permissions -rw-r--r--
Start working on engine messages parser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
     1
use nom::*;
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
     2
use std::str;
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
     3
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
     4
#[derive(Debug, PartialEq)]
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
     5
pub enum KeystrokeAction {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
     6
    Press,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
     7
    Release,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
     8
}
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
     9
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
    10
#[derive(Debug, PartialEq)]
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    11
pub enum SyncedEngineMessage {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    12
    Left(KeystrokeAction),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    13
    Right(KeystrokeAction),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    14
    Up(KeystrokeAction),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    15
    Down(KeystrokeAction),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    16
    Precise(KeystrokeAction),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    17
    Attack(KeystrokeAction),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    18
    NextTurn,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    19
    Switch,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    20
    Timer(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    21
    Slot(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    22
    SetWeapon(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    23
    Put(i32, i32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    24
    HighJump,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    25
    LowJump,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    26
    Skip,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    27
    TeamControlGained(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    28
    TeamControlLost(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    29
}
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    30
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
    31
#[derive(Debug, PartialEq)]
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    32
pub enum UnsyncedEngineMessage {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    33
    Ping,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    34
    Pong,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    35
    Say(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    36
    Taunt(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    37
    GameType(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    38
    Warning(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    39
    StopSyncing,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    40
    GameOver,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    41
    GameInterrupted,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    42
    GameSetupChecksum(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    43
}
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    44
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
    45
#[derive(Debug, PartialEq)]
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    46
pub enum ConfigEngineMessage {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    47
    ConfigRequest,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    48
    SetAmmo(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    49
    SetScript(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    50
    SetScriptParam(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    51
    Spectate,
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    52
    TeamLocality(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    53
    SetMap(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    54
    SetTheme(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    55
    SetSeed(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    56
    SetTemplateFilter(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    57
    SetMapGenerator(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    58
    SetFeatureSize(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    59
    SetDelay(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    60
    SetReadyDelay(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    61
    SetCratesFrequency(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    62
    SetHealthCrateProbability(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    63
    SetHealthCratesNumber(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    64
    SetRoundsTilSuddenDeath(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    65
    SetSuddenDeathWaterRiseSpeed(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    66
    SetSuddenDeathHealthDecreaseRate(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    67
    SetDamageMultiplier(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    68
    SetRopeLength(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    69
    SetGetawayTime(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    70
    SetDudMinesPercent(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    71
    SetMinesNumber(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    72
    SetAirMinesNumber(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    73
    SetBarrelsNumber(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    74
    SetTurnTime(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    75
    SetMinesTime(u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    76
    SetWorldEdge(u8),
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
    77
    Draw,
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
    78
    // TODO
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    79
    SetVoicePack(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    80
    AddHedgehog(String, u8, u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    81
    AddTeam(String, u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    82
    SetHedgehogCoordinates(i32, i32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    83
    SetFort(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    84
    SetGrave(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    85
    SetHat(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    86
    SetFlag(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    87
    SetOwner(String),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    88
    SetOneClanMode(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    89
    SetMultishootMode(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    90
    SetSolidLand(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    91
    SetBorders(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    92
    SetDivideTeams(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    93
    SetLowGravity(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    94
    SetLaserSight(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    95
    SetInvulnerability(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    96
    SetHealthReset(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    97
    SetVampiric(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    98
    SetKarma(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
    99
    SetArtilleryMode(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   100
    SetHedgehogSwitch(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   101
    SetRandomOrder(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   102
    SetKingMode(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   103
    SetPlaceHedgehog(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   104
    SetSharedAmmo(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   105
    SetGirdersEnabled(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   106
    SetLandObjectsEnabled(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   107
    SetAISurvivalMode(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   108
    SetInfiniteAttack(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   109
    SetResetWeapons(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   110
    SetAmmoPerHedgehog(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   111
    SetWindMode(u8),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   112
    SetTagTeam(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   113
    SetBottomBorder(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   114
    SetShoppaBorder(bool),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   115
}
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   116
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   117
#[derive(Debug, PartialEq)]
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   118
pub enum EngineMessage {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   119
    Synced(SyncedEngineMessage, u32),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   120
    Unsynced(UnsyncedEngineMessage),
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   121
    Config(ConfigEngineMessage),
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   122
    Unknown,
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   123
    Empty,
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   124
}
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   125
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   126
impl EngineMessage {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   127
    fn from_bytes(buf: &[u8]) -> Self {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   128
        unimplemented!()
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   129
    }
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   130
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   131
    fn to_bytes(&self) -> Vec<u8> {
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   132
        unimplemented!()
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   133
    }
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   134
}
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   135
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   136
named!(length_specifier<&[u8], u16>, alt!(
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   137
    verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   138
    | map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   139
    )
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   140
);
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   141
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   142
named!(unrecognized_message<&[u8], EngineMessage>,
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   143
    do_parse!(rest >> (EngineMessage::Unknown))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   144
);
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   145
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   146
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   147
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   148
named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   149
      do_parse!(tag!("+l") >> (SyncedEngineMessage::Left(KeystrokeAction::Press)))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   150
));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   151
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   152
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   153
      do_parse!(tag!("?") >> (UnsyncedEngineMessage::Ping))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   154
    | do_parse!(tag!("!") >> (UnsyncedEngineMessage::Ping))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   155
    | do_parse!(tag!("esay ") >> s: string_tail  >> (UnsyncedEngineMessage::Say(s)))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   156
));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   157
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   158
named!(config_message<&[u8], ConfigEngineMessage>, alt!(
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   159
    do_parse!(tag!("C") >> (ConfigEngineMessage::ConfigRequest))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   160
));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   161
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   162
named!(empty_message<&[u8], EngineMessage>,
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   163
    do_parse!(tag!("\0") >> (EngineMessage::Empty))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   164
);
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   165
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   166
named!(non_empty_message<&[u8], EngineMessage>, length_value!(length_specifier,
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   167
    alt!(
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   168
          map!(synced_message, |m| EngineMessage::Synced(m, 0))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   169
        | map!(unsynced_message, |m| EngineMessage::Unsynced(m))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   170
        | map!(config_message, |m| EngineMessage::Config(m))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   171
        | unrecognized_message
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   172
    )
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   173
));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   174
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   175
named!(message<&[u8], EngineMessage>, alt!(
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   176
      empty_message
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   177
    | non_empty_message
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   178
    )
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   179
);
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   180
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   181
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message)));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   182
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   183
#[test]
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   184
fn parse_length() {
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   185
    assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   186
    assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   187
    assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   188
    assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   189
    assert_eq!(length_specifier(b"\xff\xff"), Ok((&b""[..], 49215)));
14210
a8fe9cd51154 Move engine messages into separate lib for reuse
unc0rr
parents:
diff changeset
   190
}
14211
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   191
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   192
#[test]
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   193
fn parse_synced_messages() {
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   194
    assert_eq!(message(b"\x02+l"), Ok((&b""[..], EngineMessage::Synced(SyncedEngineMessage::Left(KeystrokeAction::Press), 0))));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   195
}
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   196
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   197
#[test]
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   198
fn parse_unsynced_messages() {
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   199
    assert_eq!(message(b"\x0aesay hello"), Ok((&b""[..], EngineMessage::Unsynced(UnsyncedEngineMessage::Say(String::from("hello"))))));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   200
}
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   201
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   202
#[test]
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   203
fn parse_incorrect_messages() {
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   204
    assert_eq!(message(b"\x00"), Ok((&b""[..], EngineMessage::Empty)));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   205
    assert_eq!(message(b"\x01\x00"), Ok((&b""[..], EngineMessage::Unknown)));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   206
}
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   207
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   208
#[test]
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   209
fn parse_config_messages() {
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   210
    assert_eq!(
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   211
        message(b"\x01C"),
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   212
        Ok((
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   213
            &b""[..],
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   214
            EngineMessage::Config(ConfigEngineMessage::ConfigRequest)
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   215
        ))
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   216
    );
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   217
}
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   218
#[test]
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   219
fn parse_test_general() {
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   220
    assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
257b296169a8 Start working on engine messages parser
unC0Rr
parents: 14210
diff changeset
   221
}