organic/src/main.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

2023-03-23 23:35:32 +00:00
#![feature(round_char_boundary)]
#[cfg(feature = "compare")]
use std::io::Read;
2023-08-11 00:21:39 +00:00
#[cfg(feature = "compare")]
use ::organic::parser::document;
2023-08-11 00:21:39 +00:00
#[cfg(feature = "compare")]
use organic::compare_document;
2023-08-11 00:21:39 +00:00
#[cfg(feature = "compare")]
use organic::emacs_parse_org_document;
2023-08-11 00:21:39 +00:00
#[cfg(feature = "compare")]
use organic::parser::sexp::sexp_with_padding;
#[cfg(feature = "tracing")]
2023-04-11 19:08:46 +00:00
use crate::init_tracing::init_telemetry;
#[cfg(feature = "tracing")]
2023-04-11 19:08:46 +00:00
use crate::init_tracing::shutdown_telemetry;
#[cfg(feature = "tracing")]
2023-04-11 19:08:46 +00:00
mod init_tracing;
2022-07-16 03:26:49 +00:00
#[cfg(not(feature = "tracing"))]
2022-07-16 03:26:49 +00:00
fn main() -> Result<(), Box<dyn std::error::Error>> {
main_body()
}
#[cfg(feature = "tracing")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rt = tokio::runtime::Runtime::new()?;
let result = rt.block_on(async {
init_telemetry()?;
let main_body_result = main_body();
shutdown_telemetry()?;
main_body_result
});
result
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn main_body() -> Result<(), Box<dyn std::error::Error>> {
2023-08-17 04:10:14 +00:00
#[cfg(not(feature = "compare"))]
let org_contents = "";
#[cfg(feature = "compare")]
let org_contents = read_stdin_to_string()?;
run_compare(org_contents)
}
2023-08-17 04:10:14 +00:00
#[cfg(feature = "compare")]
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-11 00:21:39 +00:00
#[cfg(feature = "compare")]
fn run_compare<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
let (remaining, rust_parsed) = document(org_contents.as_ref()).expect("Org Parse failure");
let org_sexp = emacs_parse_org_document(org_contents.as_ref())?;
let (_remaining, parsed_sexp) =
sexp_with_padding(org_sexp.as_str()).expect("Sexp Parse failure");
println!("{}\n\n\n", org_contents.as_ref());
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)?;
diff_result.print()?;
if diff_result.is_bad() {
Err("Diff results do not match.")?;
}
if remaining != "" {
Err(format!("There was unparsed text remaining: {}", remaining))?;
}
Ok(())
}
2023-08-11 00:21:39 +00:00
#[cfg(not(feature = "compare"))]
2023-08-17 04:10:14 +00:00
fn run_compare<P: AsRef<str>>(_org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
2023-08-11 00:21:39 +00:00
println!("This program was built with compare disabled. Doing nothing.");
Ok(())
}