Actions are parsed until the end of the line only
Some checks are pending
ci/woodpecker/push/woodpecker Pipeline is pending

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-17 10:30:28 +01:00
parent 12299c5898
commit 7314127763
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -138,7 +138,10 @@ fn comment(s: Span) -> IResult<Span, Token> {
Ok((s, Token { position: pos })) Ok((s, Token { position: pos }))
} }
fn take_until_tags<T, I, E: ParseError<I>, List>(list: List) -> impl Fn(I) -> IResult<I, I, E> fn take_until_tags<T, I, E: ParseError<I>, List>(
list: List,
endmark: T,
) -> impl Fn(I) -> IResult<I, I, E>
where where
I: InputTake + FindSubstring<T>, I: InputTake + FindSubstring<T>,
T: InputLength + Clone, T: InputLength + Clone,
@ -146,11 +149,19 @@ where
{ {
move |i: I| { move |i: I| {
let mut l = list.clone(); 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<usize> = loop { let res: Option<usize> = loop {
if let Some(item) = l.next() { if let Some(item) = l.next() {
match i.find_substring(item) { match i.find_substring(item) {
None | Some(0) => continue, None | Some(0) => continue,
valid @ Some(_) => break valid, Some(index) if index >= endindex => continue,
valid => break valid,
} }
} else { } else {
break None; break None;
@ -273,6 +284,7 @@ fn parse_action(s: Span) -> IResult<Span, Action> {
"receives:", "receives:",
] ]
.into_iter(), .into_iter(),
"\n",
)(s)?; )(s)?;
let (s, (tagname, _, _)) = tuple(( let (s, (tagname, _, _)) = tuple((
alt(( alt((
@ -575,6 +587,23 @@ mod tests {
assert_eq!(parse_action(buf.into()).unwrap().1, receive); assert_eq!(parse_action(buf.into()).unwrap().1, receive);
} }
#[test]
fn test_actions_take_until() {
let buf = "Rosa receives:\n\t<presence/>\n\n\n# Comment\n\nPeter sends:\n\t<presence/>\n";
let xml = b"<presence/>";
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] #[test]
fn test_action_receive_none() { fn test_action_receive_none() {
let buf = "rosa receives: nothing\n"; let buf = "rosa receives: nothing\n";