# HG changeset patch # User alfadur # Date 1707760881 -10800 # Node ID b26c3497ea854bac10a53992ddd2eb9c3c045674 # Parent cd8392e52165e498d5405de6397f6eed1857b9a9 fix protocol parser life assurance diff -r cd8392e52165 -r b26c3497ea85 rust/hedgewars-network-protocol/src/parser.rs --- a/rust/hedgewars-network-protocol/src/parser.rs Mon Feb 12 01:19:32 2024 +0300 +++ b/rust/hedgewars-network-protocol/src/parser.rs Mon Feb 12 21:01:21 2024 +0300 @@ -14,7 +14,7 @@ error::{ErrorKind, ParseError}, multi::separated_list0, sequence::{delimited, pair, preceded, terminated, tuple}, - Err, IResult, + Err, IResult, Parser }; use std::{ @@ -187,7 +187,7 @@ fn message( name: &str, msg: HwProtocolMessage, - ) -> impl Fn(&[u8]) -> HwResult { + ) -> impl Fn(&[u8]) -> HwResult + '_ { move |i| map(tag(name), |_| msg.clone())(i) } @@ -207,14 +207,14 @@ } fn single_arg_message(input: &[u8]) -> HwResult { - fn message( - name: &str, + fn message<'a, T: 'a, F, G>( + name: &'a str, parser: F, constructor: G, - ) -> impl FnMut(&[u8]) -> HwResult + ) -> impl FnMut(&'a [u8]) -> HwResult + '_ where - F: Fn(&[u8]) -> HwResult, - G: Fn(T) -> HwProtocolMessage, + F: Parser<&'a [u8], T, HwProtocolError> + 'a, + G: FnMut(T) -> HwProtocolMessage + 'a { map(preceded(tag(name), parser), constructor) } @@ -242,7 +242,7 @@ fn cmd_no_arg( name: &str, msg: HwProtocolMessage, - ) -> impl Fn(&[u8]) -> HwResult { + ) -> impl Fn(&[u8]) -> HwResult + '_ { move |i| map(tag_no_case(name), |_| msg.clone())(i) } @@ -319,14 +319,14 @@ } fn config_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> { - fn cfg_single_arg( - name: &str, + fn cfg_single_arg<'a, T: 'a, F, G>( + name: &'a str, parser: F, constructor: G, - ) -> impl FnMut(&[u8]) -> HwResult + ) -> impl FnMut(&'a [u8]) -> HwResult + '_ where - F: Fn(&[u8]) -> HwResult, - G: Fn(T) -> GameCfg, + F: Parser<&'a [u8], T, HwProtocolError> + 'a, + G: Fn(T) -> GameCfg + 'a, { map(preceded(pair(tag(name), newline), parser), constructor) } @@ -521,14 +521,14 @@ pub fn server_message(input: &[u8]) -> HwResult { use HwServerMessage::*; - fn single_arg_message( - name: &str, + fn single_arg_message<'a, T: 'a, F, G>( + name: &'a str, parser: F, constructor: G, - ) -> impl FnMut(&[u8]) -> HwResult + ) -> impl FnMut(&'a [u8]) -> HwResult + '_ where - F: Fn(&[u8]) -> HwResult, - G: Fn(T) -> HwServerMessage, + F: Parser<&'a [u8], T, HwProtocolError> + 'a, + G: Fn(T) -> HwServerMessage + 'a, { map( preceded(terminated(tag(name), newline), parser), @@ -536,12 +536,12 @@ ) } - fn list_message( - name: &str, + fn list_message<'a, G>( + name: &'a str, constructor: G, - ) -> impl FnMut(&[u8]) -> HwResult + ) -> impl FnMut(&'a [u8]) -> HwResult + '_ where - G: Fn(Vec) -> HwServerMessage, + G: Fn(Vec) -> HwServerMessage + 'a, { map( preceded( @@ -555,12 +555,12 @@ ) } - fn string_and_list_message( - name: &str, + fn string_and_list_message<'a, G>( + name: &'a str, constructor: G, - ) -> impl FnMut(&[u8]) -> HwResult + ) -> impl FnMut(&'a [u8]) -> HwResult + '_ where - G: Fn(String, Vec) -> HwServerMessage, + G: Fn(String, Vec) -> HwServerMessage + 'a, { preceded( pair(tag(name), newline), @@ -580,7 +580,7 @@ fn message( name: &str, msg: HwServerMessage, - ) -> impl Fn(&[u8]) -> HwResult { + ) -> impl Fn(&[u8]) -> HwResult + '_ { move |i| map(tag(name), |_| msg.clone())(i) }