Apply more suggestions.

This commit is contained in:
Tom Alexander 2023-10-16 17:28:28 -04:00
parent b4f9a3b9b6
commit 1d329cc310
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
8 changed files with 43 additions and 45 deletions

View File

@ -30,4 +30,5 @@ pub use global_settings::GlobalSettings;
pub use global_settings::HeadlineLevelFilter; pub use global_settings::HeadlineLevelFilter;
pub use global_settings::DEFAULT_TAB_WIDTH; pub use global_settings::DEFAULT_TAB_WIDTH;
pub(crate) use list::List; pub(crate) use list::List;
pub(crate) use parser_with_context::bind_context;
pub(crate) use parser_with_context::parser_with_context; pub(crate) use parser_with_context::parser_with_context;

View File

@ -4,3 +4,10 @@ macro_rules! parser_with_context {
}; };
} }
pub(crate) use parser_with_context; pub(crate) use parser_with_context;
macro_rules! bind_context {
($target:expr, $context:expr) => {
move |i| $target($context, i)
};
}
pub(crate) use bind_context;

View File

@ -552,6 +552,7 @@ fn detect_contentless_item_contents<'b, 'g, 'r, 's>(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::context::bind_context;
use crate::context::Context; use crate::context::Context;
use crate::context::GlobalSettings; use crate::context::GlobalSettings;
use crate::context::List; use crate::context::List;
@ -563,7 +564,7 @@ mod tests {
let global_settings = GlobalSettings::default(); let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context)); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context); let plain_list_item_matcher = bind_context!(plain_list_item, &initial_context);
let (remaining, (_, result)) = plain_list_item_matcher(input).unwrap(); let (remaining, (_, result)) = plain_list_item_matcher(input).unwrap();
assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(Into::<&str>::into(remaining), "");
assert_eq!(result.get_standard_properties().get_source(), "1."); assert_eq!(result.get_standard_properties().get_source(), "1.");
@ -575,7 +576,7 @@ mod tests {
let global_settings = GlobalSettings::default(); let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context)); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context); let plain_list_item_matcher = bind_context!(plain_list_item, &initial_context);
let (remaining, (_, result)) = plain_list_item_matcher(input).unwrap(); let (remaining, (_, result)) = plain_list_item_matcher(input).unwrap();
assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(Into::<&str>::into(remaining), "");
assert_eq!(result.get_standard_properties().get_source(), "1. foo"); assert_eq!(result.get_standard_properties().get_source(), "1. foo");
@ -642,7 +643,7 @@ mod tests {
let global_settings = GlobalSettings::default(); let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context)); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let plain_list_matcher = bind_context!(element(true), &initial_context);
let (remaining, result) = let (remaining, result) =
plain_list_matcher(input).expect("Should parse the plain list successfully."); plain_list_matcher(input).expect("Should parse the plain list successfully.");
assert_eq!(Into::<&str>::into(remaining), " ipsum\n"); assert_eq!(Into::<&str>::into(remaining), " ipsum\n");
@ -670,7 +671,7 @@ baz"#,
let global_settings = GlobalSettings::default(); let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context)); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let plain_list_matcher = bind_context!(element(true), &initial_context);
let (remaining, result) = let (remaining, result) =
plain_list_matcher(input).expect("Should parse the plain list successfully."); plain_list_matcher(input).expect("Should parse the plain list successfully.");
assert_eq!(Into::<&str>::into(remaining), "baz"); assert_eq!(Into::<&str>::into(remaining), "baz");
@ -703,7 +704,7 @@ dolar"#,
let global_settings = GlobalSettings::default(); let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context)); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let plain_list_matcher = bind_context!(element(true), &initial_context);
let (remaining, result) = let (remaining, result) =
plain_list_matcher(input).expect("Should parse the plain list successfully."); plain_list_matcher(input).expect("Should parse the plain list successfully.");
assert_eq!(Into::<&str>::into(remaining), "dolar"); assert_eq!(Into::<&str>::into(remaining), "dolar");

View File

@ -114,18 +114,15 @@ impl<'x> RematchObject<'x> for PlainText<'x> {
recognize(one_of::<&str, &str, CustomError<_>>(" \t")), recognize(one_of::<&str, &str, CustomError<_>>(" \t")),
line_ending, line_ending,
))))(goal); ))))(goal);
match is_whitespace { if let Ok((new_goal, _)) = is_whitespace {
Ok((new_goal, _)) => {
let (new_remaining, _) = many1(org_space_or_line_ending)(remaining)?; let (new_remaining, _) = many1(org_space_or_line_ending)(remaining)?;
remaining = new_remaining; remaining = new_remaining;
goal = new_goal; goal = new_goal;
continue; continue;
} }
Err(_) => {}
};
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(
"Target does not match.".into(), "Target does not match.",
)))); ))));
} }
@ -144,6 +141,7 @@ mod tests {
use nom::combinator::map; use nom::combinator::map;
use super::*; use super::*;
use crate::context::bind_context;
use crate::context::Context; use crate::context::Context;
use crate::context::ContextElement; use crate::context::ContextElement;
use crate::context::GlobalSettings; use crate::context::GlobalSettings;
@ -158,8 +156,9 @@ mod tests {
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context)); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let (remaining, result) = map( let (remaining, result) = map(
parser_with_context!(plain_text(detect_standard_set_object_sans_plain_text))( bind_context!(
&initial_context, plain_text(detect_standard_set_object_sans_plain_text),
&initial_context
), ),
Object::PlainText, Object::PlainText,
)(input) )(input)

View File

@ -40,7 +40,7 @@ pub(crate) fn property_drawer<'b, 'g, 'r, 's>(
) -> Res<OrgSource<'s>, PropertyDrawer<'s>> { ) -> Res<OrgSource<'s>, PropertyDrawer<'s>> {
if immediate_in_section(context, "property-drawer") { if immediate_in_section(context, "property-drawer") {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(), "Cannot nest objects of the same element",
)))); ))));
} }
let ( let (

View File

@ -53,7 +53,7 @@ pub(crate) fn radio_link<'b, 'g, 'r, 's>(
} }
} }
Err(nom::Err::Error(CustomError::MyError(MyError( Err(nom::Err::Error(CustomError::MyError(MyError(
"NoRadioLink".into(), "NoRadioLink",
)))) ))))
} }
@ -99,7 +99,7 @@ pub(crate) fn rematch_target<'x, 'b, 'g, 'r, 's>(
} }
_ => { _ => {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(
"OnlyMinimalSetObjectsAllowed".into(), "OnlyMinimalSetObjectsAllowed",
)))); ))));
} }
}; };

View File

@ -139,14 +139,7 @@ fn pathreg<'b, 'g, 'r, 's>(
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathReg<'s>> { ) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, path) = map_parser( let (remaining, path) = map_parser(
escaped( escaped(take_till1(|c| matches!(c, '\\' | '[' | ']')), '\\', anychar),
take_till1(|c| match c {
'\\' | '[' | ']' => true,
_ => false,
}),
'\\',
anychar,
),
parser_with_context!(parse_path_reg)(context), parser_with_context!(parse_path_reg)(context),
)(input)?; )(input)?;
Ok((remaining, path)) Ok((remaining, path))
@ -262,12 +255,9 @@ fn apply_link_templates<'b, 'g, 'r, 's>(
}; };
} }
// Handle lingering state // Handle lingering state
match state { if let ParserState::Percent = state {
ParserState::Percent => {
ret.push('%'); ret.push('%');
} }
_ => {}
}
if !injected_value { if !injected_value {
ret.push_str(inject_value); ret.push_str(inject_value);
} }
@ -494,6 +484,6 @@ fn impl_path_reg_end<'b, 'g, 'r, 's>(
} }
Err(nom::Err::Error(CustomError::MyError(MyError( Err(nom::Err::Error(CustomError::MyError(MyError(
"No path reg end".into(), "No path reg end",
)))) ))))
} }

View File

@ -65,12 +65,12 @@ pub(crate) fn zeroth_section<'b, 'g, 'r, 's>(
}, },
)(remaining)?; )(remaining)?;
comment_and_property_drawer_element.map(|(comment, property_drawer, _ws)| { if let Some((comment, property_drawer, _ws)) = comment_and_property_drawer_element {
children.insert(0, Element::PropertyDrawer(property_drawer)); children.insert(0, Element::PropertyDrawer(property_drawer));
comment if let Some(ele) = comment.map(Element::Comment) {
.map(Element::Comment) children.insert(0, ele);
.map(|ele| children.insert(0, ele)); }
}); }
let (remaining, _trailing_ws) = let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
@ -121,12 +121,12 @@ pub(crate) fn section<'b, 'g, 'r, 's>(
!children.is_empty() || property_drawer_element.is_some() || planning_element.is_some() !children.is_empty() || property_drawer_element.is_some() || planning_element.is_some()
}, },
)(remaining)?; )(remaining)?;
property_drawer_element if let Some(ele) = property_drawer_element.map(Element::PropertyDrawer) {
.map(Element::PropertyDrawer) children.insert(0, ele);
.map(|ele| children.insert(0, ele)); }
planning_element if let Some(ele) = planning_element.map(Element::Planning) {
.map(Element::Planning) children.insert(0, ele)
.map(|ele| children.insert(0, ele)); }
let (remaining, _trailing_ws) = let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;