-
-
Notifications
You must be signed in to change notification settings - Fork 718
/
Copy pathcommon_items.rs
66 lines (57 loc) · 2.3 KB
/
common_items.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::common::Common;
use crate::common_messages::Messages;
use std::path::Path;
use std::time::SystemTime;
#[derive(Default)]
pub struct ExcludedItems {
pub items: Vec<String>,
}
impl ExcludedItems {
pub fn new() -> Self {
Default::default()
}
/// Setting excluded items which needs to contains * wildcard
/// 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.is_empty() {
continue;
}
if expression == "DEFAULT" {
if cfg!(target_family = "unix") {
checked_expressions.push("*/.git/*,*/node_modules/*,*/lost+found/*,*/Trash/*,*/.Trash-*/*,*/snap/*".to_string());
}
if cfg!(target_family = "windows") {
checked_expressions.push("*/.git/*,*/node_modules/*,*/lost+found/*,*:/windows/*".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());
}
/// 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
}
}