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