Implement clone for the context.

This commit is contained in:
Tom Alexander 2022-12-03 21:35:30 -05:00
parent b91e4df797
commit dff528da32
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 26 additions and 7 deletions

View File

@ -1,9 +1,11 @@
use std::rc::Rc;
#[derive(Debug, Clone)]
pub struct List<T> {
head: Option<Rc<Node<T>>>,
}
#[derive(Debug, Clone)]
pub struct Node<T> {
data: T,
parent: Option<Rc<Node<T>>>,

View File

@ -5,4 +5,4 @@ mod parser_with_context;
mod text;
mod text_element_parser;
pub use text_element_parser::document;
type Context<'r> = new_context::ContextTree<'r>;
type Context<'r> = &'r new_context::ContextTree<'r>;

View File

@ -5,6 +5,7 @@ use super::list::List;
type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
#[derive(Debug, Clone)]
pub struct ContextTree<'r> {
tree: List<ContextElement<'r>>,
}
@ -16,21 +17,22 @@ impl<'r> ContextTree<'r> {
pub fn with_additional_node<'x>(&self, data: ContextElement<'x>) -> ContextTree<'x> {
let new_list = self.tree.push_front(data);
ContextTree {
tree: new_list,
}
ContextTree { tree: new_list }
}
}
#[derive(Debug, Clone)]
pub enum ContextElement<'r> {
FailMatcherNode(FailMatcherNode<'r>),
PreviousElementNode(PreviousElementNode<'r>),
}
#[derive(Debug, Clone)]
pub struct FailMatcherNode<'r> {
pub fail_matcher: ChainBehavior<'r>,
}
#[derive(Debug, Clone)]
pub struct PreviousElementNode<'r> {
pub dummy: &'r str,
}
@ -40,3 +42,18 @@ pub enum ChainBehavior<'r> {
AndParent(Option<&'r Matcher>),
IgnoreParent(Option<&'r Matcher>),
}
impl std::fmt::Debug for ChainBehavior {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut formatter = f.debug_struct("ChainBehavior");
// match self {
// ChainBehavior::AndParent(_) => {
// formatter = formatter.field("type", &"AndParent");
// }
// ChainBehavior::IgnoreParent(_) => {
// formatter = formatter.field("type", &"IgnoreParent");
// }
// };
formatter.finish()
}
}

View File

@ -1,6 +1,6 @@
macro_rules! parser_with_context {
($target:ident) => {
move |context| move |i| $target(context, i)
move |context| |i| $target(&context, i)
};
}
pub(crate) use parser_with_context;

View File

@ -113,8 +113,8 @@ pub fn paragraph<'s, 'r>(
fn flat_text_element<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, TextElement<'s>> {
not(|i| context.check_fail_matcher(i))(i)?;
let bold_matcher = parser_with_context!(flat_bold)(context);
let link_matcher = parser_with_context!(flat_link)(context);
let bold_matcher = parser_with_context!(flat_bold)(context.clone());
let link_matcher = parser_with_context!(flat_link)(context.clone());
alt((
map(bold_matcher, TextElement::Bold),