2023-04-24 18:02:37 -04:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::character::complete::anychar;
|
|
|
|
use nom::combinator::peek;
|
2023-04-22 19:46:27 -04:00
|
|
|
use nom::combinator::recognize;
|
2023-04-24 18:02:37 -04:00
|
|
|
use nom::combinator::verify;
|
|
|
|
use nom::multi::many_till;
|
2023-04-22 19:46:27 -04:00
|
|
|
|
2023-03-25 12:53:57 -04:00
|
|
|
use super::object::PlainText;
|
|
|
|
use super::Context;
|
2023-04-21 18:36:01 -04:00
|
|
|
use crate::error::Res;
|
2023-04-22 19:46:27 -04:00
|
|
|
use crate::parser::object_parser::any_object_except_plain_text;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
2023-04-24 18:02:37 -04:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
2023-03-25 12:53:57 -04:00
|
|
|
|
2023-03-27 18:08:17 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-03-25 12:53:57 -04:00
|
|
|
pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainText<'s>> {
|
2023-04-24 18:02:37 -04:00
|
|
|
let (remaining, source) = recognize(verify(
|
|
|
|
many_till(
|
|
|
|
anychar,
|
|
|
|
peek(alt((
|
|
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
|
|
parser_with_context!(plain_text_end)(context),
|
|
|
|
))),
|
|
|
|
),
|
|
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
|
|
))(input)?;
|
|
|
|
|
2023-04-24 17:58:10 -04:00
|
|
|
Ok((remaining, PlainText { source }))
|
2023-03-25 12:53:57 -04:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:46:27 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn plain_text_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
recognize(parser_with_context!(any_object_except_plain_text)(context))(input)
|
|
|
|
}
|
|
|
|
|
2023-03-25 12:53:57 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use nom::combinator::map;
|
|
|
|
|
2023-04-22 19:46:27 -04:00
|
|
|
use super::*;
|
2023-03-25 12:53:57 -04:00
|
|
|
use crate::parser::object::Object;
|
|
|
|
use crate::parser::parser_context::ContextElement;
|
|
|
|
use crate::parser::parser_context::ContextTree;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
use crate::parser::source::Source;
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
}
|