rust/hedgewars-server/src/protocol/parser.rs
author alfadur
Tue, 04 Jun 2019 23:05:12 +0300
changeset 15118 0e59abde6766
parent 15116 cce6e707172f
child 15119 901751d3cd80
permissions -rw-r--r--
parser cleanup
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},
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
    13
    combinator::{map, peek},
15114
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,
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
    16
    sequence::{pair, preceded, terminated},
15114
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> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
    96
    map(str_line, String::from)(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
    97
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
    98
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    99
fn cmd_arg(input: &[u8]) -> HwResult<String> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   100
    let delimiters = b" \n";
15116
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   101
    let (i, str) = take_while(move |c| !delimiters.contains(&c))(input.clone())?;
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   102
    if i != input {
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   103
        Ok((i, convert_utf8(str)?.1.to_string()))
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   104
    } else {
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   105
        Err(Err::Error(HwProtocolError::new()))
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   106
    }
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   107
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   108
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   109
fn u8_line(input: &[u8]) -> HwResult<u8> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   110
    let (i, str) = str_line(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   111
    Ok((i, convert_from_str(str)?.1))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   112
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   113
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   114
fn u16_line(input: &[u8]) -> HwResult<u16> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   115
    let (i, str) = str_line(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   116
    Ok((i, convert_from_str(str)?.1))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   117
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   118
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   119
fn u32_line(input: &[u8]) -> HwResult<u32> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   120
    let (i, str) = str_line(input)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   121
    Ok((i, convert_from_str(str)?.1))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   122
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   123
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   124
fn yes_no_line(input: &[u8]) -> HwResult<bool> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   125
    alt((
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   126
        map(tag_no_case(b"YES"), |_| true),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   127
        map(tag_no_case(b"NO"), |_| false),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   128
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   129
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   130
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   131
fn opt_arg<'a>(input: &'a [u8]) -> HwResult<'a, Option<String>> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   132
    alt((
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   133
        map(peek(end_of_message), |_| None),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   134
        map(preceded(tag("\n"), a_line), Some),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   135
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   136
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   137
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   138
fn spaces(input: &[u8]) -> HwResult<&[u8]> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   139
    preceded(tag(" "), take_while(|c| c == b' '))(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   140
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   141
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   142
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
   143
    alt((
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   144
        map(peek(end_of_message), |_| None),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   145
        map(preceded(spaces, a_line), Some),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   146
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   147
}
12134
109e6765b1fc Optional parameters parsing function
unc0rr
parents: 12133
diff changeset
   148
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   149
fn hedgehog_array(input: &[u8]) -> HwResult<[HedgehogInfo; 8]> {
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   150
    fn hedgehog_line(input: &[u8]) -> HwResult<HedgehogInfo> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   151
        let (i, name) = terminated(a_line, newline)(input)?;
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   152
        let (i, hat) = a_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   153
        Ok((i, HedgehogInfo { name, hat }))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   154
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   155
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   156
    let (i, h1) = terminated(hedgehog_line, newline)(input)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   157
    let (i, h2) = terminated(hedgehog_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   158
    let (i, h3) = terminated(hedgehog_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   159
    let (i, h4) = terminated(hedgehog_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   160
    let (i, h5) = terminated(hedgehog_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   161
    let (i, h6) = terminated(hedgehog_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   162
    let (i, h7) = terminated(hedgehog_line, newline)(i)?;
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   163
    let (i, h8) = hedgehog_line(i)?;
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   164
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   165
    Ok((i, [h1, h2, h3, h4, h5, h6, h7, h8]))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   166
}
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   167
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   168
fn voting(input: &[u8]) -> HwResult<VoteType> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   169
    alt((
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   170
        map(tag_no_case("PAUSE"), |_| VoteType::Pause),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   171
        map(tag_no_case("NEWSEED"), |_| VoteType::NewSeed),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   172
        map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   173
            preceded(pair(tag_no_case("KICK"), spaces), a_line),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   174
            VoteType::Kick,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   175
        ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   176
        map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   177
            preceded(pair(tag_no_case("HEDGEHOGS"), spaces), u8_line),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   178
            VoteType::HedgehogsPerTeam,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   179
        ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   180
        map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   181
            preceded(tag_no_case("MAP"), opt_space_arg),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   182
            VoteType::Map,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   183
        ),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   184
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   185
}
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   186
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   187
fn no_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   188
    fn messagec<'a>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   189
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   190
        name: &'a str,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   191
        msg: HwProtocolMessage,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   192
    ) -> HwResult<'a, HwProtocolMessage> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   193
        map(tag(name), |_| msg.clone())(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   194
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   195
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   196
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   197
        |i| messagec(i, "PING", Ping),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   198
        |i| messagec(i, "PONG", Pong),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   199
        |i| messagec(i, "LIST", List),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   200
        |i| messagec(i, "BANLIST", BanList),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   201
        |i| messagec(i, "GET_SERVER_VAR", GetServerVar),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   202
        |i| messagec(i, "TOGGLE_READY", ToggleReady),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   203
        |i| messagec(i, "START_GAME", StartGame),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   204
        |i| messagec(i, "TOGGLE_RESTRICT_JOINS", ToggleRestrictJoin),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   205
        |i| messagec(i, "TOGGLE_RESTRICT_TEAMS", ToggleRestrictTeams),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   206
        |i| messagec(i, "TOGGLE_REGISTERED_ONLY", ToggleRegisteredOnly),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   207
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   208
}
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   209
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   210
fn single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   211
    fn messagec<'a, T, F, G>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   212
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   213
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   214
        parser: F,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   215
        constructor: G,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   216
    ) -> HwResult<'a, HwProtocolMessage>
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   217
    where
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   218
        F: Fn(&[u8]) -> HwResult<T>,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   219
        G: Fn(T) -> HwProtocolMessage,
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   220
    {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   221
        map(preceded(tag(name), parser), constructor)(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   222
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   223
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   224
    alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   225
        |i| messagec(i, "NICK\n", a_line, Nick),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   226
        |i| messagec(i, "INFO\n", a_line, Info),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   227
        |i| messagec(i, "CHAT\n", a_line, Chat),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   228
        |i| messagec(i, "PART", opt_arg, Part),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   229
        |i| messagec(i, "FOLLOW\n", a_line, Follow),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   230
        |i| messagec(i, "KICK\n", a_line, Kick),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   231
        |i| messagec(i, "UNBAN\n", a_line, Unban),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   232
        |i| messagec(i, "EM\n", a_line, EngineMessage),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   233
        |i| messagec(i, "TEAMCHAT\n", a_line, TeamChat),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   234
        |i| messagec(i, "ROOM_NAME\n", a_line, RoomName),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   235
        |i| messagec(i, "REMOVE_TEAM\n", a_line, RemoveTeam),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   236
        |i| messagec(i, "ROUNDFINISHED", opt_arg, |_| RoundFinished),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   237
        |i| messagec(i, "PROTO\n", u16_line, Proto),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   238
        |i| messagec(i, "QUIT", opt_arg, Quit),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   239
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   240
}
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   241
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   242
fn cmd_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   243
    fn cmdc_no_arg<'a>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   244
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   245
        name: &'a str,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   246
        msg: HwProtocolMessage,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   247
    ) -> HwResult<'a, HwProtocolMessage> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   248
        map(tag_no_case(name), |_| msg.clone())(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   249
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   250
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   251
    fn cmdc_single_arg<'a, T, F, G>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   252
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   253
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   254
        parser: F,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   255
        constructor: G,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   256
    ) -> HwResult<'a, HwProtocolMessage>
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   257
    where
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   258
        F: Fn(&'a [u8]) -> HwResult<'a, T>,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   259
        G: Fn(T) -> HwProtocolMessage,
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   260
    {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   261
        map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   262
            preceded(pair(tag_no_case(name), spaces), parser),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   263
            constructor,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   264
        )(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   265
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   266
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   267
    fn cmd_no_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   268
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   269
            |i| cmdc_no_arg(i, "STATS", Stats),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   270
            |i| cmdc_no_arg(i, "FIX", Fix),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   271
            |i| cmdc_no_arg(i, "UNFIX", Unfix),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   272
            |i| cmdc_no_arg(i, "REGISTERED_ONLY", ToggleServerRegisteredOnly),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   273
            |i| cmdc_no_arg(i, "SUPER_POWER", SuperPower),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   274
        ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   275
    }
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12134
diff changeset
   276
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   277
    fn cmd_single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   278
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   279
            |i| cmdc_single_arg(i, "RESTART_SERVER", |i| tag("YES")(i), |_| RestartServer),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   280
            |i| cmdc_single_arg(i, "DELEGATE", a_line, Delegate),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   281
            |i| cmdc_single_arg(i, "DELETE", a_line, Delete),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   282
            |i| cmdc_single_arg(i, "SAVEROOM", a_line, SaveRoom),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   283
            |i| cmdc_single_arg(i, "LOADROOM", a_line, LoadRoom),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   284
            |i| cmdc_single_arg(i, "GLOBAL", a_line, Global),
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14783
diff changeset
   285
            |i| cmdc_single_arg(i, "WATCH", u32_line, Watch),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   286
            |i| cmdc_single_arg(i, "VOTE", yes_no_line, Vote),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   287
            |i| cmdc_single_arg(i, "FORCE", yes_no_line, ForceVote),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   288
            |i| cmdc_single_arg(i, "INFO", a_line, Info),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   289
            |i| cmdc_single_arg(i, "MAXTEAMS", u8_line, MaxTeams),
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   290
            |i| cmdc_single_arg(i, "CALLVOTE", voting, |v| CallVote(Some(v))),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   291
        ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   292
    }
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   293
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   294
    preceded(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   295
        tag("CMD\n"),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   296
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   297
            cmd_no_arg_message,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   298
            cmd_single_arg_message,
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   299
            map(tag_no_case("CALLVOTE"), |_| CallVote(None)),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   300
            map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   301
                preceded(tag_no_case("GREETING"), opt_space_arg),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   302
                Greeting,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   303
            ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   304
            map(preceded(tag_no_case("PART"), opt_space_arg), Part),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   305
            map(preceded(tag_no_case("QUIT"), opt_space_arg), Quit),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   306
            map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   307
                preceded(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   308
                    tag_no_case("SAVE"),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   309
                    pair(preceded(spaces, cmd_arg), preceded(spaces, cmd_arg)),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   310
                ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   311
                |(n, l)| Save(n, l),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   312
            ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   313
            map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   314
                preceded(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   315
                    tag_no_case("RND"),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   316
                    alt((
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   317
                        map(peek(end_of_message), |_| vec![]),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   318
                        preceded(spaces, separated_list(spaces, cmd_arg)),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   319
                    )),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   320
                ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   321
                Rnd,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   322
            ),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   323
        )),
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   324
    )(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   325
}
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   326
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   327
fn config_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   328
    fn cfgc_single_arg<'a, T, F, G>(
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   329
        input: &'a [u8],
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   330
        name: &'a str,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   331
        parser: F,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   332
        constructor: G,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   333
    ) -> HwResult<'a, GameCfg>
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   334
    where
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   335
        F: Fn(&[u8]) -> HwResult<T>,
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   336
        G: Fn(T) -> GameCfg,
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   337
    {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   338
        map(preceded(pair(tag(name), newline), parser), constructor)(input)
14775
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
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   341
    let (i, cfg) = preceded(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   342
        tag("CFG\n"),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   343
        alt((
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   344
            |i| cfgc_single_arg(i, "THEME", a_line, GameCfg::Theme),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   345
            |i| cfgc_single_arg(i, "SCRIPT", a_line, GameCfg::Script),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   346
            |i| cfgc_single_arg(i, "MAP", a_line, GameCfg::MapType),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   347
            |i| cfgc_single_arg(i, "MAPGEN", u32_line, GameCfg::MapGenerator),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   348
            |i| cfgc_single_arg(i, "MAZE_SIZE", u32_line, GameCfg::MazeSize),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   349
            |i| cfgc_single_arg(i, "TEMPLATE", u32_line, GameCfg::Template),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   350
            |i| cfgc_single_arg(i, "FEATURE_SIZE", u32_line, GameCfg::FeatureSize),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   351
            |i| cfgc_single_arg(i, "SEED", a_line, GameCfg::Seed),
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   352
            |i| cfgc_single_arg(i, "DRAWNMAP", a_line, GameCfg::DrawnMap),
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   353
            preceded(pair(tag("AMMO"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   354
                let (i, name) = a_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   355
                let (i, value) = opt_arg(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   356
                Ok((i, GameCfg::Ammo(name, value)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   357
            }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   358
            preceded(pair(tag("SCHEME"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   359
                let (i, name) = a_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   360
                let (i, values) = alt((
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   361
                    map(peek(end_of_message), |_| None),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   362
                    map(preceded(newline, separated_list(newline, a_line)), Some),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   363
                ))(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   364
                Ok((i, GameCfg::Scheme(name, values.unwrap_or_default())))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   365
            }),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   366
        )),
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   367
    )(input)?;
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   368
    Ok((i, Cfg(cfg)))
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   369
}
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   370
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   371
fn server_var_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   372
    map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   373
        preceded(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   374
            tag("SET_SERVER_VAR\n"),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   375
            alt((
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   376
                map(preceded(tag("MOTD_NEW\n"), a_line), ServerVar::MOTDNew),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   377
                map(preceded(tag("MOTD_OLD\n"), a_line), ServerVar::MOTDOld),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   378
                map(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   379
                    preceded(tag("LATEST_PROTO\n"), u16_line),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   380
                    ServerVar::LatestProto,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   381
                ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   382
            )),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   383
        ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   384
        SetServerVar,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   385
    )(input)
14783
b3adc030104b implement server vars
alfadur
parents: 14777
diff changeset
   386
}
b3adc030104b implement server vars
alfadur
parents: 14777
diff changeset
   387
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   388
fn complex_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   389
    alt((
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   390
        preceded(pair(tag("PASSWORD"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   391
            let (i, pass) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   392
            let (i, salt) = a_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   393
            Ok((i, Password(pass, salt)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   394
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   395
        preceded(pair(tag("CHECKER"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   396
            let (i, protocol) = terminated(u16_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   397
            let (i, name) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   398
            let (i, pass) = a_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   399
            Ok((i, Checker(protocol, name, pass)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   400
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   401
        preceded(pair(tag("CREATE_ROOM"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   402
            let (i, name) = a_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   403
            let (i, pass) = opt_arg(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   404
            Ok((i, CreateRoom(name, pass)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   405
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   406
        preceded(pair(tag("JOIN_ROOM"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   407
            let (i, name) = a_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   408
            let (i, pass) = opt_arg(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   409
            Ok((i, JoinRoom(name, pass)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   410
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   411
        preceded(pair(tag("ADD_TEAM"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   412
            let (i, name) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   413
            let (i, color) = terminated(u8_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   414
            let (i, grave) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   415
            let (i, fort) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   416
            let (i, voice_pack) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   417
            let (i, flag) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   418
            let (i, difficulty) = terminated(u8_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   419
            let (i, hedgehogs) = hedgehog_array(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   420
            Ok((
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   421
                i,
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   422
                AddTeam(Box::new(TeamInfo {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   423
                    owner: String::new(),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   424
                    name,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   425
                    color,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   426
                    grave,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   427
                    fort,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   428
                    voice_pack,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   429
                    flag,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   430
                    difficulty,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   431
                    hedgehogs,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   432
                    hedgehogs_number: 0,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   433
                })),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   434
            ))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   435
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   436
        preceded(pair(tag("HH_NUM"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   437
            let (i, name) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   438
            let (i, count) = u8_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   439
            Ok((i, SetHedgehogsNumber(name, count)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   440
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   441
        preceded(pair(tag("TEAM_COLOR"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   442
            let (i, name) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   443
            let (i, color) = u8_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   444
            Ok((i, SetTeamColor(name, color)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   445
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   446
        preceded(pair(tag("BAN"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   447
            let (i, n) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   448
            let (i, r) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   449
            let (i, t) = u32_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   450
            Ok((i, Ban(n, r, t)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   451
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   452
        preceded(pair(tag("BAN_IP"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   453
            let (i, n) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   454
            let (i, r) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   455
            let (i, t) = u32_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   456
            Ok((i, BanIP(n, r, t)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   457
        }),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   458
        preceded(pair(tag("BAN_NICK"), newline), |i| {
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   459
            let (i, n) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   460
            let (i, r) = terminated(a_line, newline)(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   461
            let (i, t) = u32_line(i)?;
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   462
            Ok((i, BanNick(n, r, t)))
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   463
        }),
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   464
    ))(input)
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   465
}
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   466
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   467
pub fn malformed_message(input: &[u8]) -> HwResult<()> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   468
    map(terminated(take_until(&b"\n\n"[..]), end_of_message), |_| ())(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   469
}
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   470
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   471
pub fn message(input: &[u8]) -> HwResult<HwProtocolMessage> {
15118
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   472
    preceded(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   473
        take_while(|c| c == b'\n'),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   474
        terminated(
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   475
            alt((
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   476
                no_arg_message,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   477
                single_arg_message,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   478
                cmd_message,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   479
                config_message,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   480
                server_var_message,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   481
                complex_message,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   482
            )),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   483
            end_of_message,
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   484
        ),
0e59abde6766 parser cleanup
alfadur
parents: 15116
diff changeset
   485
    )(input)
14775
09d46ab83361 port server parser to nom 5
alfadur
parents: 14457
diff changeset
   486
}
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents:
diff changeset
   487
13796
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13795
diff changeset
   488
#[cfg(test)]
14777
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   489
mod test {
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   490
    use super::message;
15116
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   491
    use crate::{
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   492
        core::types::GameCfg,
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   493
        protocol::{messages::HwProtocolMessage::*, parser::HwProtocolError, test::gen_proto_msg},
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   494
    };
14777
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   495
    use proptest::{proptest, proptest_helper};
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   496
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   497
    #[cfg(test)]
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   498
    proptest! {
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   499
        #[test]
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   500
        fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   501
            println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   502
            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
   503
        }
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   504
    }
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   505
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   506
    #[test]
14777
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   507
    fn parse_test() {
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   508
        assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   509
        assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   510
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   511
            message(b"NICK\nit's me\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   512
            Ok((&b""[..], Nick("it's me".to_string())))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   513
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   514
        assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   515
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   516
            message(b"QUIT\nbye-bye\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   517
            Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   518
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   519
        assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   520
        assert_eq!(
14795
add191d825f4 add parser error handling
alfadur
parents: 14785
diff changeset
   521
            message(b"CMD\nwatch 49471\n\n"),
add191d825f4 add parser error handling
alfadur
parents: 14785
diff changeset
   522
            Ok((&b""[..], Watch(49471)))
14777
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   523
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   524
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   525
            message(b"BAN\nme\nbad\n77\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   526
            Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   527
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   528
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   529
        assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   530
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   531
            message(b"CMD\nPART _msg_\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   532
            Ok((&b""[..], Part(Some("_msg_".to_string()))))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   533
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   534
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   535
        assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   536
        assert_eq!(
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   537
            message(b"CMD\nRND A B\n\n"),
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   538
            Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   539
        );
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   540
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   541
        assert_eq!(
15116
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   542
            message(b"CFG\nSCHEME\na\nA\n\n"),
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   543
            Ok((
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   544
                &b""[..],
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   545
                Cfg(GameCfg::Scheme("a".to_string(), vec!["A".to_string()]))
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   546
            ))
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   547
        );
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   548
cce6e707172f fix lists parsing
alfadur
parents: 15114
diff changeset
   549
        assert_eq!(
14795
add191d825f4 add parser error handling
alfadur
parents: 14785
diff changeset
   550
            message(b"QUIT\n1\n2\n\n"),
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   551
            Err(nom::Err::Error(HwProtocolError::new()))
14777
8015a6e4ca3c move parser tests into test module
alfadur
parents: 14775
diff changeset
   552
        );
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   553
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12142
diff changeset
   554
}