use nom::branch::alt; use nom::bytes::complete::tag; use nom::character::complete::anychar; use nom::combinator::map; use nom::combinator::peek; use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many_till; use super::object_parser::any_object_except_plain_text; use super::org_source::OrgSource; use super::radio_link::RematchObject; use super::util::exit_matcher_parser; use crate::context::parser_with_context; use crate::context::RefContext; use crate::error::Res; use crate::types::Object; use crate::types::PlainText; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn plain_text<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, PlainText<'s>> { 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)?; Ok(( remaining, PlainText { source: source.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn plain_text_end<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { recognize(parser_with_context!(any_object_except_plain_text)(context))(input) } impl<'x> RematchObject<'x> for PlainText<'x> { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn rematch_object<'b, 'g, 'r, 's>( &'x self, _context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Object<'s>> { map(tag(self.source), |s| { Object::PlainText(PlainText { source: Into::<&str>::into(s), }) })(input) } } #[cfg(test)] mod tests { use nom::combinator::map; use super::*; use crate::context::Context; use crate::context::ContextElement; use crate::context::GlobalSettings; use crate::context::List; use crate::types::Source; #[test] fn plain_text_simple() { let input = OrgSource::new("foobarbaz"); let global_settings = GlobalSettings::default(); let initial_context = ContextElement::document_context(); let initial_context = Context::new(&global_settings, List::new(&initial_context)); let plain_text_matcher = parser_with_context!(plain_text)(&initial_context); let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap(); assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.get_source(), Into::<&str>::into(input)); } }