refactor(cli): print the final result; fix plural on number of files

This commit is contained in:
Boshen 2023-08-21 10:44:32 +08:00
parent 8e6f84c40d
commit 03df8fcc68
No known key found for this signature in database
GPG key ID: 234DA6A7079C6801
4 changed files with 91 additions and 97 deletions

View file

@ -38,4 +38,3 @@ ignore = { workspace = true, features = ["simd-accel"] }
miette = { workspace = true, features = ["fancy-no-backtrace"] }
rayon = { workspace = true }
bpaf = { workspace = true, features = ["derive", "autocomplete", "bright-color"] }
# git2 = { version = "0.16.1", default_features = false }

View file

@ -1,14 +1,10 @@
// mod git;
mod command;
mod lint;
mod result;
mod runner;
mod type_check;
mod walk;
pub use crate::{
command::*,
lint::LintRunner,
runner::{CliRunResult, Runner},
type_check::TypeCheckRunner,
walk::Walk,
command::*, lint::LintRunner, result::CliRunResult, runner::Runner, type_check::TypeCheckRunner,
};

View file

@ -0,0 +1,88 @@
use std::{
path::PathBuf,
process::{ExitCode, Termination},
time::Duration,
};
#[derive(Debug)]
pub enum CliRunResult {
None,
IOError(crate::lint::Error),
PathNotFound {
paths: Vec<PathBuf>,
},
LintResult {
duration: Duration,
number_of_rules: usize,
number_of_files: usize,
number_of_warnings: usize,
number_of_errors: usize,
max_warnings_exceeded: bool,
},
TypeCheckResult {
duration: Duration,
number_of_diagnostics: usize,
},
}
impl Termination for CliRunResult {
fn report(self) -> ExitCode {
match self {
Self::None => ExitCode::from(0),
Self::PathNotFound { paths } => {
println!("Path {paths:?} does not exist.");
ExitCode::from(1)
}
Self::IOError(e) => {
println!("IO Error: {e}");
ExitCode::from(1)
}
Self::LintResult {
duration,
number_of_rules,
number_of_files,
number_of_warnings,
number_of_errors,
max_warnings_exceeded,
} => {
let ms = duration.as_millis();
let threads = rayon::current_num_threads();
let number_of_diagnostics = number_of_warnings + number_of_errors;
if number_of_diagnostics > 0 {
println!();
}
let s = if number_of_files == 1 { "" } else { "s" };
println!(
"Finished in {ms}ms on {number_of_files} file{s} with {number_of_rules} rules using {threads} threads."
);
if max_warnings_exceeded {
println!("Exceeded maximum number of warnings. Found {number_of_warnings}.");
return ExitCode::from(1);
}
println!(
"Found {number_of_warnings} warning{} and {number_of_errors} error{}.",
if number_of_warnings == 1 { "" } else { "s" },
if number_of_errors == 1 { "" } else { "s" }
);
let exit_code = u8::from(number_of_diagnostics > 0);
ExitCode::from(exit_code)
}
Self::TypeCheckResult { duration, number_of_diagnostics } => {
let ms = duration.as_millis();
println!("Finished in {ms}ms.");
if number_of_diagnostics > 0 {
println!("Found {number_of_diagnostics} errors.");
return ExitCode::from(1);
}
ExitCode::from(0)
}
}
}
}

View file

@ -1,7 +1,4 @@
use std::{
path::PathBuf,
process::{ExitCode, Termination},
};
use crate::CliRunResult;
/// A trait for exposing functionality to the CLI.
pub trait Runner: Send + Sync {
@ -12,89 +9,3 @@ pub trait Runner: Send + Sync {
/// Executes the runner, providing some result to the CLI.
fn run(self) -> CliRunResult;
}
#[derive(Debug)]
pub enum CliRunResult {
None,
IOError(crate::lint::Error),
PathNotFound {
paths: Vec<PathBuf>,
},
LintResult {
duration: std::time::Duration,
number_of_rules: usize,
number_of_files: usize,
number_of_warnings: usize,
number_of_errors: usize,
max_warnings_exceeded: bool,
},
TypeCheckResult {
duration: std::time::Duration,
number_of_diagnostics: usize,
},
}
impl Termination for CliRunResult {
fn report(self) -> ExitCode {
match self {
Self::None => ExitCode::from(0),
Self::PathNotFound { paths } => {
println!("Path {paths:?} does not exist.");
ExitCode::from(1)
}
Self::IOError(e) => {
println!("IO Error: {e}");
ExitCode::from(1)
}
Self::LintResult {
duration,
number_of_rules,
number_of_files,
number_of_warnings,
number_of_errors,
max_warnings_exceeded,
} => {
let ms = duration.as_millis();
let threads = rayon::current_num_threads();
let number_of_diagnostics = number_of_warnings + number_of_errors;
if number_of_diagnostics > 0 {
println!();
}
println!(
"Finished in {ms}ms on {number_of_files} files with {number_of_rules} rules using {threads} threads."
);
if max_warnings_exceeded {
println!("Exceeded maximum number of warnings. Found {number_of_warnings}.");
return ExitCode::from(1);
}
if number_of_diagnostics > 0 {
let warnings = if number_of_warnings == 1 { "warning" } else { "warnings" };
let errors = if number_of_errors == 1 { "error" } else { "errors" };
println!(
"Found {number_of_warnings} {warnings} and {number_of_errors} {errors}."
);
return ExitCode::from(1);
}
// eslint does not print anything after success, so we do the same.
// It is also standard to not print anything after success in the *nix world.
ExitCode::from(0)
}
Self::TypeCheckResult { duration, number_of_diagnostics } => {
let ms = duration.as_millis();
println!("Finished in {ms}ms.");
if number_of_diagnostics > 0 {
println!("Found {number_of_diagnostics} errors.");
return ExitCode::from(1);
}
ExitCode::from(0)
}
}
}
}