Fixing more errors.

This commit is contained in:
Tom Alexander
2023-09-03 00:27:50 -04:00
parent 15e8d1ab77
commit b54c6d366c
7 changed files with 118 additions and 72 deletions

View File

@@ -5,16 +5,22 @@ use nom::character::complete::space0;
use nom::combinator::verify;
use nom::multi::many_till;
use super::object_parser::minimal_set_object;
use super::org_source::OrgSource;
use super::util::exit_matcher_parser;
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
use super::Context;
use super::Object;
use crate::context::parser_with_context;
use crate::context::ContextElement;
use crate::context::ExitClass;
use crate::context::ExitMatcherNode;
use crate::context::RefContext;
use crate::error::CustomError;
use crate::error::MyError;
use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::RadioLink;
use crate::parser::RadioTarget;
use crate::types::Object;
use crate::types::RadioLink;
use crate::types::RadioTarget;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn radio_link<'r, 's>(
@@ -23,7 +29,7 @@ pub fn radio_link<'r, 's>(
) -> Res<OrgSource<'s>, RadioLink<'s>> {
let radio_targets = context
.iter()
.filter_map(|context_element| match context_element.get_data() {
.filter_map(|context_element| match context_element {
ContextElement::RadioTarget(targets) => Some(targets),
_ => None,
})
@@ -84,11 +90,11 @@ pub fn radio_target<'r, 's>(
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, RadioTarget<'s>> {
let (remaining, _opening) = tag("<<<")(input)?;
let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &radio_target_end,
}));
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &radio_target_end,
});
let parser_context = context.with_additional_node(&parser_context);
let (remaining, (children, _exit_contents)) = verify(
many_till(
@@ -130,25 +136,28 @@ pub trait RematchObject<'x> {
#[cfg(test)]
mod tests {
use super::*;
use crate::context::Context;
use crate::context::GlobalSettings;
use crate::context::List;
use crate::parser::element_parser::element;
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;
use crate::parser::Bold;
use crate::parser::PlainText;
use crate::types::Bold;
use crate::types::Element;
use crate::types::PlainText;
use crate::types::Source;
#[test]
fn plain_text_radio_target() {
let input = OrgSource::new("foo bar baz");
let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })];
let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = initial_context
.with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match]));
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let document_context = ContextElement::RadioTarget(vec![&radio_target_match]);
let document_context = initial_context.with_additional_node(&document_context);
let paragraph_matcher = parser_with_context!(element(true))(&document_context);
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
let first_paragraph = match first_paragraph {
crate::parser::Element::Paragraph(paragraph) => paragraph,
Element::Paragraph(paragraph) => paragraph,
_ => panic!("Should be a paragraph!"),
};
assert_eq!(Into::<&str>::into(remaining), "");
@@ -173,14 +182,16 @@ mod tests {
source: "*bar*",
children: vec![Object::PlainText(PlainText { source: "bar" })],
})];
let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = initial_context
.with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match]));
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let document_context = ContextElement::RadioTarget(vec![&radio_target_match]);
let document_context = initial_context.with_additional_node(&document_context);
let paragraph_matcher = parser_with_context!(element(true))(&document_context);
let (remaining, first_paragraph) =
paragraph_matcher(input.into()).expect("Parse first paragraph");
let first_paragraph = match first_paragraph {
crate::parser::Element::Paragraph(paragraph) => paragraph,
Element::Paragraph(paragraph) => paragraph,
_ => panic!("Should be a paragraph!"),
};
assert_eq!(Into::<&str>::into(remaining), "");