scancode_rust/scanner/
count.rs1use crate::utils::file::is_path_excluded;
2use glob::Pattern;
3use std::fs;
4use std::path::Path;
5
6pub fn count<P: AsRef<Path>>(
7 path: P,
8 max_depth: usize,
9 exclude_patterns: &[Pattern],
10) -> std::io::Result<(usize, usize, usize)> {
11 let path = path.as_ref();
12
13 if is_path_excluded(path, exclude_patterns) {
14 return Ok((0, 0, 1));
15 }
16
17 let mut files_count = 0;
18 let mut dirs_count = 1; let mut excluded_count = 0;
20
21 for entry in fs::read_dir(path)? {
23 let entry = entry?;
24 let entry_path = entry.path();
25
26 if is_path_excluded(&entry_path, exclude_patterns) {
27 excluded_count += 1;
28 continue;
29 }
30
31 let metadata = entry.metadata()?;
32 if metadata.is_file() {
33 files_count += 1;
34 } else if metadata.is_dir() {
35 dirs_count += 1;
36
37 if max_depth > 0 {
39 let (sub_files, sub_dirs, sub_excluded) =
40 count(&entry_path, max_depth - 1, exclude_patterns)?;
41
42 files_count += sub_files;
43 dirs_count += sub_dirs - 1; excluded_count += sub_excluded;
45 }
46 }
47 }
48
49 Ok((files_count, dirs_count, excluded_count))
50}