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