rust/hedgewars-network-protocol/src/parser.rs
author unC0Rr
Mon, 02 Jan 2023 15:59:26 +0100
branchtransitional_engine
changeset 15929 128ace913837
parent 15852 ea459da15b30
child 16011 cf580d9ff7ef
permissions -rw-r--r--
Introduce hwengine-future library, use Land allocated in it
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     1
/** The parsers for the chat and multiplayer protocol. The main parser is `message`.
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     2
 * # Protocol
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     3
 * All messages consist of `\n`-separated strings. The end of a message is
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     4
 * indicated by a double newline - `\n\n`.
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     5
 *
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     6
 * For example, a nullary command like PING will be actually sent as `PING\n\n`.
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     7
 * A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`.
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     8
 */
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     9
use nom::{
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    10
    branch::alt,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    11
    bytes::complete::{tag, tag_no_case, take_until, take_while},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    12
    character::complete::{newline, not_line_ending},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    13
    combinator::{map, peek},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    14
    error::{ErrorKind, ParseError},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    15
    multi::separated_list0,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    16
    sequence::{delimited, pair, preceded, terminated, tuple},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    17
    Err, IResult,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    18
};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    19
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    20
use std::{
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    21
    num::ParseIntError,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    22
    str,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    23
    str::{FromStr, Utf8Error},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    24
};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    25
15852
ea459da15b30 update protocol 📦
alfadur
parents: 15833
diff changeset
    26
use crate::{
ea459da15b30 update protocol 📦
alfadur
parents: 15833
diff changeset
    27
    messages::{HwProtocolMessage, HwProtocolMessage::*, HwServerMessage},
ea459da15b30 update protocol 📦
alfadur
parents: 15833
diff changeset
    28
    types::{GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType},
ea459da15b30 update protocol 📦
alfadur
parents: 15833
diff changeset
    29
};
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    30
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    31
#[derive(Debug, PartialEq)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    32
pub struct HwProtocolError {}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    33
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    34
impl HwProtocolError {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    35
    pub fn new() -> Self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    36
        HwProtocolError {}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    37
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    38
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    39
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    40
impl<I> ParseError<I> for HwProtocolError {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    41
    fn from_error_kind(_input: I, _kind: ErrorKind) -> Self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    42
        HwProtocolError::new()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    43
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    44
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    45
    fn append(_input: I, _kind: ErrorKind, _other: Self) -> Self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    46
        HwProtocolError::new()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    47
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    48
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    49
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    50
impl From<Utf8Error> for HwProtocolError {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    51
    fn from(_: Utf8Error) -> Self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    52
        HwProtocolError::new()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    53
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    54
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    55
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    56
impl From<ParseIntError> for HwProtocolError {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    57
    fn from(_: ParseIntError) -> Self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    58
        HwProtocolError::new()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    59
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    60
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    61
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    62
pub type HwResult<'a, O> = IResult<&'a [u8], O, HwProtocolError>;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    63
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    64
fn end_of_message(input: &[u8]) -> HwResult<&[u8]> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    65
    tag("\n\n")(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    66
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    67
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    68
fn convert_utf8(input: &[u8]) -> HwResult<&str> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    69
    match str::from_utf8(input) {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    70
        Ok(str) => Ok((b"", str)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    71
        Err(utf_err) => Result::Err(Err::Failure(utf_err.into())),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    72
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    73
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    74
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    75
fn convert_from_str<T>(str: &str) -> HwResult<T>
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    76
where
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    77
    T: FromStr<Err = ParseIntError>,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    78
{
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    79
    match T::from_str(str) {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    80
        Ok(x) => Ok((b"", x)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    81
        Err(format_err) => Result::Err(Err::Failure(format_err.into())),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    82
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    83
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    84
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    85
fn str_line(input: &[u8]) -> HwResult<&str> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    86
    let (i, text) = not_line_ending(<&[u8]>::clone(&input))?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    87
    if i != input {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    88
        Ok((i, convert_utf8(text)?.1))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    89
    } else {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    90
        Err(Err::Error(HwProtocolError::new()))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    91
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    92
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    93
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    94
fn a_line(input: &[u8]) -> HwResult<String> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    95
    map(str_line, String::from)(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    96
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    97
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    98
fn cmd_arg(input: &[u8]) -> HwResult<String> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    99
    let delimiters = b" \n";
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   100
    let (i, str) = take_while(move |c| !delimiters.contains(&c))(<&[u8]>::clone(&input))?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   101
    if i != input {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   102
        Ok((i, convert_utf8(str)?.1.to_string()))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   103
    } else {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   104
        Err(Err::Error(HwProtocolError::new()))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   105
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   106
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   107
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   108
fn u8_line(input: &[u8]) -> HwResult<u8> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   109
    let (i, str) = str_line(input)?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   110
    Ok((i, convert_from_str(str)?.1))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   111
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   112
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   113
fn u16_line(input: &[u8]) -> HwResult<u16> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   114
    let (i, str) = str_line(input)?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   115
    Ok((i, convert_from_str(str)?.1))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   116
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   117
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   118
fn u32_line(input: &[u8]) -> HwResult<u32> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   119
    let (i, str) = str_line(input)?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   120
    Ok((i, convert_from_str(str)?.1))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   121
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   122
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   123
fn yes_no_line(input: &[u8]) -> HwResult<bool> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   124
    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   125
        map(tag_no_case(b"YES"), |_| true),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   126
        map(tag_no_case(b"NO"), |_| false),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   127
    ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   128
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   129
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   130
fn opt_arg<'a>(input: &'a [u8]) -> HwResult<'a, Option<String>> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   131
    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   132
        map(peek(end_of_message), |_| None),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   133
        map(preceded(tag("\n"), a_line), Some),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   134
    ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   135
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   136
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   137
fn spaces(input: &[u8]) -> HwResult<&[u8]> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   138
    preceded(tag(" "), take_while(|c| c == b' '))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   139
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   140
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   141
fn opt_space_arg<'a>(input: &'a [u8]) -> HwResult<'a, Option<String>> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   142
    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   143
        map(peek(end_of_message), |_| None),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   144
        map(preceded(spaces, a_line), Some),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   145
    ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   146
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   147
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   148
fn hedgehog_array(input: &[u8]) -> HwResult<[HedgehogInfo; 8]> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   149
    fn hedgehog_line(input: &[u8]) -> HwResult<HedgehogInfo> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   150
        map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   151
            tuple((terminated(a_line, newline), a_line)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   152
            |(name, hat)| HedgehogInfo { name, hat },
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   153
        )(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   154
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   155
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   156
    let (i, (h1, h2, h3, h4, h5, h6, h7, h8)) = tuple((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   157
        terminated(hedgehog_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   158
        terminated(hedgehog_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   159
        terminated(hedgehog_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   160
        terminated(hedgehog_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   161
        terminated(hedgehog_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   162
        terminated(hedgehog_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   163
        terminated(hedgehog_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   164
        hedgehog_line,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   165
    ))(input)?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   166
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   167
    Ok((i, [h1, h2, h3, h4, h5, h6, h7, h8]))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   168
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   169
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   170
fn voting(input: &[u8]) -> HwResult<VoteType> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   171
    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   172
        map(tag_no_case("PAUSE"), |_| VoteType::Pause),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   173
        map(tag_no_case("NEWSEED"), |_| VoteType::NewSeed),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   174
        map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   175
            preceded(pair(tag_no_case("KICK"), spaces), a_line),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   176
            VoteType::Kick,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   177
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   178
        map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   179
            preceded(pair(tag_no_case("HEDGEHOGS"), spaces), u8_line),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   180
            VoteType::HedgehogsPerTeam,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   181
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   182
        map(preceded(tag_no_case("MAP"), opt_space_arg), VoteType::Map),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   183
    ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   184
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   185
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   186
fn no_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   187
    fn message<'a>(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   188
        name: &'a str,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   189
        msg: HwProtocolMessage,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   190
    ) -> impl Fn(&'a [u8]) -> HwResult<'a, HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   191
        move |i| map(tag(name), |_| msg.clone())(i)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   192
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   193
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   194
    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   195
        message("PING", Ping),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   196
        message("PONG", Pong),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   197
        message("LIST", List),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   198
        message("BANLIST", BanList),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   199
        message("GET_SERVER_VAR", GetServerVar),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   200
        message("TOGGLE_READY", ToggleReady),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   201
        message("START_GAME", StartGame),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   202
        message("TOGGLE_RESTRICT_JOINS", ToggleRestrictJoin),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   203
        message("TOGGLE_RESTRICT_TEAMS", ToggleRestrictTeams),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   204
        message("TOGGLE_REGISTERED_ONLY", ToggleRegisteredOnly),
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   205
        message("READY", CheckerReady),
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   206
    ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   207
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   208
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   209
fn single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   210
    fn message<'a, T, F, G>(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   211
        name: &'a str,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   212
        parser: F,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   213
        constructor: G,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   214
    ) -> impl FnMut(&'a [u8]) -> HwResult<'a, HwProtocolMessage>
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   215
    where
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   216
        F: Fn(&[u8]) -> HwResult<T>,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   217
        G: Fn(T) -> HwProtocolMessage,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   218
    {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   219
        map(preceded(tag(name), parser), constructor)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   220
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   221
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   222
    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   223
        message("NICK\n", a_line, Nick),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   224
        message("INFO\n", a_line, Info),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   225
        message("CHAT\n", a_line, Chat),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   226
        message("PART", opt_arg, Part),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   227
        message("FOLLOW\n", a_line, Follow),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   228
        message("KICK\n", a_line, Kick),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   229
        message("UNBAN\n", a_line, Unban),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   230
        message("EM\n", a_line, EngineMessage),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   231
        message("TEAMCHAT\n", a_line, TeamChat),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   232
        message("ROOM_NAME\n", a_line, RoomName),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   233
        message("REMOVE_TEAM\n", a_line, RemoveTeam),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   234
        message("ROUNDFINISHED", opt_arg, |_| RoundFinished),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   235
        message("PROTO\n", u16_line, Proto),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   236
        message("QUIT", opt_arg, Quit),
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   237
        message("CHECKED\nFAIL\n", a_line, CheckedFail),
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   238
    ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   239
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   240
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   241
fn cmd_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   242
    fn cmd_no_arg<'a>(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   243
        name: &'a str,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   244
        msg: HwProtocolMessage,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   245
    ) -> impl Fn(&'a [u8]) -> HwResult<'a, HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   246
        move |i| map(tag_no_case(name), |_| msg.clone())(i)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   247
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   248
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   249
    fn cmd_single_arg<'a, T, F, G>(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   250
        name: &'a str,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   251
        parser: F,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   252
        constructor: G,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   253
    ) -> impl FnMut(&'a [u8]) -> HwResult<'a, HwProtocolMessage>
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   254
    where
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   255
        F: Fn(&'a [u8]) -> HwResult<'a, T>,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   256
        G: Fn(T) -> HwProtocolMessage,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   257
    {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   258
        map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   259
            preceded(pair(tag_no_case(name), spaces), parser),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   260
            constructor,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   261
        )
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   262
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   263
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   264
    fn cmd_no_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   265
        alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   266
            cmd_no_arg("STATS", Stats),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   267
            cmd_no_arg("FIX", Fix),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   268
            cmd_no_arg("UNFIX", Unfix),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   269
            cmd_no_arg("REGISTERED_ONLY", ToggleServerRegisteredOnly),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   270
            cmd_no_arg("SUPER_POWER", SuperPower),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   271
        ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   272
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   273
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   274
    fn cmd_single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   275
        alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   276
            cmd_single_arg("RESTART_SERVER", |i| tag("YES")(i), |_| RestartServer),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   277
            cmd_single_arg("DELEGATE", a_line, Delegate),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   278
            cmd_single_arg("DELETE", a_line, Delete),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   279
            cmd_single_arg("SAVEROOM", a_line, SaveRoom),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   280
            cmd_single_arg("LOADROOM", a_line, LoadRoom),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   281
            cmd_single_arg("GLOBAL", a_line, Global),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   282
            cmd_single_arg("WATCH", u32_line, Watch),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   283
            cmd_single_arg("VOTE", yes_no_line, Vote),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   284
            cmd_single_arg("FORCE", yes_no_line, ForceVote),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   285
            cmd_single_arg("INFO", a_line, Info),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   286
            cmd_single_arg("MAXTEAMS", u8_line, MaxTeams),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   287
            cmd_single_arg("CALLVOTE", voting, |v| CallVote(Some(v))),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   288
        ))(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   289
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   290
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   291
    preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   292
        tag("CMD\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   293
        alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   294
            cmd_no_arg_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   295
            cmd_single_arg_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   296
            map(tag_no_case("CALLVOTE"), |_| CallVote(None)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   297
            map(preceded(tag_no_case("GREETING"), opt_space_arg), Greeting),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   298
            map(preceded(tag_no_case("PART"), opt_space_arg), Part),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   299
            map(preceded(tag_no_case("QUIT"), opt_space_arg), Quit),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   300
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   301
                preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   302
                    tag_no_case("SAVE"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   303
                    pair(preceded(spaces, cmd_arg), preceded(spaces, cmd_arg)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   304
                ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   305
                |(n, l)| Save(n, l),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   306
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   307
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   308
                preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   309
                    tag_no_case("RND"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   310
                    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   311
                        map(peek(end_of_message), |_| vec![]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   312
                        preceded(spaces, separated_list0(spaces, cmd_arg)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   313
                    )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   314
                ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   315
                Rnd,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   316
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   317
        )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   318
    )(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   319
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   320
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   321
fn config_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   322
    fn cfg_single_arg<'a, T, F, G>(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   323
        name: &'a str,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   324
        parser: F,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   325
        constructor: G,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   326
    ) -> impl FnMut(&'a [u8]) -> HwResult<'a, GameCfg>
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   327
    where
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   328
        F: Fn(&[u8]) -> HwResult<T>,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   329
        G: Fn(T) -> GameCfg,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   330
    {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   331
        map(preceded(pair(tag(name), newline), parser), constructor)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   332
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   333
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   334
    let (i, cfg) = preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   335
        tag("CFG\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   336
        alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   337
            cfg_single_arg("THEME", a_line, GameCfg::Theme),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   338
            cfg_single_arg("SCRIPT", a_line, GameCfg::Script),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   339
            cfg_single_arg("MAP", a_line, GameCfg::MapType),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   340
            cfg_single_arg("MAPGEN", u32_line, GameCfg::MapGenerator),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   341
            cfg_single_arg("MAZE_SIZE", u32_line, GameCfg::MazeSize),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   342
            cfg_single_arg("TEMPLATE", u32_line, GameCfg::Template),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   343
            cfg_single_arg("FEATURE_SIZE", u32_line, GameCfg::FeatureSize),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   344
            cfg_single_arg("SEED", a_line, GameCfg::Seed),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   345
            cfg_single_arg("DRAWNMAP", a_line, GameCfg::DrawnMap),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   346
            preceded(pair(tag("AMMO"), newline), |i| {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   347
                let (i, name) = a_line(i)?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   348
                let (i, value) = opt_arg(i)?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   349
                Ok((i, GameCfg::Ammo(name, value)))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   350
            }),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   351
            preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   352
                pair(tag("SCHEME"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   353
                map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   354
                    pair(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   355
                        a_line,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   356
                        alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   357
                            map(peek(end_of_message), |_| None),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   358
                            map(preceded(newline, separated_list0(newline, a_line)), Some),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   359
                        )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   360
                    ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   361
                    |(name, values)| GameCfg::Scheme(name, values.unwrap_or_default()),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   362
                ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   363
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   364
        )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   365
    )(input)?;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   366
    Ok((i, Cfg(cfg)))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   367
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   368
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   369
fn server_var_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   370
    map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   371
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   372
            tag("SET_SERVER_VAR\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   373
            alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   374
                map(preceded(tag("MOTD_NEW\n"), a_line), ServerVar::MOTDNew),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   375
                map(preceded(tag("MOTD_OLD\n"), a_line), ServerVar::MOTDOld),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   376
                map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   377
                    preceded(tag("LATEST_PROTO\n"), u16_line),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   378
                    ServerVar::LatestProto,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   379
                ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   380
            )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   381
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   382
        SetServerVar,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   383
    )(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   384
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   385
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   386
fn complex_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   387
    alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   388
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   389
            pair(tag("PASSWORD"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   390
            map(pair(terminated(a_line, newline), a_line), |(pass, salt)| {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   391
                Password(pass, salt)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   392
            }),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   393
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   394
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   395
            pair(tag("CHECKER"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   396
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   397
                tuple((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   398
                    terminated(u16_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   399
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   400
                    a_line,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   401
                )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   402
                |(protocol, name, pass)| Checker(protocol, name, pass),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   403
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   404
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   405
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   406
            pair(tag("CREATE_ROOM"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   407
            map(pair(a_line, opt_arg), |(name, pass)| CreateRoom(name, pass)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   408
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   409
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   410
            pair(tag("JOIN_ROOM"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   411
            map(pair(a_line, opt_arg), |(name, pass)| JoinRoom(name, pass)),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   412
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   413
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   414
            pair(tag("ADD_TEAM"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   415
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   416
                tuple((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   417
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   418
                    terminated(u8_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   419
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   420
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   421
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   422
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   423
                    terminated(u8_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   424
                    hedgehog_array,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   425
                )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   426
                |(name, color, grave, fort, voice_pack, flag, difficulty, hedgehogs)| {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   427
                    AddTeam(Box::new(TeamInfo {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   428
                        owner: String::new(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   429
                        name,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   430
                        color,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   431
                        grave,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   432
                        fort,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   433
                        voice_pack,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   434
                        flag,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   435
                        difficulty,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   436
                        hedgehogs,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   437
                        hedgehogs_number: 0,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   438
                    }))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   439
                },
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   440
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   441
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   442
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   443
            pair(tag("HH_NUM"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   444
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   445
                pair(terminated(a_line, newline), u8_line),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   446
                |(name, count)| SetHedgehogsNumber(name, count),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   447
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   448
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   449
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   450
            pair(tag("TEAM_COLOR"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   451
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   452
                pair(terminated(a_line, newline), u8_line),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   453
                |(name, color)| SetTeamColor(name, color),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   454
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   455
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   456
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   457
            pair(tag("BAN"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   458
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   459
                tuple((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   460
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   461
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   462
                    u32_line,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   463
                )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   464
                |(name, reason, time)| Ban(name, reason, time),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   465
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   466
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   467
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   468
            pair(tag("BAN_IP"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   469
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   470
                tuple((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   471
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   472
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   473
                    u32_line,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   474
                )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   475
                |(ip, reason, time)| BanIp(ip, reason, time),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   476
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   477
        ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   478
        preceded(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   479
            pair(tag("BAN_NICK"), newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   480
            map(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   481
                tuple((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   482
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   483
                    terminated(a_line, newline),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   484
                    u32_line,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   485
                )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   486
                |(nick, reason, time)| BanNick(nick, reason, time),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   487
            ),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   488
        ),
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   489
        map(
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   490
            preceded(
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   491
                tag("CHECKED\nOK"),
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   492
                alt((
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   493
                    map(peek(end_of_message), |_| None),
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   494
                    map(preceded(newline, separated_list0(newline, a_line)), Some),
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   495
                )),
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   496
            ),
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   497
            |values| CheckedOk(values.unwrap_or_default()),
15852
ea459da15b30 update protocol 📦
alfadur
parents: 15833
diff changeset
   498
        ),
ea459da15b30 update protocol 📦
alfadur
parents: 15833
diff changeset
   499
    ))(input)
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   500
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   501
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   502
pub fn malformed_message(input: &[u8]) -> HwResult<()> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   503
    map(terminated(take_until(&b"\n\n"[..]), end_of_message), |_| ())(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   504
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   505
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   506
pub fn message(input: &[u8]) -> HwResult<HwProtocolMessage> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   507
    delimited(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   508
        take_while(|c| c == b'\n'),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   509
        alt((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   510
            no_arg_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   511
            single_arg_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   512
            cmd_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   513
            config_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   514
            server_var_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   515
            complex_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   516
        )),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   517
        end_of_message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   518
    )(input)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   519
}
15832
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   520
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   521
pub fn server_message(input: &[u8]) -> HwResult<HwServerMessage> {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   522
    use HwServerMessage::*;
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   523
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   524
    fn single_arg_message<'a, T, F, G>(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   525
        name: &'a str,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   526
        parser: F,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   527
        constructor: G,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   528
    ) -> impl FnMut(&'a [u8]) -> HwResult<'a, HwServerMessage>
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   529
    where
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   530
        F: Fn(&[u8]) -> HwResult<T>,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   531
        G: Fn(T) -> HwServerMessage,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   532
    {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   533
        map(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   534
            preceded(terminated(tag(name), newline), parser),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   535
            constructor,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   536
        )
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   537
    }
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   538
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   539
    fn list_message<'a, G>(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   540
        name: &'a str,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   541
        constructor: G,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   542
    ) -> impl FnMut(&'a [u8]) -> HwResult<'a, HwServerMessage>
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   543
    where
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   544
        G: Fn(Vec<String>) -> HwServerMessage,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   545
    {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   546
        map(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   547
            preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   548
                tag(name),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   549
                alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   550
                    map(peek(end_of_message), |_| None),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   551
                    map(preceded(newline, separated_list0(newline, a_line)), Some),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   552
                )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   553
            ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   554
            move |values| constructor(values.unwrap_or_default()),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   555
        )
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   556
    }
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   557
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   558
    fn string_and_list_message<'a, G>(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   559
        name: &'a str,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   560
        constructor: G,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   561
    ) -> impl FnMut(&'a [u8]) -> HwResult<'a, HwServerMessage>
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   562
    where
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   563
        G: Fn(String, Vec<String>) -> HwServerMessage,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   564
    {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   565
        preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   566
            pair(tag(name), newline),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   567
            map(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   568
                pair(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   569
                    a_line,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   570
                    alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   571
                        map(peek(end_of_message), |_| None),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   572
                        map(preceded(newline, separated_list0(newline, a_line)), Some),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   573
                    )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   574
                ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   575
                move |(name, values)| constructor(name, values.unwrap_or_default()),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   576
            ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   577
        )
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   578
    }
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   579
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   580
    fn message<'a>(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   581
        name: &'a str,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   582
        msg: HwServerMessage,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   583
    ) -> impl Fn(&'a [u8]) -> HwResult<'a, HwServerMessage> {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   584
        move |i| map(tag(name), |_| msg.clone())(i)
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   585
    }
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   586
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   587
    delimited(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   588
        take_while(|c| c == b'\n'),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   589
        alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   590
            alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   591
                message("PING", Ping),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   592
                message("PONG", Pong),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   593
                message("LOGONPASSED", LogonPassed),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   594
                message("KICKED", Kicked),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   595
                message("RUN_GAME", RunGame),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   596
                message("ROUND_FINISHED", RoundFinished),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   597
                message("REPLAY_START", ReplayStart),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   598
            )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   599
            alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   600
                single_arg_message("REDIRECT", u16_line, Redirect),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   601
                single_arg_message("BYE", a_line, Bye),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   602
                single_arg_message("NICK", a_line, Nick),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   603
                single_arg_message("PROTO", u16_line, Proto),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   604
                single_arg_message("ASKPASSWORD", a_line, AskPassword),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   605
                single_arg_message("SERVER_AUTH", a_line, ServerAuth),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   606
                single_arg_message("ROOM\nDEL", a_line, RoomRemove),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   607
                single_arg_message("JOINING", a_line, Joining),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   608
                single_arg_message("REMOVE_TEAM", a_line, TeamRemove),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   609
                single_arg_message("TEAM_ACCEPTED", a_line, TeamAccepted),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   610
                single_arg_message("SERVER_MESSAGE", a_line, ServerMessage),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   611
                single_arg_message("NOTICE", a_line, Notice),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   612
                single_arg_message("WARNING", a_line, Warning),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   613
                single_arg_message("ERROR", a_line, Error),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   614
            )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   615
            alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   616
                preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   617
                    pair(tag("LOBBY:LEFT"), newline),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   618
                    map(pair(terminated(a_line, newline), a_line), |(nick, msg)| {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   619
                        LobbyLeft(nick, msg)
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   620
                    }),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   621
                ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   622
                preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   623
                    pair(tag("CHAT"), newline),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   624
                    map(pair(terminated(a_line, newline), a_line), |(nick, msg)| {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   625
                        ChatMsg { nick, msg }
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   626
                    }),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   627
                ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   628
                preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   629
                    pair(tag("TEAM_COLOR"), newline),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   630
                    map(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   631
                        pair(terminated(a_line, newline), u8_line),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   632
                        |(name, color)| TeamColor(name, color),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   633
                    ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   634
                ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   635
                preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   636
                    pair(tag("HH_NUM"), newline),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   637
                    map(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   638
                        pair(terminated(a_line, newline), u8_line),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   639
                        |(name, count)| HedgehogsNumber(name, count),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   640
                    ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   641
                ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   642
                preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   643
                    pair(tag("CONNECTED"), newline),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   644
                    map(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   645
                        pair(terminated(a_line, newline), u32_line),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   646
                        |(msg, server_protocol_version)| Connected(msg, server_protocol_version),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   647
                    ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   648
                ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   649
                preceded(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   650
                    pair(tag("LEFT"), newline),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   651
                    map(pair(terminated(a_line, newline), a_line), |(nick, msg)| {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   652
                        RoomLeft(nick, msg)
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   653
                    }),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   654
                ),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   655
            )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   656
            alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   657
                string_and_list_message("CLIENT_FLAGS", ClientFlags),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   658
                string_and_list_message("ROOM\nUPD", RoomUpdated),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   659
                string_and_list_message("CFG", ConfigEntry),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   660
            )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   661
            alt((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   662
                list_message("LOBBY:JOINED", LobbyJoined),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   663
                list_message("ROOMS", Rooms),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   664
                list_message("ROOM\nADD", RoomAdd),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   665
                list_message("JOINED", RoomJoined),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   666
                list_message("ADD_TEAM", TeamAdd),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   667
                list_message("EM", ForwardEngineMessage),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   668
                list_message("INFO", Info),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   669
                list_message("SERVER_VARS", ServerVars),
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15832
diff changeset
   670
                list_message("REPLAY", Replay),
15832
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   671
            )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   672
        )),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   673
        end_of_message,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   674
    )(input)
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
   675
}