From 9060f9b26dd6462c6a7e8c66fa3d3d9c2fa0d313 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 4 Sep 2023 22:05:43 -0400 Subject: [PATCH] Only do a single pre-pass on the full document pulling out both setupfile and all other in-buffer settings. Previously we made a separate pass just to find setupfile and then we pulled the in-buffer settings from everything. --- src/parser/document.rs | 57 ++++++++++++++++------------------------ src/parser/mod.rs | 1 - src/parser/setup_file.rs | 22 ---------------- 3 files changed, 23 insertions(+), 57 deletions(-) delete mode 100644 src/parser/setup_file.rs diff --git a/src/parser/document.rs b/src/parser/document.rs index 9fd8021..033199c 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -20,7 +20,6 @@ use nom::sequence::tuple; use super::in_buffer_settings::scan_for_in_buffer_settings; use super::org_source::OrgSource; -use super::setup_file::scan_for_setup_file; use super::token::AllTokensIterator; use super::token::Token; use super::util::exit_matcher_parser; @@ -111,41 +110,31 @@ fn document_org_source<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Document<'s>> { - let setup_file = scan_for_setup_file(input); - let setup_file = if setup_file.is_ok() { - let (_remaining, setup_file) = setup_file.expect("If-statement proves this is okay."); - let setup_file_contents = context - .get_global_settings() - .file_access - .read_file(Into::<&str>::into(setup_file)) - .map_err(|err| nom::Err::>>::Failure(err.into()))?; - Some(setup_file_contents) - } else { - None - }; - let setup_file_settings = setup_file - .as_ref() - .map(|input| input.as_str().into()) - .map(scan_for_in_buffer_settings) - .map_or(Ok(None), |r| r.map(Some)) - .map_err(|_err| { - nom::Err::Error(CustomError::MyError(MyError( - "TODO: make this take an owned string so I can dump err.to_string() into it." - .into(), - ))) - })?; + let mut final_settings = Vec::new(); let (_, document_settings) = scan_for_in_buffer_settings(input)?; - let mut final_settings = Vec::with_capacity( - document_settings.len() - + match setup_file_settings { - Some((_, ref setup_file_settings)) => setup_file_settings.len(), - None => 0, - }, - ); - if let Some((_, setup_file_settings)) = setup_file_settings { - final_settings.extend(setup_file_settings.into_iter()); + let setup_files: Vec = document_settings + .iter() + .filter(|kw| kw.key.eq_ignore_ascii_case("setupfile")) + .map(|kw| kw.value) + .map(|setup_file| { + context + .get_global_settings() + .file_access + .read_file(setup_file) + .map_err(|err| nom::Err::>>::Failure(err.into())) + }) + .collect::, _>>()?; + for setup_file in setup_files.iter().map(String::as_str) { + let (_, setup_file_settings) = + scan_for_in_buffer_settings(setup_file.into()).map_err(|_err| { + nom::Err::Error(CustomError::MyError(MyError( + "TODO: make this take an owned string so I can dump err.to_string() into it." + .into(), + ))) + })?; + final_settings.extend(setup_file_settings); } - + final_settings.extend(document_settings); // TODO: read the keywords into settings and apply them to the GlobalSettings. let (remaining, document) = diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 8394ab0..72a350c 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -34,7 +34,6 @@ mod planning; mod property_drawer; mod radio_link; mod regular_link; -mod setup_file; pub mod sexp; mod statistics_cookie; mod subscript_and_superscript; diff --git a/src/parser/setup_file.rs b/src/parser/setup_file.rs deleted file mode 100644 index 509ab17..0000000 --- a/src/parser/setup_file.rs +++ /dev/null @@ -1,22 +0,0 @@ -use nom::bytes::complete::tag_no_case; -use nom::character::complete::anychar; -use nom::combinator::map; -use nom::multi::many_till; - -use super::keyword::filtered_keyword; -use super::OrgSource; -use crate::error::Res; - -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn scan_for_setup_file<'s>(input: OrgSource<'s>) -> Res, &'s str> { - let (remaining, setup) = map( - many_till(anychar, filtered_keyword(setupfile_key)), - |(_, kw)| kw.value, - )(input)?; - Ok((remaining, setup)) -} - -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn setupfile_key<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { - tag_no_case("setupfile")(input) -}