2020-09-27 03:52:13 +13:00
|
|
|
use crate::common::Common;
|
|
|
|
use crate::common_messages::Messages;
|
2020-10-15 05:41:37 +13:00
|
|
|
use std::path::Path;
|
2020-09-27 03:52:13 +13:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2020-10-10 09:32:08 +13:00
|
|
|
#[derive(Default)]
|
2020-09-27 03:52:13 +13:00
|
|
|
pub struct ExcludedItems {
|
|
|
|
pub items: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExcludedItems {
|
2020-10-10 09:32:08 +13:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2020-10-07 21:34:15 +13:00
|
|
|
/// Setting excluded items which needs to contains * wildcard
|
2020-09-27 03:52:13 +13:00
|
|
|
/// Are a lot of slower than absolute path, so it should be used to heavy
|
|
|
|
pub fn set_excluded_items(&mut self, mut excluded_items: String, text_messages: &mut Messages) {
|
|
|
|
let start_time: SystemTime = SystemTime::now();
|
|
|
|
|
|
|
|
if excluded_items.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
excluded_items = excluded_items.replace("\"", "");
|
|
|
|
let expressions: Vec<String> = excluded_items.split(',').map(String::from).collect();
|
|
|
|
let mut checked_expressions: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
for expression in expressions {
|
|
|
|
let expression: String = expression.trim().to_string();
|
|
|
|
|
|
|
|
if expression == "" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if expression == "DEFAULT" {
|
|
|
|
// TODO add more files by default
|
|
|
|
checked_expressions.push("*/.git/*".to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if !expression.contains('*') {
|
|
|
|
text_messages.warnings.push("Excluded Items Warning: Wildcard * is required in expression, ignoring ".to_string() + expression.as_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
checked_expressions.push(expression);
|
|
|
|
}
|
|
|
|
self.items = checked_expressions;
|
|
|
|
Common::print_time(start_time, SystemTime::now(), "set_excluded_items".to_string());
|
|
|
|
}
|
2020-10-15 05:41:37 +13:00
|
|
|
|
|
|
|
/// Checks whether a specified path is excluded from searching
|
|
|
|
pub fn is_excluded(&self, path: impl AsRef<Path>) -> bool {
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
let path = Common::normalize_windows_path(path);
|
|
|
|
|
|
|
|
for expression in &self.items {
|
|
|
|
if Common::regex_check(expression, &path) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|