Add default constructors.

This commit is contained in:
Tom Alexander 2023-09-02 18:40:01 -04:00
parent 54825538e4
commit 25b8c80d4e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 44 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#![feature(round_char_boundary)] #![feature(round_char_boundary)]
#![feature(exit_status_error)] #![feature(exit_status_error)]
#![feature(trait_alias)]
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
mod compare; mod compare;

View File

@ -18,6 +18,7 @@ use nom::multi::separated_list1;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::element::Element; use super::element::Element;
use super::list::List;
use super::object::Object; use super::object::Object;
use super::org_source::convert_error; use super::org_source::convert_error;
use super::org_source::OrgSource; use super::org_source::OrgSource;
@ -35,7 +36,6 @@ use crate::parser::element_parser::element;
use crate::parser::exiting::ExitClass; use crate::parser::exiting::ExitClass;
use crate::parser::object_parser::standard_set_object; use crate::parser::object_parser::standard_set_object;
use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ContextTree;
use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_context::ExitMatcherNode;
use crate::parser::planning::planning; use crate::parser::planning::planning;
use crate::parser::property_drawer::property_drawer; 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"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
#[allow(dead_code)] #[allow(dead_code)]
pub fn document(input: &str) -> Res<&str, Document> { 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 wrapped_input = OrgSource::new(input);
let (remaining, document) = _document(&initial_context, wrapped_input) let (remaining, document) = _document(&initial_context, wrapped_input)
.map(|(rem, out)| (Into::<&str>::into(rem), out)) .map(|(rem, out)| (Into::<&str>::into(rem), out))

View File

@ -1,6 +1,10 @@
use nom::combinator::eof;
use super::list::List; use super::list::List;
use super::org_source::OrgSource;
use super::DynContextMatcher; use super::DynContextMatcher;
use super::Object; use super::Object;
use crate::error::Res;
use crate::parser::exiting::ExitClass; use crate::parser::exiting::ExitClass;
use crate::parser::list::ListType; 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)] #[derive(Debug)]
pub struct Context<'r, 's> { pub struct Context<'r, 's> {
global_settings: &'s GlobalSettings<'s>, global_settings: &'s GlobalSettings<'s>,
@ -147,12 +157,22 @@ impl<'r, 's> Context<'r, 's> {
global_settings: &'s GlobalSettings<'s>, global_settings: &'s GlobalSettings<'s>,
tree: List<'r, ContextElement<'r, 's>>, tree: List<'r, ContextElement<'r, 's>>,
) -> Self { ) -> Self {
Context { Self {
global_settings, global_settings,
tree, 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 { pub fn push(&self, data: ContextElement<'r, 's>) -> Self {
let new_tree = self.tree.push(data); let new_tree = self.tree.push(data);
Self { 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>, OrgSource<'s>> {
eof(input)
}