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";