2023-12-27 09:31:54 -05:00
|
|
|
use super::compare::wasm_compare_document;
|
2023-12-27 08:57:56 -05:00
|
|
|
use crate::compare::sexp;
|
2023-12-27 08:17:15 -05:00
|
|
|
use crate::context::GlobalSettings;
|
|
|
|
use crate::parser::parse_with_settings;
|
2023-12-27 08:46:18 -05:00
|
|
|
use crate::util::emacs_parse_anonymous_org_document;
|
2023-12-27 08:17:15 -05:00
|
|
|
use crate::util::print_versions;
|
2023-12-27 08:57:56 -05:00
|
|
|
use crate::wasm::ToWasm;
|
|
|
|
use crate::wasm::ToWasmContext;
|
2023-12-27 08:17:15 -05:00
|
|
|
|
|
|
|
pub async fn wasm_run_anonymous_compare<P: AsRef<str>>(
|
|
|
|
org_contents: P,
|
|
|
|
) -> Result<bool, Box<dyn std::error::Error>> {
|
|
|
|
wasm_run_anonymous_compare_with_settings(org_contents, &GlobalSettings::default(), false).await
|
|
|
|
}
|
2023-12-26 19:06:12 -05:00
|
|
|
|
2023-12-26 18:55:28 -05:00
|
|
|
pub async fn wasm_run_anonymous_compare_with_settings<'g, 's, P: AsRef<str>>(
|
|
|
|
org_contents: P,
|
|
|
|
global_settings: &GlobalSettings<'g, 's>,
|
|
|
|
silent: bool,
|
|
|
|
) -> Result<bool, 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.
|
|
|
|
let org_contents = org_contents.as_ref().replace("\r\n", "\n");
|
|
|
|
let org_contents = org_contents.as_str();
|
2023-12-26 19:06:12 -05:00
|
|
|
if !silent {
|
|
|
|
print_versions().await?;
|
|
|
|
}
|
2023-12-26 18:55:28 -05:00
|
|
|
let rust_parsed = parse_with_settings(org_contents, global_settings)?;
|
2023-12-27 08:57:56 -05:00
|
|
|
let to_wasm_context = ToWasmContext::new(org_contents);
|
2023-12-27 09:31:54 -05:00
|
|
|
let wasm_parsed = rust_parsed
|
|
|
|
.to_wasm(to_wasm_context)
|
|
|
|
.map_err(|_e| "Failed to convert to wasm.")?;
|
2023-12-27 08:46:18 -05:00
|
|
|
let org_sexp = emacs_parse_anonymous_org_document(org_contents, global_settings).await?;
|
2023-12-27 08:57:56 -05:00
|
|
|
let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?;
|
2023-12-26 18:55:28 -05:00
|
|
|
|
|
|
|
if !silent {
|
|
|
|
println!("{}\n\n\n", org_contents);
|
2023-12-27 08:57:56 -05:00
|
|
|
println!("{}", org_sexp);
|
2023-12-26 18:55:28 -05:00
|
|
|
println!("{:#?}", rust_parsed);
|
2023-12-27 08:57:56 -05:00
|
|
|
println!("{}", serde_json::to_string(&wasm_parsed)?);
|
2023-12-26 18:55:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We do the diffing after printing out both parsed forms in case the diffing panics
|
2023-12-27 09:31:54 -05:00
|
|
|
let diff_result = wasm_compare_document(&parsed_sexp, &wasm_parsed)?;
|
2023-12-27 10:52:59 -05:00
|
|
|
if !silent {
|
|
|
|
diff_result.print(org_contents)?;
|
|
|
|
}
|
2023-12-26 18:55:28 -05:00
|
|
|
|
2023-12-27 10:52:59 -05:00
|
|
|
if diff_result.is_bad() {
|
|
|
|
return Ok(false);
|
|
|
|
} else if !silent {
|
|
|
|
// println!(
|
|
|
|
// "{color}Entire document passes.{reset}",
|
|
|
|
// color = WasmDiffResult::foreground_color(0, 255, 0),
|
|
|
|
// reset = WasmDiffResult::reset_color(),
|
|
|
|
// );
|
|
|
|
}
|
2023-12-26 18:55:28 -05:00
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
}
|