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