diff --git a/CHANGELOG.md b/CHANGELOG.md index 61ee6a1..ae885a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.3.1 + +- Program now sets the exit code properly instead of printing it out +- Rather than printing the compiled document to stdout, correctly writes it to files +- Program now prints long help when no arguments are passed in + # 0.3.0 - Added support for string/number constant interpolation diff --git a/Cargo.lock b/Cargo.lock index b7b6f0e..830bab0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,7 +263,7 @@ dependencies = [ [[package]] name = "kmonadx" -version = "0.3.0" +version = "0.3.1" dependencies = [ "ahash", "bimap", diff --git a/Cargo.toml b/Cargo.toml index f7a5cdf..409a01c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kmonadx" -version = "0.3.0" +version = "0.3.1" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index 0b0c973..22684d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,18 @@ use kmonadx::kbdx::diagnostic::DiagnosticAggregator; use std::cell::UnsafeCell; -fn main() -> Result<()> { +// Because std::process::exit terminates the program without running destructors, we need a helper +// function to wrap all the functionality so that there is nothing left to cleanup in the actual +// main function. +// This function returns the exit code. +fn _main() -> Result { let cli = CLI::from_args(); + if cli.filenames.is_empty() { + CLI::clap().print_long_help()?; + return Ok(1); + } + // https://github.com/yaahc/color-eyre/issues/83 // We do not want the "Backtrace has been omitted" message to display color_eyre::config::HookBuilder::default() @@ -26,7 +35,7 @@ fn main() -> Result<()> { // errors let files_content = UnsafeCell::new(Vec::with_capacity(cli.filenames.len())); - for file_name in cli.filenames { + for mut file_name in cli.filenames { let file_contents = std::fs::read_to_string(&file_name) .wrap_err(format!("Unable to read file: {:?}", file_name)) .suggestion("Please specify a file that exists")?; @@ -37,7 +46,7 @@ fn main() -> Result<()> { files_content_ref.last().unwrap() }; - let file_handle = diagnostics.new_file( + let mut file_handle = diagnostics.new_file( file_name .to_str() .ok_or_else(|| eyre!("Filename is not valid unicode!"))? @@ -45,6 +54,17 @@ fn main() -> Result<()> { file_contents, ); + // OPTIMIZE: shouldn't have to read the file before handling this case + if let Some(ext) = file_name.extension() { + if ext == "kbd" { + file_handle.error(format!( + "Cannot compile '{:?}'; file already has .kbd extension!", + file_name + )); + continue; + } + } + let mut parser = Parser::new(file_contents, file_handle); if cli.debug_output { @@ -55,10 +75,13 @@ fn main() -> Result<()> { match compiler.compile_string() { Ok(string) => { if !cli.check { - println!("{}", string); + // replace kbdx extension with kbd or append .kbd if there is no + // extension + file_name.set_extension("kbd"); + std::fs::write(file_name, string)?; } - }, - Err(_) => break + } + Err(_) => break, } } Err(_) => break, @@ -75,7 +98,10 @@ fn main() -> Result<()> { // are no more destructors left to run. // END QUOTE let exit_code: usize = diagnostics.emit_all()?.into(); - eprintln!("Exit code would be {}", exit_code); + Ok(exit_code) +} - Ok(()) +fn main() -> Result<()> { + let exit_code = _main()?; + std::process::exit(exit_code as i32) }