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