scanit/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std::error::Error;
use regex::Regex;
use jwalk::WalkDir;

pub fn find_files(pattern: &str, directory: &str,show_hidden:bool) -> Result<(), Box<dyn Error + 'static>> {
   
    let mut matches = Vec::new();
    let re = Regex::new(pattern)?;

    for entry in WalkDir::new(directory).skip_hidden(!show_hidden) {
        match entry {
            Ok(e) => {
                let path = e.path().to_string_lossy().into_owned();
                if re.is_match(&path) {
                    matches.push(path);
                }
            },
            Err(e) if e.to_string().contains("Permission denied") => {
                //skipped += 1;
                continue
            },
            Err(e) => return Err(Box::new(e))
        }
    }

    for path in &matches {
        println!("{}", path);
    }
   
    
    Ok(())
}