Working now that we use clone and manually instantiate it.

This commit is contained in:
Tom Alexander 2022-07-17 18:06:14 -04:00
parent 3519c76c61
commit 8321c071e4
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 21 additions and 18 deletions

View File

@ -8,6 +8,7 @@ use std::rc::Rc;
pub struct NomContext<F>
where
F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>,
F: Clone,
{
pub fail_matcher: F,
@ -37,20 +38,17 @@ where
}
}
pub fn with_additional_fail_matcher<G>(
self,
additional_fail_matcher: G,
) -> NomContext<impl (for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>)>
where
G: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>,
G: Clone,
// O: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>,
// O: Clone,
{
NomContext {
fail_matcher: alt((additional_fail_matcher, self.fail_matcher)),
can_match_bold: self.can_match_bold,
can_match_link: self.can_match_link,
}
}
// pub fn with_additional_fail_matcher<G, O>(self, additional_fail_matcher: G) -> NomContext<O>
// where
// G: for<'g> FnMut(&'g str) -> IResult<&'g str, &'g str, VerboseError<&'g str>>,
// G: Clone,
// O: for<'o> FnMut(&'o str) -> IResult<&'o str, &'o str, VerboseError<&'o str>>,
// O: Clone,
// {
// NomContext {
// fail_matcher: move |i| alt((additional_fail_matcher, self.fail_matcher))(i),
// can_match_bold: self.can_match_bold,
// can_match_link: self.can_match_link,
// }
// }
}

View File

@ -26,7 +26,8 @@ pub fn flat_text_element<'a, F>(
context: &mut NomContext<F>,
) -> Res<&'a str, TextElement<'a>>
where
F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>,
F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>
+ std::clone::Clone,
{
not(&mut context.fail_matcher)(i)?;
if context.can_match_bold {
@ -59,7 +60,11 @@ where
F: Clone,
{
not(&mut context.fail_matcher)(i)?;
let new_context = context.without_bold();
let mut old_fail_matcher_clone = context.fail_matcher.clone();
let mut new_context =
NomContext::new(move |i| alt((recognize(bold_end), &mut old_fail_matcher_clone))(i));
new_context.can_match_bold = context.can_match_bold;
new_context.can_match_link = context.can_match_link;
let text_element_parser = parser_with_context!(flat_text_element)(new_context);
map(
recognize(tuple((