Remove dependency on files for running compare.

The tests still use files since they get the test name from a file but compare does the same action via stdin so it can operator on any org source.
This commit is contained in:
Tom Alexander
2023-08-16 21:06:22 -04:00
parent 4776898894
commit 74f4aa8d33
4 changed files with 50 additions and 33 deletions

View File

@@ -1,22 +1,22 @@
use std::path::Path;
use std::process::Command;
pub fn emacs_parse_org_document<'a, C>(file_path: C) -> Result<String, Box<dyn std::error::Error>>
pub fn emacs_parse_org_document<C>(file_contents: C) -> Result<String, Box<dyn std::error::Error>>
where
C: AsRef<Path>,
C: AsRef<str>,
{
let elisp_script = r#"(progn
let escaped_file_contents = escape_elisp_string(file_contents);
let elisp_script = format!(r#"(progn
(erase-buffer)
(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 proc = cmd
.arg("-q")
.arg("--no-site-file")
.arg("--no-splash")
.arg("--batch")
.arg("--insert")
.arg(file_path.as_ref().as_os_str())
.arg("--eval")
.arg(elisp_script);
let out = proc.output()?;
@@ -24,3 +24,25 @@ where
let org_sexp = out.stderr;
Ok(String::from_utf8(org_sexp)?)
}
fn escape_elisp_string<C>(file_contents: C) -> String
where
C: AsRef<str>,
{
let source = file_contents.as_ref();
let source_len = source.len();
// We allocate a string 10% larger than the source to account for escape characters. Without this, we would have more allocations during processing.
let mut output = String::with_capacity(source_len + (source_len / 10));
for c in source.chars() {
match c {
'"' | '\\' => {
output.push('\\');
output.push(c);
}
_ => {
output.push(c);
}
}
}
output
}