-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsearch_config.rs
90 lines (80 loc) · 2.27 KB
/
search_config.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use anyhow::Result;
use ignore::{
overrides::{Override, OverrideBuilder},
types::{Types, TypesBuilder},
};
use std::path::PathBuf;
#[derive(Clone)]
pub struct SearchConfig {
pub pattern: String,
pub paths: Vec<PathBuf>,
pub case_insensitive: bool,
pub case_smart: bool,
pub overrides: Override,
pub types: Types,
pub search_hidden: bool,
pub follow_links: bool,
pub word_regexp: bool,
}
impl SearchConfig {
pub fn from(pattern: String, paths: Vec<PathBuf>) -> Result<Self> {
let mut builder = TypesBuilder::new();
builder.add_defaults();
let types = builder.build()?;
Ok(Self {
pattern,
paths,
case_insensitive: false,
case_smart: false,
overrides: Override::empty(),
types,
search_hidden: false,
follow_links: false,
word_regexp: false,
})
}
pub fn case_insensitive(mut self, case_insensitive: bool) -> Self {
self.case_insensitive = case_insensitive;
self
}
pub fn case_smart(mut self, case_smart: bool) -> Self {
self.case_smart = case_smart;
self
}
pub fn globs(mut self, globs: Vec<String>) -> Result<Self> {
let mut builder = OverrideBuilder::new(std::env::current_dir()?);
for glob in globs {
builder.add(&glob)?;
}
self.overrides = builder.build()?;
Ok(self)
}
pub fn file_types(
mut self,
file_types: Vec<String>,
file_types_not: Vec<String>,
) -> Result<Self> {
let mut builder = TypesBuilder::new();
builder.add_defaults();
for file_type in file_types {
builder.select(&file_type);
}
for file_type in file_types_not {
builder.negate(&file_type);
}
self.types = builder.build()?;
Ok(self)
}
pub fn search_hidden(mut self, search_hidden: bool) -> Self {
self.search_hidden = search_hidden;
self
}
pub fn follow_links(mut self, follow_links: bool) -> Self {
self.follow_links = follow_links;
self
}
pub fn word_regexp(mut self, word_regexp: bool) -> Self {
self.word_regexp = word_regexp;
self
}
}