pub mod file_operations;
pub mod dir_operations;
pub mod path_manipulation;
use std::collections::HashSet;
use std::fs;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::fs::OpenOptions;
pub struct DirectorySearcher{
blacklist: HashSet<String>,
blacklist_file_write: File,
}
impl DirectorySearcher {
pub fn new() -> Self {
let file= match OpenOptions::new().append(true).open("src\\blacklist.txt") {
Ok(file) => file,
Err(_e) => file_operations::new_file_return_append()
};
DirectorySearcher{
blacklist: HashSet::new(),
blacklist_file_write: file,
}
}
pub fn update_blacklist(&mut self) {
let file: File = match OpenOptions::new().read(true).open("src\\blacklist.txt") {
Ok(file) => file,
Err(_e) => file_operations::new_file_return()
};
let reader: BufReader<File> = BufReader::new(file);
self.blacklist.clear();
for line in reader.lines() {
if let Ok(line) = line {
self.blacklist.insert(line);
}
}
}
pub(crate) fn search(&mut self, root_path: &str, be_searched_: &str) -> Vec<String> {
if self.blacklist.contains(&root_path.to_string()) {
return Vec::new();
}
let be_searched: String = be_searched_.to_lowercase();
let paths: Vec<String> = self.get_paths(&root_path.trim());
let mut matchers: Vec<String> = Vec::new();
let mut dirs: Vec<String> = Vec::new();
for i in paths.iter() {
if dir_operations::is_dir(i) {
dirs.push(i.to_string());
}
let breakers: Vec<&str> = i.split("\\").collect();
if breakers[breakers.len() - 1].contains(&be_searched) {
matchers.push(i.to_string());
}
}
dirs.into_iter().for_each(|dir| matchers.append(&mut self.search(&dir, &be_searched)));
matchers
}
fn get_paths(&mut self, path: &str) -> Vec<String> {
if let Ok(paths) = fs::read_dir(path) {
let mut paths_: Vec<String> = Vec::new();
for path in paths {
paths_.push(path.unwrap().path().display().to_string());
}
return paths_;
} else {
let _ = self.blacklist_file_write.write(path.as_bytes()).unwrap();
let _ = self.blacklist_file_write.write(b"\n").unwrap();
return Vec::new();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn is_working() {
let mut searcher = DirectorySearcher::new();
searcher.search("F:", "java");
}
}