Fix plain text matcher exit conditions.

This commit is contained in:
Tom Alexander 2023-03-25 11:40:12 -04:00
parent 2ab407bb83
commit faf2bb401d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 21 additions and 1 deletions

View File

@ -64,7 +64,7 @@ fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
match current_input.next() {
Some((offset, _char)) => {
let remaining = &input[offset..];
let exit_matcher_status = context.check_exit_matcher(remaining);
let exit_matcher_status = not(|i| context.check_exit_matcher(i))(remaining);
if exit_matcher_status.is_err() {
if offset == 0 {
// If we're at the start of the input, then nothing is plain text, so fire an error for zero-length match.
@ -86,3 +86,23 @@ fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
};
}
}
#[cfg(test)]
mod tests {
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ContextTree;
use super::*;
#[test]
fn plain_text_simple() {
let input = "foobarbaz";
let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context =
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let plain_text_matcher = parser_with_context!(plain_text)(&document_context);
let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap();
assert_eq!(remaining, "");
assert_eq!(result.get_source(), input);
}
}