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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
use {
crate::{
app::AppContext,
errors::{ConfError, PatternError},
},
fnv::FnvHashMap,
lazy_regex::regex_is_match,
std::convert::TryFrom,
};
/// where to search
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SearchObject {
Name,
Path,
Content,
}
/// how to search
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SearchKind {
Exact,
Fuzzy,
Regex,
Tokens,
}
/// a valid combination of SearchObject and SearchKind,
/// determine how a pattern will be used
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SearchMode {
NameExact,
NameFuzzy,
NameRegex,
NameTokens,
PathExact,
PathFuzzy,
PathRegex,
PathTokens,
ContentExact,
ContentRegex,
}
pub static SEARCH_MODES: &[SearchMode] = &[
SearchMode::NameFuzzy,
SearchMode::NameRegex,
SearchMode::NameExact,
SearchMode::NameTokens,
SearchMode::PathExact,
SearchMode::PathFuzzy,
SearchMode::PathRegex,
SearchMode::PathTokens,
SearchMode::ContentExact,
SearchMode::ContentRegex,
];
impl SearchMode {
fn new(search_object: SearchObject, search_kind: SearchKind) -> Option<Self> {
use {
SearchObject::*,
SearchKind::*,
};
match (search_object, search_kind) {
(Name, Exact) => Some(Self::NameExact),
(Name, Fuzzy) => Some(Self::NameFuzzy),
(Name, Regex) => Some(Self::NameRegex),
(Name, Tokens) => Some(Self::NameTokens),
(Path, Exact) => Some(Self::PathExact),
(Path, Fuzzy) => Some(Self::PathFuzzy),
(Path, Regex) => Some(Self::PathRegex),
(Path, Tokens) => Some(Self::PathTokens),
(Content, Exact) => Some(Self::ContentExact),
(Content, Fuzzy) => None, // unsupported for now - could be but why ?
(Content, Regex) => Some(Self::ContentRegex),
(Content, Tokens) => None, // unsupported for now - could be but need bench
}
}
/// Return the prefix to type, eg "/" in standard for a name-regex,
/// "" for a name-fuzzy, and "ep" for a path-exact
pub fn prefix(self, con: &AppContext) -> String {
con
.search_modes
.key(self)
.map_or_else(|| "".to_string(), |k| format!("{}/", k))
}
pub fn object(self) -> SearchObject {
match self {
Self::NameExact | Self::NameFuzzy | Self::NameRegex | Self::NameTokens => SearchObject::Name,
Self::PathExact | Self::PathFuzzy | Self::PathRegex | Self::PathTokens => SearchObject::Path,
Self::ContentExact | Self::ContentRegex => SearchObject::Content,
}
}
pub fn kind(self) -> SearchKind {
match self {
Self::NameExact => SearchKind::Exact,
Self::NameFuzzy => SearchKind::Fuzzy,
Self::NameRegex => SearchKind::Regex,
Self::NameTokens => SearchKind::Tokens,
Self::PathExact => SearchKind::Exact,
Self::PathFuzzy => SearchKind::Fuzzy,
Self::PathRegex => SearchKind::Regex,
Self::PathTokens => SearchKind::Tokens,
Self::ContentExact => SearchKind::Exact,
Self::ContentRegex => SearchKind::Regex,
}
}
}
/// define a mapping from a search mode which can be typed in
/// the input to a SearchMode value
#[derive(Debug, Clone)]
pub struct SearchModeMapEntry {
pub key: Option<String>,
pub mode: SearchMode,
}
/// manage how to find the search mode to apply to a
/// pattern taking the config in account.
#[derive(Debug, Clone)]
pub struct SearchModeMap {
pub entries: Vec<SearchModeMapEntry>,
}
impl SearchModeMapEntry {
pub fn parse(conf_key: &str, conf_mode: &str) -> Result<Self, ConfError> {
let mut search_kinds = Vec::new();
let mut search_objects = Vec::new();
let s = conf_mode.to_lowercase();
for t in s.split_whitespace() {
match t {
"exact" => search_kinds.push(SearchKind::Exact),
"fuzzy" => search_kinds.push(SearchKind::Fuzzy),
"regex" => search_kinds.push(SearchKind::Regex),
"tokens" => search_kinds.push(SearchKind::Tokens),
"name" => search_objects.push(SearchObject::Name),
"content" => search_objects.push(SearchObject::Content),
"path" => search_objects.push(SearchObject::Path),
_ => {
return Err(ConfError::InvalidSearchMode {
details: format!("{:?} not understood in search mode definition", t),
});
}
}
}
if search_kinds.is_empty() {
return Err(ConfError::InvalidSearchMode {
details: "missing search kind in search mode definition\
(the search kind must be one of 'exact', 'fuzzy', 'regex', 'tokens')".to_string()
});
}
if search_kinds.len() > 1 {
return Err(ConfError::InvalidSearchMode {
details: "only one search kind can be specified in a search mode".to_string()
});
}
if search_objects.is_empty() {
return Err(ConfError::InvalidSearchMode {
details: "missing search object in search mode definition\
(the search object must be one of 'name', 'path', 'content')".to_string()
});
}
if search_objects.len() > 1 {
return Err(ConfError::InvalidSearchMode {
details: "only one search object can be specified in a search mode".to_string()
});
}
let mode = match SearchMode::new(search_objects[0], search_kinds[0]) {
Some(mode) => mode,
None => {
return Err(ConfError::InvalidSearchMode {
details: "Unsupported combination of search object and kind".to_string()
});
},
};
let key = if conf_key.is_empty() || conf_key == "<empty>" {
// serde toml parser doesn't handle correctly empty keys so we accept as
// alternative the `"<empty>" = "fuzzy name"` solution.
// TODO look at issues and/or code in serde-toml
None
} else if regex_is_match!(r"^\w*/$", conf_key) {
Some(conf_key[0..conf_key.len() - 1].to_string())
} else {
return Err(ConfError::InvalidKey {
raw: conf_key.to_string(),
});
};
Ok(SearchModeMapEntry { key, mode })
}
}
impl Default for SearchModeMap {
fn default() -> Self {
let mut smm = SearchModeMap {
entries: Vec::new(),
};
// the last keys are prefered
smm.setm(&["ne", "en", "e"], SearchMode::NameExact);
smm.setm(&["nf", "fn", "n", "f"], SearchMode::NameFuzzy);
smm.setm(&["r", "nr", "rn", ""], SearchMode::NameRegex);
smm.setm(&["pe", "ep"], SearchMode::PathExact);
smm.setm(&["pf", "fp", "p"], SearchMode::PathFuzzy);
smm.setm(&["pr", "rp"], SearchMode::PathRegex);
smm.setm(&["ce", "ec", "c"], SearchMode::ContentExact);
smm.setm(&["rx", "cr"], SearchMode::ContentRegex);
smm.setm(&["pt", "tp", "t"], SearchMode::PathTokens);
smm.setm(&["tn", "nt"], SearchMode::NameTokens);
smm.set(SearchModeMapEntry { key: None, mode: SearchMode::PathFuzzy });
smm
}
}
impl TryFrom<&FnvHashMap<String, String>> for SearchModeMap {
type Error = ConfError;
fn try_from(map: &FnvHashMap<String, String>) -> Result<Self, Self::Error> {
let mut smm = Self::default();
for (k, v) in map {
smm.entries.push(SearchModeMapEntry::parse(k, v)?);
}
Ok(smm)
}
}
impl SearchModeMap {
pub fn setm(&mut self, keys: &[&str], mode: SearchMode) {
for key in keys {
self.set(SearchModeMapEntry {
key: Some(key.to_string()),
mode,
});
}
}
/// we don't remove existing entries to ensure there's always a matching entry in
/// mode->key (but search iterations will be done in reverse)
pub fn set(&mut self, entry: SearchModeMapEntry) {
self.entries.push(entry);
}
pub
|