From 9a479b33e060d1f66183919a4c7bf18d13c332f8 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 29 Sep 2023 15:42:07 -0400 Subject: [PATCH] Make the same changes we did for stdin compare to comparing files. --- src/compare/compare.rs | 77 +++++++++++++++++++++++------------------- src/compare/mod.rs | 1 + src/compare/parse.rs | 9 +++-- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/compare/compare.rs b/src/compare/compare.rs index f57abbc..4a1c033 100644 --- a/src/compare/compare.rs +++ b/src/compare/compare.rs @@ -17,41 +17,7 @@ pub fn run_anonymous_compare>( } pub fn run_compare_on_file>(org_path: P) -> Result<(), Box> { - let org_path = org_path.as_ref(); - eprintln!("Using emacs version: {}", get_emacs_version()?.trim()); - eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim()); - let parent_directory = org_path - .parent() - .ok_or("Should be contained inside a directory.")?; - let org_contents = std::fs::read_to_string(org_path)?; - // TODO: This is a work-around to pretend that dos line endings do not exist. It would be better to handle the difference in line endings. - let org_contents = org_contents.replace("\r\n", "\n"); - let org_contents = org_contents.as_str(); - let file_access_interface = LocalFileAccessInterface { - working_directory: Some(parent_directory.to_path_buf()), - }; - let global_settings = { - let mut global_settings = GlobalSettings::default(); - global_settings.file_access = &file_access_interface; - global_settings - }; - let rust_parsed = parse_with_settings(org_contents, &global_settings)?; - let org_sexp = emacs_parse_file_org_document(org_path)?; - let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?; - - println!("{}\n\n\n", org_contents); - println!("{}", org_sexp); - println!("{:#?}", rust_parsed); - - // We do the diffing after printing out both parsed forms in case the diffing panics - let diff_result = compare_document(&parsed_sexp, &rust_parsed)?; - diff_result.print(org_contents)?; - - if diff_result.is_bad() { - Err("Diff results do not match.")?; - } - - Ok(()) + run_compare_on_file_with_settings(org_path, &GlobalSettings::default()) } pub fn run_anonymous_compare_with_settings>( @@ -81,3 +47,44 @@ pub fn run_anonymous_compare_with_settings>( Ok(()) } + +pub fn run_compare_on_file_with_settings>( + org_path: P, + global_settings: &GlobalSettings, +) -> Result<(), Box> { + let org_path = org_path.as_ref(); + eprintln!("Using emacs version: {}", get_emacs_version()?.trim()); + eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim()); + let parent_directory = org_path + .parent() + .ok_or("Should be contained inside a directory.")?; + let org_contents = std::fs::read_to_string(org_path)?; + // TODO: This is a work-around to pretend that dos line endings do not exist. It would be better to handle the difference in line endings. + let org_contents = org_contents.replace("\r\n", "\n"); + let org_contents = org_contents.as_str(); + let file_access_interface = LocalFileAccessInterface { + working_directory: Some(parent_directory.to_path_buf()), + }; + let global_settings = { + let mut global_settings = global_settings.clone(); + global_settings.file_access = &file_access_interface; + global_settings + }; + let rust_parsed = parse_with_settings(org_contents, &global_settings)?; + let org_sexp = emacs_parse_file_org_document(org_path, &global_settings)?; + let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?; + + println!("{}\n\n\n", org_contents); + println!("{}", org_sexp); + println!("{:#?}", rust_parsed); + + // We do the diffing after printing out both parsed forms in case the diffing panics + let diff_result = compare_document(&parsed_sexp, &rust_parsed)?; + diff_result.print(org_contents)?; + + if diff_result.is_bad() { + Err("Diff results do not match.")?; + } + + Ok(()) +} diff --git a/src/compare/mod.rs b/src/compare/mod.rs index f901679..1a75e2e 100644 --- a/src/compare/mod.rs +++ b/src/compare/mod.rs @@ -7,3 +7,4 @@ mod util; pub use compare::run_anonymous_compare; pub use compare::run_anonymous_compare_with_settings; pub use compare::run_compare_on_file; +pub use compare::run_compare_on_file_with_settings; diff --git a/src/compare/parse.rs b/src/compare/parse.rs index 5440749..37cf4e1 100644 --- a/src/compare/parse.rs +++ b/src/compare/parse.rs @@ -49,7 +49,10 @@ where Ok(String::from_utf8(org_sexp)?) } -pub fn emacs_parse_file_org_document

(file_path: P) -> Result> +pub(crate) fn emacs_parse_file_org_document

( + file_path: P, + global_settings: &GlobalSettings, +) -> Result> where P: AsRef, { @@ -62,9 +65,11 @@ where r#"(progn (require 'org) (defun org-table-align () t) + {global_settings} (org-mode) (message "%s" (pp-to-string (org-element-parse-buffer))) -)"# +)"#, + global_settings = global_settings_elisp(global_settings) ); let mut cmd = Command::new("emacs"); let cmd = cmd