rust/hedgewars-server/src/protocol/parser.rs
changeset 15801 f57a3d48072b
parent 15124 824472aa4d97
equal deleted inserted replaced
15800:6af892a0a4b8 15801:f57a3d48072b
    10     branch::alt,
    10     branch::alt,
    11     bytes::complete::{tag, tag_no_case, take_until, take_while},
    11     bytes::complete::{tag, tag_no_case, take_until, take_while},
    12     character::complete::{newline, not_line_ending},
    12     character::complete::{newline, not_line_ending},
    13     combinator::{map, peek},
    13     combinator::{map, peek},
    14     error::{ErrorKind, ParseError},
    14     error::{ErrorKind, ParseError},
    15     multi::separated_list,
    15     multi::separated_list0,
    16     sequence::{delimited, pair, preceded, terminated, tuple},
    16     sequence::{delimited, pair, preceded, terminated, tuple},
    17     Err, IResult,
    17     Err, IResult,
    18 };
    18 };
    19 
    19 
    20 use std::{
    20 use std::{
   209 fn single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
   209 fn single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> {
   210     fn message<'a, T, F, G>(
   210     fn message<'a, T, F, G>(
   211         name: &'a str,
   211         name: &'a str,
   212         parser: F,
   212         parser: F,
   213         constructor: G,
   213         constructor: G,
   214     ) -> impl Fn(&'a [u8]) -> HwResult<'a, HwProtocolMessage>
   214     ) -> impl FnMut(&'a [u8]) -> HwResult<'a, HwProtocolMessage>
   215     where
   215     where
   216         F: Fn(&[u8]) -> HwResult<T>,
   216         F: Fn(&[u8]) -> HwResult<T>,
   217         G: Fn(T) -> HwProtocolMessage,
   217         G: Fn(T) -> HwProtocolMessage,
   218     {
   218     {
   219         map(preceded(tag(name), parser), constructor)
   219         map(preceded(tag(name), parser), constructor)
   247 
   247 
   248     fn cmd_single_arg<'a, T, F, G>(
   248     fn cmd_single_arg<'a, T, F, G>(
   249         name: &'a str,
   249         name: &'a str,
   250         parser: F,
   250         parser: F,
   251         constructor: G,
   251         constructor: G,
   252     ) -> impl Fn(&'a [u8]) -> HwResult<'a, HwProtocolMessage>
   252     ) -> impl FnMut(&'a [u8]) -> HwResult<'a, HwProtocolMessage>
   253     where
   253     where
   254         F: Fn(&'a [u8]) -> HwResult<'a, T>,
   254         F: Fn(&'a [u8]) -> HwResult<'a, T>,
   255         G: Fn(T) -> HwProtocolMessage,
   255         G: Fn(T) -> HwProtocolMessage,
   256     {
   256     {
   257         map(
   257         map(
   306             map(
   306             map(
   307                 preceded(
   307                 preceded(
   308                     tag_no_case("RND"),
   308                     tag_no_case("RND"),
   309                     alt((
   309                     alt((
   310                         map(peek(end_of_message), |_| vec![]),
   310                         map(peek(end_of_message), |_| vec![]),
   311                         preceded(spaces, separated_list(spaces, cmd_arg)),
   311                         preceded(spaces, separated_list0(spaces, cmd_arg)),
   312                     )),
   312                     )),
   313                 ),
   313                 ),
   314                 Rnd,
   314                 Rnd,
   315             ),
   315             ),
   316         )),
   316         )),
   320 fn config_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> {
   320 fn config_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> {
   321     fn cfg_single_arg<'a, T, F, G>(
   321     fn cfg_single_arg<'a, T, F, G>(
   322         name: &'a str,
   322         name: &'a str,
   323         parser: F,
   323         parser: F,
   324         constructor: G,
   324         constructor: G,
   325     ) -> impl Fn(&'a [u8]) -> HwResult<'a, GameCfg>
   325     ) -> impl FnMut(&'a [u8]) -> HwResult<'a, GameCfg>
   326     where
   326     where
   327         F: Fn(&[u8]) -> HwResult<T>,
   327         F: Fn(&[u8]) -> HwResult<T>,
   328         G: Fn(T) -> GameCfg,
   328         G: Fn(T) -> GameCfg,
   329     {
   329     {
   330         map(preceded(pair(tag(name), newline), parser), constructor)
   330         map(preceded(pair(tag(name), newline), parser), constructor)
   352                 map(
   352                 map(
   353                     pair(
   353                     pair(
   354                         a_line,
   354                         a_line,
   355                         alt((
   355                         alt((
   356                             map(peek(end_of_message), |_| None),
   356                             map(peek(end_of_message), |_| None),
   357                             map(preceded(newline, separated_list(newline, a_line)), Some),
   357                             map(preceded(newline, separated_list0(newline, a_line)), Some),
   358                         )),
   358                         )),
   359                     ),
   359                     ),
   360                     |(name, values)| GameCfg::Scheme(name, values.unwrap_or_default()),
   360                     |(name, values)| GameCfg::Scheme(name, values.unwrap_or_default()),
   361                 ),
   361                 ),
   362             ),
   362             ),