74 lines
2.7 KiB
Rust
74 lines
2.7 KiB
Rust
![]() |
use std::path::Path;
|
||
|
|
||
|
use crate::compare::diff::compare_document;
|
||
|
use crate::compare::parse::emacs_parse_anonymous_org_document;
|
||
|
use crate::compare::parse::emacs_parse_file_org_document;
|
||
|
use crate::compare::parse::get_emacs_version;
|
||
|
use crate::compare::parse::get_org_mode_version;
|
||
|
use crate::compare::sexp::sexp;
|
||
|
use crate::parser::parse;
|
||
|
use crate::parser::parse_with_settings;
|
||
|
use crate::GlobalSettings;
|
||
|
use crate::LocalFileAccessInterface;
|
||
|
|
||
|
pub fn run_anonymous_compare<P: AsRef<str>>(
|
||
|
org_contents: P,
|
||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||
|
let org_contents = org_contents.as_ref();
|
||
|
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>> {
|
||
|
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)?;
|
||
|
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(())
|
||
|
}
|