From 25b8c80d4ee82133892cc0c06171b60eef60857e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 2 Sep 2023 18:40:01 -0400 Subject: [PATCH] Add default constructors. --- src/lib.rs | 1 + src/parser/document.rs | 4 ++-- src/parser/parser_context.rs | 42 +++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 513e5c9..70249dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![feature(round_char_boundary)] #![feature(exit_status_error)] +#![feature(trait_alias)] #[cfg(feature = "compare")] mod compare; diff --git a/src/parser/document.rs b/src/parser/document.rs index 760c6dc..1fc0dbd 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -18,6 +18,7 @@ use nom::multi::separated_list1; use nom::sequence::tuple; use super::element::Element; +use super::list::List; use super::object::Object; use super::org_source::convert_error; use super::org_source::OrgSource; @@ -35,7 +36,6 @@ use crate::parser::element_parser::element; use crate::parser::exiting::ExitClass; use crate::parser::object_parser::standard_set_object; use crate::parser::parser_context::ContextElement; -use crate::parser::parser_context::ContextTree; use crate::parser::parser_context::ExitMatcherNode; use crate::parser::planning::planning; use crate::parser::property_drawer::property_drawer; @@ -102,7 +102,7 @@ impl<'s> Source<'s> for Heading<'s> { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[allow(dead_code)] pub fn document(input: &str) -> Res<&str, Document> { - let initial_context: ContextTree<'_, '_> = ContextTree::new(); + let initial_context = Context::default(); let wrapped_input = OrgSource::new(input); let (remaining, document) = _document(&initial_context, wrapped_input) .map(|(rem, out)| (Into::<&str>::into(rem), out)) diff --git a/src/parser/parser_context.rs b/src/parser/parser_context.rs index 35e3c57..1dce6e9 100644 --- a/src/parser/parser_context.rs +++ b/src/parser/parser_context.rs @@ -1,6 +1,10 @@ +use nom::combinator::eof; + use super::list::List; +use super::org_source::OrgSource; use super::DynContextMatcher; use super::Object; +use crate::error::Res; use crate::parser::exiting::ExitClass; use crate::parser::list::ListType; @@ -136,6 +140,12 @@ impl<'s> GlobalSettings<'s> { } } +impl<'s> Default for GlobalSettings<'s> { + fn default() -> Self { + GlobalSettings::new() + } +} + #[derive(Debug)] pub struct Context<'r, 's> { global_settings: &'s GlobalSettings<'s>, @@ -147,12 +157,22 @@ impl<'r, 's> Context<'r, 's> { global_settings: &'s GlobalSettings<'s>, tree: List<'r, ContextElement<'r, 's>>, ) -> Self { - Context { + Self { global_settings, tree, } } + pub fn document_context(global_settings: &'s GlobalSettings<'s>) -> Self { + Context::new( + global_settings, + List::new(ContextElement::ExitMatcherNode(ExitMatcherNode { + exit_matcher: &document_end, + class: ExitClass::Document, + })), + ) + } + pub fn push(&self, data: ContextElement<'r, 's>) -> Self { let new_tree = self.tree.push(data); Self { @@ -161,3 +181,23 @@ impl<'r, 's> Context<'r, 's> { } } } + +impl<'r, 's> Default for Context<'r, 's> { + fn default() -> Self { + Context::new( + GlobalSettings::default(), + List::new(ContextElement::ExitMatcherNode(ExitMatcherNode { + exit_matcher: &document_end, + class: ExitClass::Document, + })), + ) + } +} + +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +fn document_end<'r, 's>( + _context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { + eof(input) +}