2023-03-23 23:35:32 +00:00
|
|
|
#![feature(round_char_boundary)]
|
2023-09-02 16:15:57 +00:00
|
|
|
#![feature(exact_size_is_empty)]
|
2023-08-17 01:06:22 +00:00
|
|
|
use std::io::Read;
|
2023-09-02 16:15:57 +00:00
|
|
|
use std::path::Path;
|
2023-08-10 22:46:19 +00:00
|
|
|
|
2023-09-03 16:52:09 +00:00
|
|
|
use ::organic::parser::parse;
|
2023-08-11 00:21:39 +00:00
|
|
|
#[cfg(feature = "compare")]
|
2023-08-10 22:46:19 +00:00
|
|
|
use organic::compare_document;
|
2023-08-11 00:21:39 +00:00
|
|
|
#[cfg(feature = "compare")]
|
2023-09-02 16:15:57 +00:00
|
|
|
use organic::emacs_parse_anonymous_org_document;
|
|
|
|
#[cfg(feature = "compare")]
|
|
|
|
use organic::emacs_parse_file_org_document;
|
2023-08-11 00:21:39 +00:00
|
|
|
#[cfg(feature = "compare")]
|
2023-08-25 07:04:07 +00:00
|
|
|
use organic::get_emacs_version;
|
|
|
|
#[cfg(feature = "compare")]
|
|
|
|
use organic::get_org_mode_version;
|
|
|
|
#[cfg(feature = "compare")]
|
2023-08-10 22:46:19 +00:00
|
|
|
use organic::parser::sexp::sexp_with_padding;
|
2023-04-23 01:45:18 +00:00
|
|
|
|
2023-08-16 20:05:16 +00:00
|
|
|
#[cfg(feature = "tracing")]
|
2023-04-11 19:08:46 +00:00
|
|
|
use crate::init_tracing::init_telemetry;
|
2023-08-16 20:05:16 +00:00
|
|
|
#[cfg(feature = "tracing")]
|
2023-04-11 19:08:46 +00:00
|
|
|
use crate::init_tracing::shutdown_telemetry;
|
2023-08-16 20:05:16 +00:00
|
|
|
#[cfg(feature = "tracing")]
|
2023-04-11 19:08:46 +00:00
|
|
|
mod init_tracing;
|
2022-07-16 03:26:49 +00:00
|
|
|
|
2023-08-11 01:22:06 +00:00
|
|
|
#[cfg(not(feature = "tracing"))]
|
2022-07-16 03:26:49 +00:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2023-08-11 01:22:06 +00:00
|
|
|
main_body()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "tracing")]
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let rt = tokio::runtime::Runtime::new()?;
|
2023-08-16 20:05:16 +00:00
|
|
|
let result = rt.block_on(async {
|
|
|
|
init_telemetry()?;
|
|
|
|
let main_body_result = main_body();
|
|
|
|
shutdown_telemetry()?;
|
|
|
|
main_body_result
|
|
|
|
});
|
2023-08-11 01:22:06 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:05:16 +00:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-11 01:22:06 +00:00
|
|
|
fn main_body() -> Result<(), Box<dyn std::error::Error>> {
|
2023-09-04 15:45:10 +00:00
|
|
|
let args = std::env::args().skip(1);
|
2023-09-02 16:15:57 +00:00
|
|
|
if args.is_empty() {
|
|
|
|
let org_contents = read_stdin_to_string()?;
|
|
|
|
run_anonymous_parse(org_contents)
|
|
|
|
} else {
|
2023-09-04 15:45:10 +00:00
|
|
|
for arg in args {
|
2023-09-02 16:15:57 +00:00
|
|
|
run_parse_on_file(arg)?
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-08-17 01:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_stdin_to_string() -> Result<String, Box<dyn std::error::Error>> {
|
|
|
|
let mut stdin_contents = String::new();
|
|
|
|
std::io::stdin()
|
|
|
|
.lock()
|
|
|
|
.read_to_string(&mut stdin_contents)?;
|
|
|
|
Ok(stdin_contents)
|
2022-07-16 03:26:49 +00:00
|
|
|
}
|
2023-08-10 22:46:19 +00:00
|
|
|
|
2023-08-11 00:21:39 +00:00
|
|
|
#[cfg(feature = "compare")]
|
2023-09-02 16:15:57 +00:00
|
|
|
fn run_anonymous_parse<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
2023-08-29 18:40:58 +00:00
|
|
|
let org_contents = org_contents.as_ref();
|
2023-09-02 16:15:57 +00:00
|
|
|
eprintln!("Using emacs version: {}", get_emacs_version()?.trim());
|
|
|
|
eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim());
|
2023-09-04 21:29:50 +00:00
|
|
|
let rust_parsed = parse(org_contents)?;
|
2023-09-02 16:15:57 +00:00
|
|
|
let org_sexp = emacs_parse_anonymous_org_document(org_contents)?;
|
2023-08-10 22:46:19 +00:00
|
|
|
let (_remaining, parsed_sexp) =
|
2023-08-25 07:04:07 +00:00
|
|
|
sexp_with_padding(org_sexp.as_str()).map_err(|e| e.to_string())?;
|
2023-08-10 22:46:19 +00:00
|
|
|
|
2023-08-29 18:40:58 +00:00
|
|
|
println!("{}\n\n\n", org_contents);
|
2023-08-10 22:46:19 +00:00
|
|
|
println!("{}", org_sexp);
|
|
|
|
println!("{:#?}", rust_parsed);
|
|
|
|
|
|
|
|
// We do the diffing after printing out both parsed forms in case the diffing panics
|
2023-08-16 20:37:14 +00:00
|
|
|
let diff_result = compare_document(&parsed_sexp, &rust_parsed)?;
|
2023-08-29 18:40:58 +00:00
|
|
|
diff_result.print(org_contents)?;
|
2023-08-10 22:46:19 +00:00
|
|
|
|
|
|
|
if diff_result.is_bad() {
|
|
|
|
Err("Diff results do not match.")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-08-11 00:21:39 +00:00
|
|
|
|
|
|
|
#[cfg(not(feature = "compare"))]
|
2023-09-02 16:15:57 +00:00
|
|
|
fn run_anonymous_parse<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
2023-08-25 07:04:07 +00:00
|
|
|
eprintln!(
|
|
|
|
"This program was built with compare disabled. Only parsing with organic, not comparing."
|
|
|
|
);
|
2023-09-04 21:29:50 +00:00
|
|
|
let rust_parsed = parse(org_contents.as_ref())?;
|
2023-08-21 05:26:52 +00:00
|
|
|
println!("{:#?}", rust_parsed);
|
2023-08-11 00:21:39 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-09-02 16:15:57 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "compare")]
|
|
|
|
fn run_parse_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());
|
2023-09-05 01:46:40 +00:00
|
|
|
let parent_directory = org_path
|
|
|
|
.parent()
|
|
|
|
.ok_or("Should be contained inside a directory.")?;
|
2023-09-02 16:15:57 +00:00
|
|
|
let org_contents = std::fs::read_to_string(org_path)?;
|
|
|
|
let org_contents = org_contents.as_str();
|
2023-09-05 01:46:40 +00:00
|
|
|
let global_settings = GlobalSettings {
|
|
|
|
radio_targets: Vec::new(),
|
|
|
|
file_access: &LocalFileAccessInterface {
|
|
|
|
working_directory: Some(parent_directory.to_path_buf()),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let rust_parsed = parse_with_settings(org_contents, &global_settings)?;
|
2023-09-02 16:15:57 +00:00
|
|
|
let org_sexp = emacs_parse_file_org_document(org_path)?;
|
|
|
|
let (_remaining, parsed_sexp) =
|
|
|
|
sexp_with_padding(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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "compare"))]
|
|
|
|
fn run_parse_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> {
|
2023-09-05 01:46:40 +00:00
|
|
|
use organic::parser::parse_with_settings;
|
|
|
|
use organic::GlobalSettings;
|
|
|
|
use organic::LocalFileAccessInterface;
|
|
|
|
|
2023-09-02 16:15:57 +00:00
|
|
|
let org_path = org_path.as_ref();
|
|
|
|
eprintln!(
|
|
|
|
"This program was built with compare disabled. Only parsing with organic, not comparing."
|
|
|
|
);
|
2023-09-05 01:46:40 +00:00
|
|
|
let parent_directory = org_path
|
|
|
|
.parent()
|
|
|
|
.ok_or("Should be contained inside a directory.")?;
|
2023-09-02 16:15:57 +00:00
|
|
|
let org_contents = std::fs::read_to_string(org_path)?;
|
|
|
|
let org_contents = org_contents.as_str();
|
2023-09-05 01:46:40 +00:00
|
|
|
let global_settings = GlobalSettings {
|
|
|
|
radio_targets: Vec::new(),
|
|
|
|
file_access: &LocalFileAccessInterface {
|
|
|
|
working_directory: Some(parent_directory.to_path_buf()),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let rust_parsed = parse_with_settings(org_contents, &global_settings)?;
|
2023-09-02 16:15:57 +00:00
|
|
|
println!("{:#?}", rust_parsed);
|
|
|
|
Ok(())
|
|
|
|
}
|