Simplify the implementation of plain text.

This commit is contained in:
Tom Alexander 2023-04-24 17:58:10 -04:00
parent 90a47b7b49
commit b2ee44ec09
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -1,4 +1,3 @@
use nom::combinator::not;
use nom::combinator::recognize;
use super::object::PlainText;
@ -11,6 +10,7 @@ use crate::parser::object_parser::any_object_except_plain_text;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ExitMatcherNode;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::util::text_until_exit;
#[tracing::instrument(ret, level = "debug")]
pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainText<'s>> {
@ -24,32 +24,8 @@ pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
class: ExitClass::Beta,
exit_matcher: &plain_text_end,
}));
let mut current_input = input.char_indices();
loop {
match current_input.next() {
Some((offset, _char)) => {
let remaining = &input[offset..];
let exit_matcher_status = not(|i| parser_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.
exit_matcher_status?;
} else {
return Ok((
&input[offset..],
PlainText {
source: &input[..offset],
},
));
}
}
}
None => {
// We hit the end of the file, so all input must be plain text
return Ok((&input[input.len()..], PlainText { source: input }));
}
};
}
let (remaining, source) = text_until_exit(&parser_context, input)?;
Ok((remaining, PlainText { source }))
}
#[tracing::instrument(ret, level = "debug")]