From 7314127763cc10aa49e12f30f4c50ed6ed85aace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 17 Jan 2023 10:30:28 +0100 Subject: [PATCH] Actions are parsed until the end of the line only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/lib.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fafea35..09e4fb3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,7 +138,10 @@ fn comment(s: Span) -> IResult { Ok((s, Token { position: pos })) } -fn take_until_tags, List>(list: List) -> impl Fn(I) -> IResult +fn take_until_tags, List>( + list: List, + endmark: T, +) -> impl Fn(I) -> IResult where I: InputTake + FindSubstring, T: InputLength + Clone, @@ -146,11 +149,19 @@ where { move |i: I| { let mut l = list.clone(); + let endmark = endmark.clone(); + let endindex = match i.find_substring(endmark) { + None | Some(0) => { + return Err(nom::Err::Error(E::from_error_kind(i, ErrorKind::TakeUntil))) + } + Some(index) => index, + }; let res: Option = loop { if let Some(item) = l.next() { match i.find_substring(item) { None | Some(0) => continue, - valid @ Some(_) => break valid, + Some(index) if index >= endindex => continue, + valid => break valid, } } else { break None; @@ -273,6 +284,7 @@ fn parse_action(s: Span) -> IResult { "receives:", ] .into_iter(), + "\n", )(s)?; let (s, (tagname, _, _)) = tuple(( alt(( @@ -575,6 +587,23 @@ mod tests { assert_eq!(parse_action(buf.into()).unwrap().1, receive); } + #[test] + fn test_actions_take_until() { + let buf = "Rosa receives:\n\t\n\n\n# Comment\n\nPeter sends:\n\t\n"; + let xml = b""; + let actions = vec![ + Action::Receive( + String::from("Rosa"), + Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(), + ), + Action::Send( + String::from("Peter"), + Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(), + ), + ]; + assert_eq!(parse_actions(buf.into()).unwrap().1, actions); + } + #[test] fn test_action_receive_none() { let buf = "rosa receives: nothing\n";