Skip to content

Commit

Permalink
Print statistics about the scan before exit
Browse files Browse the repository at this point in the history
This helps the user validate if the tool ran at all and if it seemed
to do anything useful at all
  • Loading branch information
faern committed Nov 29, 2024
1 parent 0113b37 commit fe042a9
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,46 @@ fn main() -> anyhow::Result<()> {
default_config,
};

let mut num_files_scanned: u64 = 0;
let mut global_scan_stats = ScanStats {
num_unicode_code_points: 0,
num_rule_violations: 0,
};
for path in args.paths {
for entry in walkdir::WalkDir::new(path) {
match entry {
Err(err) => eprintln!("{:}", err),
Ok(entry) if entry.file_type().is_file() => {
let entry_path = entry.path();
dispatcher.user_config = get_user_config(entry_path)?;
check_file(&dispatcher, entry_path);
if let Some(scan_stats) = check_file(&dispatcher, entry_path) {
num_files_scanned += 1;
global_scan_stats.num_unicode_code_points +=
scan_stats.num_unicode_code_points;
global_scan_stats.num_rule_violations += scan_stats.num_rule_violations;
}
}
Ok(_) => {}
}
}
}

println!(
"Scanned {} unicode code points in {} files, resulting in {} rule violations",
global_scan_stats.num_unicode_code_points,
num_files_scanned,
global_scan_stats.num_rule_violations,
);
Ok(())
}

fn check_file(dispatcher: &RuleDispatcher, path: &Path) {
/// Scans a single file at `path` using the rules defined in `dispatcher`.
///
/// If the file was actually scanned (matched a language in the rule dispatcher),
/// then stats about the scan are returned.
fn check_file(dispatcher: &RuleDispatcher, path: &Path) -> Option<ScanStats> {
let Some(lang) = dispatcher.language(path) else {
return;
return None;
};
let filename = path.display().to_string();
let src = fs::read_to_string(path).unwrap();
Expand All @@ -197,6 +218,12 @@ fn check_file(dispatcher: &RuleDispatcher, path: &Path) {
.set_language(&lang.grammar())
.expect("Error loading grammar");
let tree = parser.parse(&src, None).unwrap();

let mut scan_stats = ScanStats {
num_unicode_code_points: 0,
num_rule_violations: 0,
};

if tree.root_node().has_error() {
let mut labels = Vec::new();
if log::log_enabled!(log::Level::Debug) {
Expand All @@ -221,6 +248,7 @@ fn check_file(dispatcher: &RuleDispatcher, path: &Path) {
print!("{:?}", report);
}
for (off, ch) in src.char_indices() {
scan_stats.num_unicode_code_points += 1;
let node = tree
.root_node()
.named_descendant_for_byte_range(off, off + ch.len_utf8())
Expand All @@ -242,8 +270,19 @@ fn check_file(dispatcher: &RuleDispatcher, path: &Path) {
node.kind()
)
.with_source_code(named_source.clone());
scan_stats.num_rule_violations += 1;
print!("{:?}", report);
}

Some(scan_stats)
}

/// Statistics about unicop scans.
struct ScanStats {
/// Number of unicode code points ([`char`]s) that the scan processed.
pub num_unicode_code_points: u64,
/// Number of rule violations encountered during the scan.
pub num_rule_violations: u64,
}

fn get_user_config(path: &Path) -> anyhow::Result<Option<Config>> {
Expand Down

0 comments on commit fe042a9

Please sign in to comment.