rust/hedgewars-server/src/protocol/parser.rs
author Wuzzy <Wuzzy2@mail.ru>
Mon, 29 Apr 2019 21:24:42 +0200
changeset 14870 24df603fbcd2
parent 14816 add191d825f4
child 15095 c5a6e8566425
permissions -rw-r--r--
Match key names in sdlkeys with SDL-pretty printed names (for localization)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13431
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     1
/** The parsers for the chat and multiplayer protocol. The main parser is `message`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     2
 * # Protocol
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     3
 * All messages consist of `\n`-separated strings. The end of a message is
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     4
 * indicated by a double newline - `\n\n`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     5
 *
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     6
 * For example, a nullary command like PING will be actually sent as `PING\n\n`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     7
 * A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`.
6a818f9192f4 Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13423
diff changeset
     8
 */
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
     9
use nom::*;
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    10
use std::{
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    11
    num::ParseIntError,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    12
    ops::Range,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    13
    str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    14
    str::{FromStr, Utf8Error},
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    15
};
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
    16
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    17
use super::messages::{HWProtocolMessage, HWProtocolMessage::*};
14804
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
    18
use crate::server::coretypes::{
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
    19
    GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType, MAX_HEDGEHOGS_PER_TEAM,
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
    20
};
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    21
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    22
#[derive(Debug, PartialEq)]
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    23
pub struct HWProtocolError {}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    24
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    25
impl HWProtocolError {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    26
    fn new() -> Self {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    27
        HWProtocolError {}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    28
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    29
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    30
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    31
impl<I> ParseError<I> for HWProtocolError {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    32
    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    33
        HWProtocolError::new()
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    34
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    35
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    36
    fn append(input: I, kind: ErrorKind, other: Self) -> Self {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    37
        HWProtocolError::new()
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    38
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    39
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    40
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    41
impl From<Utf8Error> for HWProtocolError {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    42
    fn from(_: Utf8Error) -> Self {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    43
        HWProtocolError::new()
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    44
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    45
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    46
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    47
impl From<ParseIntError> for HWProtocolError {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    48
    fn from(_: ParseIntError) -> Self {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    49
        HWProtocolError::new()
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    50
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    51
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    52
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    53
pub type HWResult<'a, O> = IResult<&'a [u8], O, HWProtocolError>;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    54
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    55
fn end_of_message(input: &[u8]) -> HWResult<&[u8]> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    56
    tag("\n\n")(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    57
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    58
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    59
fn convert_utf8(input: &[u8]) -> HWResult<&str> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    60
    match str::from_utf8(input) {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    61
        Ok(str) => Ok((b"", str)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    62
        Err(utf_err) => Result::Err(Err::Failure(utf_err.into())),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    63
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    64
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    65
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    66
fn convert_from_str<T>(str: &str) -> HWResult<T>
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    67
where
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    68
    T: FromStr<Err = ParseIntError>,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    69
{
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    70
    match T::from_str(str) {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    71
        Ok(x) => Ok((b"", x)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    72
        Err(format_err) => Result::Err(Err::Failure(format_err.into())),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    73
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    74
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    75
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    76
fn str_line(input: &[u8]) -> HWResult<&str> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    77
    let (i, text) = not_line_ending(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    78
    Ok((i, convert_utf8(text)?.1))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    79
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    80
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    81
fn a_line(input: &[u8]) -> HWResult<String> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    82
    let (i, str) = str_line(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    83
    Ok((i, str.to_string()))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    84
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    85
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    86
fn hw_tag<'a>(tag_str: &'a str) -> impl Fn(&'a [u8]) -> HWResult<'a, ()> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    87
    move |i| tag(tag_str)(i).map(|(i, _)| (i, ()))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    88
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    89
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    90
fn hw_tag_no_case<'a>(tag_str: &'a str) -> impl Fn(&'a [u8]) -> HWResult<'a, ()> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    91
    move |i| tag_no_case(tag_str)(i).map(|(i, _)| (i, ()))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    92
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    93
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    94
fn cmd_arg(input: &[u8]) -> HWResult<String> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    95
    let delimiters = b" \n";
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    96
    let (i, str) = take_while(move |c| !delimiters.contains(&c))(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    97
    Ok((i, convert_utf8(str)?.1.to_string()))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    98
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
    99
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   100
fn u8_line(input: &[u8]) -> HWResult<u8> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   101
    let (i, str) = str_line(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   102
    Ok((i, convert_from_str(str)?.1))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   103
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   104
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   105
fn u16_line(input: &[u8]) -> HWResult<u16> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   106
    let (i, str) = str_line(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   107
    Ok((i, convert_from_str(str)?.1))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   108
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   109
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   110
fn u32_line(input: &[u8]) -> HWResult<u32> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   111
    let (i, str) = str_line(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   112
    Ok((i, convert_from_str(str)?.1))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   113
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   114
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   115
fn yes_no_line(input: &[u8]) -> HWResult<bool> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   116
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   117
        |i| tag_no_case(b"YES")(i).map(|(i, _)| (i, true)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   118
        |i| tag_no_case(b"NO")(i).map(|(i, _)| (i, false)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   119
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   120
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   121
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   122
fn opt_arg<'a>(input: &'a [u8]) -> HWResult<'a, Option<String>> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   123
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   124
        |i: &'a [u8]| peek!(i, end_of_message).map(|(i, _)| (i, None)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   125
        |i| precededc(i, hw_tag("\n"), a_line).map(|(i, v)| (i, Some(v))),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   126
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   127
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   128
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   129
fn spaces(input: &[u8]) -> HWResult<&[u8]> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   130
    precededc(input, hw_tag(" "), |i| take_while(|c| c == b' ')(i))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   131
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   132
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   133
fn opt_space_arg<'a>(input: &'a [u8]) -> HWResult<'a, Option<String>> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   134
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   135
        |i: &'a [u8]| peek!(i, end_of_message).map(|(i, _)| (i, None)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   136
        |i| precededc(i, spaces, a_line).map(|(i, v)| (i, Some(v))),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   137
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   138
}
12134
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12133
diff changeset
   139
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   140
fn hedgehog_array(input: &[u8]) -> HWResult<[HedgehogInfo; 8]> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   141
    fn hedgehog_line(input: &[u8]) -> HWResult<HedgehogInfo> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   142
        let (i, name) = terminatedc(input, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   143
        let (i, hat) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   144
        Ok((i, HedgehogInfo { name, hat }))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   145
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   146
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   147
    let (i, h1) = terminatedc(input, hedgehog_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   148
    let (i, h2) = terminatedc(i, hedgehog_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   149
    let (i, h3) = terminatedc(i, hedgehog_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   150
    let (i, h4) = terminatedc(i, hedgehog_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   151
    let (i, h5) = terminatedc(i, hedgehog_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   152
    let (i, h6) = terminatedc(i, hedgehog_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   153
    let (i, h7) = terminatedc(i, hedgehog_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   154
    let (i, h8) = hedgehog_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   155
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   156
    Ok((i, [h1, h2, h3, h4, h5, h6, h7, h8]))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   157
}
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   158
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   159
fn voting(input: &[u8]) -> HWResult<VoteType> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   160
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   161
        |i| tag_no_case("PAUSE")(i).map(|(i, _)| (i, VoteType::Pause)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   162
        |i| tag_no_case("NEWSEED")(i).map(|(i, _)| (i, VoteType::NewSeed)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   163
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   164
            precededc(i, |i| precededc(i, hw_tag_no_case("KICK"), spaces), a_line)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   165
                .map(|(i, s)| (i, VoteType::Kick(s)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   166
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   167
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   168
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   169
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   170
                |i| precededc(i, hw_tag_no_case("HEDGEHOGS"), spaces),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   171
                u8_line,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   172
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   173
            .map(|(i, n)| (i, VoteType::HedgehogsPerTeam(n)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   174
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   175
        |i| precededc(i, hw_tag_no_case("MAP"), opt_space_arg).map(|(i, v)| (i, VoteType::Map(v))),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   176
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   177
}
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   178
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   179
fn no_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   180
    fn messagec<'a>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   181
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   182
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   183
        msg: HWProtocolMessage,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   184
    ) -> HWResult<'a, HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   185
        tag(name)(input).map(|(i, _)| (i, msg.clone()))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   186
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   187
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   188
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   189
        |i| messagec(i, "PING", Ping),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   190
        |i| messagec(i, "PONG", Pong),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   191
        |i| messagec(i, "LIST", List),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   192
        |i| messagec(i, "BANLIST", BanList),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   193
        |i| messagec(i, "GET_SERVER_VAR", GetServerVar),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   194
        |i| messagec(i, "TOGGLE_READY", ToggleReady),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   195
        |i| messagec(i, "START_GAME", StartGame),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   196
        |i| messagec(i, "TOGGLE_RESTRICT_JOINS", ToggleRestrictJoin),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   197
        |i| messagec(i, "TOGGLE_RESTRICT_TEAMS", ToggleRestrictTeams),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   198
        |i| messagec(i, "TOGGLE_REGISTERED_ONLY", ToggleRegisteredOnly),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   199
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   200
}
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   201
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   202
fn single_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   203
    fn messagec<'a, T, F, G>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   204
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   205
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   206
        parser: F,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   207
        constructor: G,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   208
    ) -> HWResult<'a, HWProtocolMessage>
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   209
    where
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   210
        F: Fn(&[u8]) -> HWResult<T>,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   211
        G: Fn(T) -> HWProtocolMessage,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   212
    {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   213
        precededc(input, hw_tag(name), parser).map(|(i, v)| (i, constructor(v)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   214
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   215
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   216
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   217
        |i| messagec(i, "NICK\n", a_line, Nick),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   218
        |i| messagec(i, "INFO\n", a_line, Info),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   219
        |i| messagec(i, "CHAT\n", a_line, Chat),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   220
        |i| messagec(i, "PART", opt_arg, Part),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   221
        |i| messagec(i, "FOLLOW\n", a_line, Follow),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   222
        |i| messagec(i, "KICK\n", a_line, Kick),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   223
        |i| messagec(i, "UNBAN\n", a_line, Unban),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   224
        |i| messagec(i, "EM\n", a_line, EngineMessage),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   225
        |i| messagec(i, "TEAMCHAT\n", a_line, TeamChat),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   226
        |i| messagec(i, "ROOM_NAME\n", a_line, RoomName),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   227
        |i| messagec(i, "REMOVE_TEAM\n", a_line, RemoveTeam),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   228
        |i| messagec(i, "ROUNDFINISHED", opt_arg, |_| RoundFinished),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   229
        |i| messagec(i, "PROTO\n", u16_line, Proto),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   230
        |i| messagec(i, "QUIT", opt_arg, Quit),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   231
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   232
}
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   233
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   234
fn cmd_message<'a>(input: &'a [u8]) -> HWResult<'a, HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   235
    fn cmdc_no_arg<'a>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   236
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   237
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   238
        msg: HWProtocolMessage,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   239
    ) -> HWResult<'a, HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   240
        tag_no_case(name)(input).map(|(i, _)| (i, msg.clone()))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   241
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   242
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   243
    fn cmdc_single_arg<'a, T, F, G>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   244
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   245
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   246
        parser: F,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   247
        constructor: G,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   248
    ) -> HWResult<'a, HWProtocolMessage>
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   249
    where
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   250
        F: Fn(&'a [u8]) -> HWResult<'a, T>,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   251
        G: Fn(T) -> HWProtocolMessage,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   252
    {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   253
        precededc(input, |i| pairc(i, hw_tag_no_case(name), spaces), parser)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   254
            .map(|(i, v)| (i, constructor(v)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   255
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   256
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   257
    fn cmd_no_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   258
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   259
            |i| cmdc_no_arg(i, "STATS", Stats),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   260
            |i| cmdc_no_arg(i, "FIX", Fix),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   261
            |i| cmdc_no_arg(i, "UNFIX", Unfix),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   262
            |i| cmdc_no_arg(i, "REGISTERED_ONLY", ToggleServerRegisteredOnly),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   263
            |i| cmdc_no_arg(i, "SUPER_POWER", SuperPower),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   264
        ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   265
    }
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   266
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   267
    fn cmd_single_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   268
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   269
            |i| cmdc_single_arg(i, "RESTART_SERVER", |i| tag("YES")(i), |_| RestartServer),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   270
            |i| cmdc_single_arg(i, "DELEGATE", a_line, Delegate),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   271
            |i| cmdc_single_arg(i, "DELETE", a_line, Delete),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   272
            |i| cmdc_single_arg(i, "SAVEROOM", a_line, SaveRoom),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   273
            |i| cmdc_single_arg(i, "LOADROOM", a_line, LoadRoom),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   274
            |i| cmdc_single_arg(i, "GLOBAL", a_line, Global),
14806
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14804
diff changeset
   275
            |i| cmdc_single_arg(i, "WATCH", u32_line, Watch),
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   276
            |i| cmdc_single_arg(i, "GREETING", a_line, Greeting),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   277
            |i| cmdc_single_arg(i, "VOTE", yes_no_line, Vote),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   278
            |i| cmdc_single_arg(i, "FORCE", yes_no_line, ForceVote),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   279
            |i| cmdc_single_arg(i, "INFO", a_line, Info),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   280
            |i| cmdc_single_arg(i, "MAXTEAMS", u8_line, MaxTeams),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   281
            |i| cmdc_single_arg(i, "CALLVOTE", |i| opt!(i, voting), CallVote),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   282
        ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   283
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   284
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   285
    precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   286
        input,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   287
        hw_tag("CMD\n"),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   288
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   289
            cmd_no_arg_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   290
            cmd_single_arg_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   291
            |i| precededc(i, hw_tag_no_case("PART"), opt_space_arg).map(|(i, s)| (i, Part(s))),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   292
            |i| precededc(i, hw_tag_no_case("QUIT"), opt_space_arg).map(|(i, s)| (i, Quit(s))),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   293
            |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   294
                precededc(i, hw_tag_no_case("SAVE"), |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   295
                    pairc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   296
                        i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   297
                        |i| precededc(i, spaces, cmd_arg),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   298
                        |i| precededc(i, spaces, cmd_arg),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   299
                    )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   300
                })
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   301
                .map(|(i, (n, l))| (i, Save(n, l)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   302
            },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   303
            |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   304
                let (i, _) = tag_no_case("RND")(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   305
                let (i, _) = alt((spaces, |i: &'a [u8]| peek!(i, end_of_message)))(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   306
                let (i, v) = str_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   307
                Ok((i, Rnd(v.split_whitespace().map(String::from).collect())))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   308
            },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   309
        )),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   310
    )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   311
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   312
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   313
fn config_message<'a>(input: &'a [u8]) -> HWResult<'a, HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   314
    fn cfgc_single_arg<'a, T, F, G>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   315
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   316
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   317
        parser: F,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   318
        constructor: G,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   319
    ) -> HWResult<'a, GameCfg>
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   320
    where
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   321
        F: Fn(&[u8]) -> HWResult<T>,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   322
        G: Fn(T) -> GameCfg,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   323
    {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   324
        precededc(input, |i| terminatedc(i, hw_tag(name), eol), parser)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   325
            .map(|(i, v)| (i, constructor(v)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   326
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   327
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   328
    let (i, cfg) = precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   329
        input,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   330
        hw_tag("CFG\n"),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   331
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   332
            |i| cfgc_single_arg(i, "THEME", a_line, GameCfg::Theme),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   333
            |i| cfgc_single_arg(i, "SCRIPT", a_line, GameCfg::Script),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   334
            |i| cfgc_single_arg(i, "MAP", a_line, GameCfg::MapType),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   335
            |i| cfgc_single_arg(i, "MAPGEN", u32_line, GameCfg::MapGenerator),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   336
            |i| cfgc_single_arg(i, "MAZE_SIZE", u32_line, GameCfg::MazeSize),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   337
            |i| cfgc_single_arg(i, "TEMPLATE", u32_line, GameCfg::Template),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   338
            |i| cfgc_single_arg(i, "FEATURE_SIZE", u32_line, GameCfg::FeatureSize),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   339
            |i| cfgc_single_arg(i, "SEED", a_line, GameCfg::Seed),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   340
            |i| cfgc_single_arg(i, "DRAWNMAP", a_line, GameCfg::DrawnMap),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   341
            |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   342
                precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   343
                    i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   344
                    |i| terminatedc(i, hw_tag("AMMO"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   345
                    |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   346
                        let (i, name) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   347
                        let (i, value) = opt_arg(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   348
                        Ok((i, GameCfg::Ammo(name, value)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   349
                    },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   350
                )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   351
            },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   352
            |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   353
                precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   354
                    i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   355
                    |i| terminatedc(i, hw_tag("SCHEME"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   356
                    |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   357
                        let (i, name) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   358
                        let (i, values) = alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   359
                            |i: &'a [u8]| peek!(i, end_of_message).map(|(i, _)| (i, None)),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   360
                            |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   361
                                precededc(i, eol, |i| separated_list(eol, a_line)(i))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   362
                                    .map(|(i, v)| (i, Some(v)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   363
                            },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   364
                        ))(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   365
                        Ok((i, GameCfg::Scheme(name, values.unwrap_or_default())))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   366
                    },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   367
                )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   368
            },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   369
        )),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   370
    )?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   371
    Ok((i, Cfg(cfg)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   372
}
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   373
14804
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   374
fn server_var_message(input: &[u8]) -> HWResult<HWProtocolMessage> {
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   375
    precededc(
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   376
        input,
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   377
        hw_tag("SET_SERVER_VAR\n"),
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   378
        alt((
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   379
            |i| {
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   380
                precededc(i, hw_tag("MOTD_NEW\n"), a_line)
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   381
                    .map(|(i, s)| (i, SetServerVar(ServerVar::MOTDNew(s))))
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   382
            },
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   383
            |i| {
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   384
                precededc(i, hw_tag("MOTD_OLD\n"), a_line)
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   385
                    .map(|(i, s)| (i, SetServerVar(ServerVar::MOTDOld(s))))
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   386
            },
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   387
            |i| {
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   388
                precededc(i, hw_tag("LATEST_PROTO\n"), u16_line)
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   389
                    .map(|(i, n)| (i, SetServerVar(ServerVar::LatestProto(n))))
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   390
            },
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   391
        )),
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   392
    )
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   393
}
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   394
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   395
fn complex_message(input: &[u8]) -> HWResult<HWProtocolMessage> {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   396
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   397
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   398
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   399
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   400
                |i| terminatedc(i, hw_tag("PASSWORD"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   401
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   402
                    let (i, pass) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   403
                    let (i, salt) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   404
                    Ok((i, Password(pass, salt)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   405
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   406
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   407
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   408
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   409
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   410
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   411
                |i| terminatedc(i, hw_tag("CHECKER"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   412
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   413
                    let (i, protocol) = terminatedc(i, u16_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   414
                    let (i, name) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   415
                    let (i, pass) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   416
                    Ok((i, Checker(protocol, name, pass)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   417
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   418
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   419
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   420
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   421
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   422
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   423
                |i| terminatedc(i, hw_tag("CREATE_ROOM"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   424
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   425
                    let (i, name) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   426
                    let (i, pass) = opt_arg(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   427
                    Ok((i, CreateRoom(name, pass)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   428
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   429
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   430
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   431
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   432
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   433
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   434
                |i| terminatedc(i, hw_tag("JOIN_ROOM"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   435
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   436
                    let (i, name) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   437
                    let (i, pass) = opt_arg(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   438
                    Ok((i, JoinRoom(name, pass)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   439
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   440
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   441
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   442
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   443
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   444
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   445
                |i| terminatedc(i, hw_tag("ADD_TEAM"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   446
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   447
                    let (i, name) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   448
                    let (i, color) = terminatedc(i, u8_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   449
                    let (i, grave) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   450
                    let (i, fort) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   451
                    let (i, voice_pack) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   452
                    let (i, flag) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   453
                    let (i, difficulty) = terminatedc(i, u8_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   454
                    let (i, hedgehogs) = hedgehog_array(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   455
                    Ok((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   456
                        i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   457
                        AddTeam(Box::new(TeamInfo {
14806
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14804
diff changeset
   458
                            owner: String::new(),
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   459
                            name,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   460
                            color,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   461
                            grave,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   462
                            fort,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   463
                            voice_pack,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   464
                            flag,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   465
                            difficulty,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   466
                            hedgehogs,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   467
                            hedgehogs_number: 0,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   468
                        })),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   469
                    ))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   470
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   471
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   472
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   473
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   474
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   475
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   476
                |i| terminatedc(i, hw_tag("HH_NUM"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   477
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   478
                    let (i, name) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   479
                    let (i, count) = u8_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   480
                    Ok((i, SetHedgehogsNumber(name, count)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   481
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   482
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   483
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   484
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   485
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   486
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   487
                |i| terminatedc(i, hw_tag("TEAM_COLOR"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   488
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   489
                    let (i, name) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   490
                    let (i, color) = u8_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   491
                    Ok((i, SetTeamColor(name, color)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   492
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   493
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   494
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   495
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   496
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   497
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   498
                |i| terminatedc(i, hw_tag("BAN"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   499
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   500
                    let (i, n) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   501
                    let (i, r) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   502
                    let (i, t) = u32_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   503
                    Ok((i, Ban(n, r, t)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   504
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   505
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   506
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   507
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   508
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   509
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   510
                |i| terminatedc(i, hw_tag("BAN_IP"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   511
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   512
                    let (i, n) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   513
                    let (i, r) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   514
                    let (i, t) = u32_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   515
                    Ok((i, BanIP(n, r, t)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   516
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   517
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   518
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   519
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   520
            precededc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   521
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   522
                |i| terminatedc(i, hw_tag("BAN_NICK"), eol),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   523
                |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   524
                    let (i, n) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   525
                    let (i, r) = terminatedc(i, a_line, eol)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   526
                    let (i, t) = u32_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   527
                    Ok((i, BanNick(n, r, t)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   528
                },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   529
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   530
        },
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   531
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   532
}
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   533
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   534
pub fn malformed_message(input: &[u8]) -> HWResult<()> {
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   535
    let (i, _) = terminatedc(input, |i| take_until(&b"\n\n"[..])(i), end_of_message)?;
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   536
    Ok((i, ()))
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   537
}
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   538
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   539
pub fn message(input: &[u8]) -> HWResult<HWProtocolMessage> {
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   540
    precededc(
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   541
        input,
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   542
        |i| take_while(|c| c == b'\n')(i),
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   543
        |i| {
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   544
            terminatedc(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   545
                i,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   546
                alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   547
                    no_arg_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   548
                    single_arg_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   549
                    cmd_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   550
                    config_message,
14804
b3adc030104b implement server vars
alfadur
parents: 14798
diff changeset
   551
                    server_var_message,
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   552
                    complex_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   553
                )),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   554
                end_of_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   555
            )
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   556
        },
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   557
    )
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   558
}
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   559
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   560
fn extract_messages(input: &[u8]) -> HWResult<Vec<HWProtocolMessage>> {
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   561
    many0(message)(input)
14796
09d46ab83361 port server parser to nom 5
alfadur
parents: 14478
diff changeset
   562
}
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   563
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13709
diff changeset
   564
#[cfg(test)]
14798
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   565
mod test {
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   566
    use super::{extract_messages, message};
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   567
    use crate::protocol::parser::HWProtocolError;
14798
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   568
    use crate::protocol::{messages::HWProtocolMessage::*, test::gen_proto_msg};
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   569
    use proptest::{proptest, proptest_helper};
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   570
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   571
    #[cfg(test)]
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   572
    proptest! {
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   573
        #[test]
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   574
        fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   575
            println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   576
            assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   577
        }
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   578
    }
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   579
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   580
    #[test]
14798
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   581
    fn parse_test() {
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   582
        assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   583
        assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   584
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   585
            message(b"NICK\nit's me\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   586
            Ok((&b""[..], Nick("it's me".to_string())))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   587
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   588
        assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   589
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   590
            message(b"QUIT\nbye-bye\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   591
            Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   592
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   593
        assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   594
        assert_eq!(
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   595
            message(b"CMD\nwatch 49471\n\n"),
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   596
            Ok((&b""[..], Watch(49471)))
14798
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   597
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   598
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   599
            message(b"BAN\nme\nbad\n77\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   600
            Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   601
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   602
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   603
        assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   604
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   605
            message(b"CMD\nPART _msg_\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   606
            Ok((&b""[..], Part(Some("_msg_".to_string()))))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   607
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   608
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   609
        assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   610
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   611
            message(b"CMD\nRND A B\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   612
            Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   613
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   614
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   615
        assert_eq!(
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   616
            message(b"QUIT\n1\n2\n\n"),
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   617
            Err(nom::Err::Error(HWProtocolError::new()))
14798
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   618
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   619
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   620
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   621
            extract_messages(b"\n\n\n\nPING\n\n"),
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   622
            Ok((&b""[..], vec![Ping]))
14798
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   623
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   624
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   625
            extract_messages(b"\n\n\nPING\n\n"),
14816
add191d825f4 add parser error handling
alfadur
parents: 14806
diff changeset
   626
            Ok((&b""[..], vec![Ping]))
14798
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14796
diff changeset
   627
        );
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   628
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   629
}