Clean up code duplication.

This commit is contained in:
Tom Alexander 2023-09-29 15:35:57 -04:00
parent 2012e5a6d5
commit 7a854838ef
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 3 additions and 58 deletions

View File

@ -2,12 +2,10 @@ use std::path::Path;
use crate::compare::diff::compare_document; use crate::compare::diff::compare_document;
use crate::compare::parse::emacs_parse_anonymous_org_document; use crate::compare::parse::emacs_parse_anonymous_org_document;
use crate::compare::parse::emacs_parse_anonymous_org_document_with_settings;
use crate::compare::parse::emacs_parse_file_org_document; use crate::compare::parse::emacs_parse_file_org_document;
use crate::compare::parse::get_emacs_version; use crate::compare::parse::get_emacs_version;
use crate::compare::parse::get_org_mode_version; use crate::compare::parse::get_org_mode_version;
use crate::compare::sexp::sexp; use crate::compare::sexp::sexp;
use crate::parser::parse;
use crate::parser::parse_with_settings; use crate::parser::parse_with_settings;
use crate::GlobalSettings; use crate::GlobalSettings;
use crate::LocalFileAccessInterface; use crate::LocalFileAccessInterface;
@ -15,28 +13,7 @@ use crate::LocalFileAccessInterface;
pub fn run_anonymous_compare<P: AsRef<str>>( pub fn run_anonymous_compare<P: AsRef<str>>(
org_contents: P, org_contents: P,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
// 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. run_anonymous_compare_with_settings(org_contents, &GlobalSettings::default())
let org_contents = org_contents.as_ref().replace("\r\n", "\n");
let org_contents = org_contents.as_str();
eprintln!("Using emacs version: {}", get_emacs_version()?.trim());
eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim());
let rust_parsed = parse(org_contents)?;
let org_sexp = emacs_parse_anonymous_org_document(org_contents)?;
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(())
} }
pub fn run_compare_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> { pub fn run_compare_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> {
@ -87,7 +64,7 @@ pub fn run_anonymous_compare_with_settings<P: AsRef<str>>(
eprintln!("Using emacs version: {}", get_emacs_version()?.trim()); eprintln!("Using emacs version: {}", get_emacs_version()?.trim());
eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim()); eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim());
let rust_parsed = parse_with_settings(org_contents, global_settings)?; let rust_parsed = parse_with_settings(org_contents, global_settings)?;
let org_sexp = emacs_parse_anonymous_org_document_with_settings(org_contents, global_settings)?; let org_sexp = emacs_parse_anonymous_org_document(org_contents, global_settings)?;
let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?; let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?;
println!("{}\n\n\n", org_contents); println!("{}\n\n\n", org_contents);

View File

@ -14,39 +14,7 @@ fn global_settings_elisp(global_settings: &GlobalSettings) -> String {
ret ret
} }
pub fn emacs_parse_anonymous_org_document<C>( pub(crate) fn emacs_parse_anonymous_org_document<C>(
file_contents: C,
) -> Result<String, Box<dyn std::error::Error>>
where
C: AsRef<str>,
{
let escaped_file_contents = escape_elisp_string(file_contents);
let elisp_script = format!(
r#"(progn
(erase-buffer)
(require 'org)
(defun org-table-align () t)
(insert "{escaped_file_contents}")
(org-mode)
(message "%s" (pp-to-string (org-element-parse-buffer)))
)"#,
escaped_file_contents = escaped_file_contents
);
let mut cmd = Command::new("emacs");
let cmd = cmd
.arg("-q")
.arg("--no-site-file")
.arg("--no-splash")
.arg("--batch")
.arg("--eval")
.arg(elisp_script);
let out = cmd.output()?;
out.status.exit_ok()?;
let org_sexp = out.stderr;
Ok(String::from_utf8(org_sexp)?)
}
pub fn emacs_parse_anonymous_org_document_with_settings<C>(
file_contents: C, file_contents: C,
global_settings: &GlobalSettings, global_settings: &GlobalSettings,
) -> Result<String, Box<dyn std::error::Error>> ) -> Result<String, Box<dyn std::error::Error>>