Skip to content

Commit

Permalink
Merge pull request #10 from srithon/topic-7-cli-write-file
Browse files Browse the repository at this point in the history
Fix documented CLI behavior
  • Loading branch information
srithon authored Jan 13, 2022
2 parents 1fee175 + 3310211 commit 9aa0f53
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
42 changes: 34 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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()
Expand All @@ -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")?;
Expand All @@ -37,14 +46,25 @@ 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!"))?
.to_owned(),
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 {
Expand All @@ -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,
Expand All @@ -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)
}

0 comments on commit 9aa0f53

Please sign in to comment.