2023-04-24 18:02:37 -04:00
|
|
|
use nom::branch::alt;
|
2023-04-24 20:32:59 -04:00
|
|
|
use nom::bytes::complete::tag;
|
2023-04-24 18:02:37 -04:00
|
|
|
use nom::character::complete::anychar;
|
2023-04-24 20:32:59 -04:00
|
|
|
use nom::combinator::map;
|
2023-04-24 18:02:37 -04:00
|
|
|
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-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-04-24 20:32:59 -04:00
|
|
|
use super::radio_link::RematchObject;
|
2023-09-03 12:23:18 -04:00
|
|
|
use super::util::exit_matcher_parser;
|
|
|
|
use crate::context::parser_with_context;
|
|
|
|
use crate::context::RefContext;
|
2023-04-21 18:36:01 -04:00
|
|
|
use crate::error::Res;
|
2023-09-03 12:23:18 -04:00
|
|
|
use crate::types::Object;
|
|
|
|
use crate::types::PlainText;
|
2023-03-25 12:53:57 -04:00
|
|
|
|
2023-09-11 13:13:28 -04:00
|
|
|
pub(crate) fn plain_text<F>(
|
2023-09-08 17:45:49 -04:00
|
|
|
end_condition: F,
|
|
|
|
) -> impl for<'b, 'g, 'r, 's> Fn(
|
|
|
|
RefContext<'b, 'g, 'r, 's>,
|
|
|
|
OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, PlainText<'s>>
|
|
|
|
where
|
|
|
|
F: for<'bb, 'gg, 'rr, 'ss> Fn(
|
|
|
|
RefContext<'bb, 'gg, 'rr, 'ss>,
|
|
|
|
OrgSource<'ss>,
|
|
|
|
) -> Res<OrgSource<'ss>, ()>,
|
|
|
|
{
|
|
|
|
move |context, input| _plain_text(&end_condition, context, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(end_condition))
|
|
|
|
)]
|
|
|
|
fn _plain_text<'b, 'g, 'r, 's, F>(
|
|
|
|
end_condition: F,
|
2023-09-03 15:44:18 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
2023-09-08 17:45:49 -04:00
|
|
|
) -> Res<OrgSource<'s>, PlainText<'s>>
|
|
|
|
where
|
|
|
|
F: for<'bb, 'gg, 'rr, 'ss> Fn(
|
|
|
|
RefContext<'bb, 'gg, 'rr, 'ss>,
|
|
|
|
OrgSource<'ss>,
|
|
|
|
) -> Res<OrgSource<'ss>, ()>,
|
|
|
|
{
|
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),
|
2023-09-08 17:45:49 -04:00
|
|
|
recognize(parser_with_context!(end_condition)(context)),
|
2023-04-24 18:02:37 -04:00
|
|
|
))),
|
|
|
|
),
|
|
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
|
|
))(input)?;
|
|
|
|
|
2023-08-23 00:30:26 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PlainText {
|
|
|
|
source: source.into(),
|
|
|
|
},
|
|
|
|
))
|
2023-03-25 12:53:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-14 18:04:01 -04:00
|
|
|
impl<'x> RematchObject<'x> for PlainText<'x> {
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-03 15:44:18 -04:00
|
|
|
fn rematch_object<'b, 'g, 'r, 's>(
|
2023-07-14 18:04:01 -04:00
|
|
|
&'x self,
|
2023-09-03 15:44:18 -04:00
|
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
2023-04-24 20:32:59 -04:00
|
|
|
map(tag(self.source), |s| {
|
2023-08-23 00:30:26 -04:00
|
|
|
Object::PlainText(PlainText {
|
|
|
|
source: Into::<&str>::into(s),
|
|
|
|
})
|
2023-04-24 20:32:59 -04:00
|
|
|
})(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-09-03 12:23:18 -04:00
|
|
|
use crate::context::Context;
|
|
|
|
use crate::context::ContextElement;
|
|
|
|
use crate::context::GlobalSettings;
|
|
|
|
use crate::context::List;
|
2023-09-08 17:45:49 -04:00
|
|
|
use crate::parser::object_parser::detect_standard_set_object_sans_plain_text;
|
2023-09-03 12:23:18 -04:00
|
|
|
use crate::types::Source;
|
2023-03-25 12:53:57 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn plain_text_simple() {
|
2023-08-24 17:15:24 -04:00
|
|
|
let input = OrgSource::new("foobarbaz");
|
2023-09-03 12:23:18 -04:00
|
|
|
let global_settings = GlobalSettings::default();
|
|
|
|
let initial_context = ContextElement::document_context();
|
|
|
|
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
2023-09-08 17:45:49 -04:00
|
|
|
let plain_text_matcher = parser_with_context!(plain_text(
|
|
|
|
detect_standard_set_object_sans_plain_text
|
|
|
|
))(&initial_context);
|
2023-03-25 12:53:57 -04:00
|
|
|
let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap();
|
2023-08-24 17:15:24 -04:00
|
|
|
assert_eq!(Into::<&str>::into(remaining), "");
|
|
|
|
assert_eq!(result.get_source(), Into::<&str>::into(input));
|
2023-03-25 12:53:57 -04:00
|
|
|
}
|
|
|
|
}
|