Skip to content

Commit

Permalink
fix: should exit with non-zero on error (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC authored Aug 18, 2024
1 parent 11ce1d3 commit 8e5a72f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,30 @@ impl Checker {
}
}

/// Print the errors that are found in a human-readable way.
pub(crate) fn report_to_user(&self) {
println!("Errors Found:");

/// Return true if there is no error.
pub(crate) fn has_error(&self) -> bool {
// SAFETY:
// It is safe to directly modify the global static variable as there is only 1 thread.
unsafe {
let n_errors: usize = ERROR_STORAGE
let n_errors: usize = unsafe {
ERROR_STORAGE
.iter()
.map(|(_key, errors)| errors.len())
.sum();
.sum()
};

n_errors != 0
}

if n_errors == 0 {
println!(" No error");
/// Print the errors that are found in a human-readable way.
pub(crate) fn report_to_user(&self) {
// SAFETY:
// It is safe to directly modify the global static variable as there is only 1 thread.
unsafe {
if !self.has_error() {
println!("No error found!");
} else {
println!("Errors Found:");

for (rule, errors) in ERROR_STORAGE.iter() {
println!(" {}", rule);
for (key, opt_error_msg) in errors {
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use serde_yaml_ng::Value as Yaml;
use std::env::args;
use std::fs::File;

const EXIT_CODE_ON_ERROR: i32 = 1;

fn main() {
let file_name = args()
.nth(1)
Expand All @@ -33,4 +35,8 @@ fn main() {
checker.check(&localized_texts);

checker.report_to_user();

if checker.has_error() {
std::process::exit(EXIT_CODE_ON_ERROR);
}
}

0 comments on commit 8e5a72f

Please sign in to comment.