fix protocol recovery
authoralfadur
Mon, 25 Mar 2024 00:04:13 +0300
changeset 15997 7c8697fa019f
parent 15996 19927b70588c
child 15998 14fe5bfe9862
fix protocol recovery
rust/hedgewars-network-protocol/src/parser.rs
rust/hedgewars-network-protocol/src/tests/parser.rs
--- a/rust/hedgewars-network-protocol/src/parser.rs	Sun Mar 24 14:34:27 2024 -0400
+++ b/rust/hedgewars-network-protocol/src/parser.rs	Mon Mar 25 00:04:13 2024 +0300
@@ -8,13 +8,13 @@
  */
 use nom::{
     branch::alt,
-    bytes::complete::{tag, tag_no_case, take_until, take_while},
-    character::complete::{newline, not_line_ending},
+    bytes::streaming::{tag, tag_no_case, take_until, take_while},
+    character::streaming::{newline, not_line_ending},
     combinator::{map, peek},
     error::{ErrorKind, ParseError},
     multi::separated_list0,
     sequence::{delimited, pair, preceded, terminated, tuple},
-    Err, IResult, Parser
+    Err, IResult, Parser,
 };
 
 use std::{
@@ -214,7 +214,7 @@
     ) -> impl FnMut(&'a [u8]) -> HwResult<HwProtocolMessage> + '_
     where
         F: Parser<&'a [u8], T, HwProtocolError> + 'a,
-        G: FnMut(T) -> HwProtocolMessage + 'a
+        G: FnMut(T) -> HwProtocolMessage + 'a,
     {
         map(preceded(tag(name), parser), constructor)
     }
--- a/rust/hedgewars-network-protocol/src/tests/parser.rs	Sun Mar 24 14:34:27 2024 -0400
+++ b/rust/hedgewars-network-protocol/src/tests/parser.rs	Mon Mar 25 00:04:13 2024 +0300
@@ -1,12 +1,22 @@
 use crate::{
     parser::HwProtocolError,
-    parser::{message, server_message},
+    parser::{malformed_message, message, server_message},
     types::GameCfg,
 };
 
 #[test]
 fn parse_test() {
     use crate::messages::HwProtocolMessage::*;
+    use nom::Err::Incomplete;
+
+    assert!(matches!(
+        dbg!(message(b"CHAT\nWhat the")),
+        Err(Incomplete(_))
+    ));
+    assert!(matches!(
+        dbg!(malformed_message(b"CHAT\nWhat the \xF0\x9F\xA6\x94\n\nBYE")),
+        Ok((b"BYE", _))
+    ));
 
     assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
     assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));