Fix using references for context elements.

This commit is contained in:
Tom Alexander 2023-09-02 22:44:21 -04:00
parent c309d14776
commit ba57eb16fd
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 22 additions and 38 deletions

View File

@ -31,14 +31,8 @@ impl<'parent, T> List<'parent, T> {
pub fn iter_list(&self) -> IterList<'_, T> { pub fn iter_list(&self) -> IterList<'_, T> {
IterList { next: Some(self) } IterList { next: Some(self) }
} }
}
pub trait ListType<'parent, T> { pub fn push(&'parent self, item: T) -> Self {
fn push(&'parent self, item: T) -> List<'parent, T>;
}
impl<'parent, T> ListType<'parent, T> for List<'parent, T> {
fn push(&'parent self, item: T) -> Self {
Self { Self {
data: item, data: item,
parent: Some(self), parent: Some(self),
@ -46,15 +40,6 @@ impl<'parent, T> ListType<'parent, T> for List<'parent, T> {
} }
} }
impl<'parent, T> ListType<'parent, T> for Link<'parent, T> {
fn push(&'parent self, item: T) -> List<'parent, T> {
List {
data: item,
parent: *self,
}
}
}
pub struct Iter<'a, T> { pub struct Iter<'a, T> {
next: Link<'a, T>, next: Link<'a, T>,
} }

View File

@ -1,4 +1,3 @@
use self::parser_context::Context;
use crate::error::Res; use crate::error::Res;
use crate::parser::OrgSource; use crate::parser::OrgSource;
@ -8,14 +7,17 @@ mod list;
mod parser_context; mod parser_context;
mod parser_with_context; mod parser_with_context;
pub type RefContext<'r, 's> = &'r Context<'r, 's>; pub type RefContext<'b, 'r, 's> = &'b Context<'r, 's>;
trait ContextMatcher = pub trait ContextMatcher =
for<'r, 's> Fn(RefContext<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>; for<'b, 'r, 's> Fn(RefContext<'b, 'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
type DynContextMatcher<'c> = dyn ContextMatcher + 'c; type DynContextMatcher<'c> = dyn ContextMatcher + 'c;
trait Matcher = for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>; pub trait Matcher = for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
type DynMatcher<'c> = dyn Matcher + 'c; type DynMatcher<'c> = dyn Matcher + 'c;
pub use exiting::ExitClass; pub use exiting::ExitClass;
pub use global_settings::GlobalSettings;
pub use list::List;
pub use parser_context::Context;
pub use parser_context::ContextElement; pub use parser_context::ContextElement;
pub use parser_context::ExitMatcherNode; pub use parser_context::ExitMatcherNode;
pub(crate) use parser_with_context::parser_with_context; pub(crate) use parser_with_context::parser_with_context;

View File

@ -3,7 +3,6 @@ use nom::combinator::eof;
use super::exiting::ExitClass; use super::exiting::ExitClass;
use super::global_settings::GlobalSettings; use super::global_settings::GlobalSettings;
use super::list::List; use super::list::List;
use super::list::ListType;
use super::DynContextMatcher; use super::DynContextMatcher;
use super::RefContext; use super::RefContext;
use crate::error::Res; use crate::error::Res;
@ -134,13 +133,13 @@ impl<'r> std::fmt::Debug for ExitMatcherNode<'r> {
#[derive(Debug)] #[derive(Debug)]
pub struct Context<'r, 's> { pub struct Context<'r, 's> {
global_settings: &'s GlobalSettings<'s>, global_settings: &'s GlobalSettings<'s>,
tree: &'r List<'r, ContextElement<'r, 's>>, tree: List<'r, &'r ContextElement<'r, 's>>,
} }
impl<'r, 's> Context<'r, 's> { impl<'r, 's> Context<'r, 's> {
pub fn new( pub fn new(
global_settings: &'s GlobalSettings<'s>, global_settings: &'s GlobalSettings<'s>,
tree: &'r List<'r, ContextElement<'r, 's>>, tree: List<'r, &'r ContextElement<'r, 's>>,
) -> Self { ) -> Self {
Self { Self {
global_settings, global_settings,
@ -148,36 +147,33 @@ impl<'r, 's> Context<'r, 's> {
} }
} }
pub fn with_additional_node(&self, data: ContextElement<'r, 's>) -> Self { pub fn with_additional_node(&'r self, new_element: &'r ContextElement<'r, 's>) -> Self {
let new_tree = self.tree.push(data); let new_tree = self.tree.push(new_element);
Self { Self::new(self.global_settings, new_tree)
global_settings: self.global_settings,
tree: new_tree,
}
} }
pub fn iter(&self) -> super::list::Iter<'r, ContextElement<'r, 's>> { pub fn iter(&'r self) -> super::list::Iter<'r, &'r ContextElement<'r, 's>> {
self.tree.iter() self.tree.iter()
} }
pub fn iter_context(&self) -> Iter<'r, 's> { pub fn iter_context(&'r self) -> Iter<'r, 's> {
Iter { Iter {
next: self.tree.iter_list(), next: self.tree.iter_list(),
global_settings: self.global_settings, global_settings: self.global_settings,
} }
} }
pub fn get_parent(&self) -> Option<Self> { pub fn get_parent(&'r self) -> Option<Self> {
self.tree.get_parent().map(|parent_tree| Self { self.tree.get_parent().map(|parent_tree| Self {
global_settings: self.global_settings, global_settings: self.global_settings,
tree: parent_tree, tree: parent_tree.clone(),
}) })
} }
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn document_end<'r, 's>( fn document_end<'b, 'r, 's>(
_context: RefContext<'r, 's>, _context: RefContext<'b, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> { ) -> Res<OrgSource<'s>, OrgSource<'s>> {
eof(input) eof(input)
@ -185,7 +181,7 @@ fn document_end<'r, 's>(
pub struct Iter<'r, 's> { pub struct Iter<'r, 's> {
global_settings: &'s GlobalSettings<'s>, global_settings: &'s GlobalSettings<'s>,
next: super::list::IterList<'r, ContextElement<'r, 's>>, next: super::list::IterList<'r, &'r ContextElement<'r, 's>>,
} }
impl<'r, 's> Iterator for Iter<'r, 's> { impl<'r, 's> Iterator for Iter<'r, 's> {
@ -193,7 +189,8 @@ impl<'r, 's> Iterator for Iter<'r, 's> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let next_tree = self.next.next(); let next_tree = self.next.next();
let ret = next_tree.map(|parent_tree| Context::new(self.global_settings, parent_tree)); let ret =
next_tree.map(|parent_tree| Context::new(self.global_settings, parent_tree.clone()));
ret ret
} }
} }