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
33
34
35
36
37
use std::error::Error;

use crossbeam::channel::Sender;
use fancy_regex::Regex;
use walkdir::WalkDir;

/// The crawler type that goes through a (sub-)tree of a file/directory (inode)
/// and recursively gets files.
pub struct Crawler<'s> {
    matcher: &'s Regex,
}
impl<'s> Crawler<'s> {
    /// Constructor for the crawler.
    /// Accepting the regex which shall match the (fully qualified, relative)
    /// file names.
    pub fn new(matcher: &'s Regex) -> Self {
        Self { matcher }
    }

    /// Executing the crawl, sending the results during the operation through
    /// the sender argument.
    pub fn crawl(&self, path: &str, sender: Sender<String>) -> Result<(), Box<dyn Error>> {
        for entry in WalkDir::new(path)
            .into_iter()
            .filter_map(Result::ok)
            .filter(|e| !e.file_type().is_dir())
        {
            let entry_path = entry.into_path();
            let entry_path_str = entry_path.to_str().unwrap();
            if !self.matcher.is_match(entry_path_str)? {
                continue;
            }
            sender.send(entry_path_str.to_owned())?;
        }
        Ok(())
    }
}