-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfiltering.rs
406 lines (321 loc) · 11.3 KB
/
filtering.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::{collections::HashMap, hash::Hash};
use wildflower::{Pattern, WILDCARD_MANY_CHAR, WILDCARD_SINGLE_CHAR};
// ---
pub trait KeyNormalize: Clone {
fn normalize(&self, byte: u8) -> u8;
}
// ---
#[derive(Default, Clone)]
pub struct NoNormalizing {}
impl KeyNormalize for NoNormalizing {
#[inline(always)]
fn normalize(&self, byte: u8) -> u8 {
byte
}
}
// ---
#[derive(Default, Clone)]
pub struct DefaultNormalizing {}
impl KeyNormalize for DefaultNormalizing {
#[inline(always)]
fn normalize(&self, byte: u8) -> u8 {
if byte == b'_' { b'-' } else { byte.to_ascii_lowercase() }
}
}
// ---
#[derive(PartialEq, Eq, Clone, Ord, PartialOrd, Copy, Debug)]
pub enum IncludeExcludeSetting {
Unspecified,
Include,
Exclude,
}
impl IncludeExcludeSetting {
#[inline(always)]
pub fn apply(&self, other: Self) -> Self {
match other {
Self::Unspecified => *self,
Self::Include => other,
Self::Exclude => other,
}
}
}
impl Default for IncludeExcludeSetting {
#[inline(always)]
fn default() -> Self {
Self::Unspecified
}
}
// ---
#[derive(PartialEq, Eq, Clone)]
pub struct MatchOptions<N: KeyNormalize> {
pub delimiter: u8,
pub norm: N,
}
impl<N: KeyNormalize + Default> Default for MatchOptions<N> {
#[inline(always)]
fn default() -> Self {
Self {
delimiter: b'.',
norm: N::default(),
}
}
}
// ---
#[derive(Default)]
pub struct IncludeExcludeKeyFilter<N: KeyNormalize> {
children: HashMap<Key, IncludeExcludeKeyFilter<N>>,
patterns: Vec<(Pattern<String>, IncludeExcludeKeyFilter<N>)>,
fallback: Option<Box<IncludeExcludeKeyFilter<N>>>,
options: MatchOptions<N>,
setting: IncludeExcludeSetting,
}
impl<N: KeyNormalize> IncludeExcludeKeyFilter<N> {
pub fn new(options: MatchOptions<N>) -> Self {
Self {
children: HashMap::new(),
patterns: Vec::new(),
fallback: None,
options,
setting: IncludeExcludeSetting::default(),
}
}
pub fn entry<'a>(&'a mut self, key: &'a str) -> &'a mut IncludeExcludeKeyFilter<N> {
let (head, tail) = self.split(key);
if Self::is_pattern(&head) {
return self.add_pattern(head, tail);
}
self.set_fallback(self.setting);
let child = self.children.entry(head).or_insert(Self::new(self.options.clone()));
match tail {
None => child,
Some(tail) => child.entry(tail),
}
}
pub fn get<'a>(&'a self, key: &str) -> Option<&'a IncludeExcludeKeyFilter<N>> {
if self.leaf() {
return if self.setting == IncludeExcludeSetting::Unspecified {
None
} else {
Some(self)
};
}
let (head, tail) = self.split(key);
let found = |child: &'a Self| match tail {
None => Some(child),
Some(tail) => child.get(tail),
};
if let Some(child) = self.children.get(&head) {
return found(child);
}
if self.patterns.len() != 0 {
let head = head.as_str();
for (pattern, child) in self.patterns.iter().rev() {
if pattern.matches(head) {
return found(child);
}
}
}
self.fallback.as_deref()
}
#[inline(always)]
pub fn include(&mut self) -> &mut Self {
self.reset(IncludeExcludeSetting::Include);
self.update_fallback();
self
}
#[inline(always)]
pub fn included(mut self) -> Self {
self.include();
self
}
#[inline(always)]
pub fn exclude(&mut self) -> &mut Self {
self.reset(IncludeExcludeSetting::Exclude);
self.update_fallback();
self
}
#[inline(always)]
pub fn excluded(mut self) -> Self {
self.exclude();
self
}
#[inline(always)]
pub fn setting(&self) -> IncludeExcludeSetting {
self.setting.clone()
}
#[inline(always)]
pub fn leaf(&self) -> bool {
self.children.len() == 0 && self.patterns.len() == 0
}
fn split<'a>(&self, key: &'a str) -> (Key, Option<&'a str>) {
let bytes = key.as_bytes();
let n = bytes.iter().take_while(|&&x| x != self.options.delimiter).count();
let head = bytes[..n].iter().map(|&x| self.options.norm.normalize(x));
let head = if n <= 64 {
Key::Short(head.collect())
} else {
Key::Long(head.collect())
};
let tail = if n == key.len() { None } else { Some(&key[n + 1..]) };
(head, tail)
}
fn is_pattern(key: &Key) -> bool {
let b = key.as_bytes();
b.contains(&(WILDCARD_MANY_CHAR as u8)) || b.contains(&(WILDCARD_SINGLE_CHAR as u8))
}
fn add_pattern<'a>(&'a mut self, key: Key, tail: Option<&'a str>) -> &'a mut IncludeExcludeKeyFilter<N> {
let pattern = Pattern::new(key.to_string());
self.children.retain(|k, _| !pattern.matches(k.as_str()));
let item = match self.patterns.iter().position(|(p, _)| p == &pattern) {
Some(i) => &mut self.patterns[i].1,
None => {
self.patterns.push((pattern, Self::new(self.options.clone())));
self.update_fallback();
&mut self.patterns.last_mut().unwrap().1
}
};
match tail {
None => item,
Some(tail) => item.entry(tail),
}
}
fn update_fallback(&mut self) {
if self.setting == IncludeExcludeSetting::Unspecified || self.leaf() {
self.fallback = None;
} else {
self.set_fallback(self.setting);
}
}
fn set_fallback(&mut self, setting: IncludeExcludeSetting) {
if setting == IncludeExcludeSetting::Unspecified {
self.fallback = None;
} else {
let mut fallback = self
.fallback
.take()
.unwrap_or_else(|| Box::new(Self::new(self.options.clone())));
fallback.setting = setting;
self.fallback = Some(fallback);
}
}
fn reset(&mut self, setting: IncludeExcludeSetting) {
self.children.clear();
self.patterns.clear();
self.fallback = None;
self.setting = setting;
}
}
// ---
#[derive(PartialEq, Eq, Hash, Debug)]
enum Key {
Short(heapless::Vec<u8, 64>),
Long(Vec<u8>),
}
impl PartialEq<&str> for Key {
fn eq(&self, other: &&str) -> bool {
match self {
Key::Short(v) => v == other.as_bytes(),
Key::Long(v) => v.as_slice() == other.as_bytes(),
}
}
}
impl Key {
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
match self {
Key::Short(v) => v.as_ref(),
Key::Long(v) => v.as_slice(),
}
}
#[inline(always)]
fn as_str(&self) -> &str {
std::str::from_utf8(self.as_bytes()).unwrap()
}
#[inline(always)]
fn to_string(&self) -> String {
self.as_str().to_string()
}
}
// ---
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter() {
let mut filter = IncludeExcludeKeyFilter::new(MatchOptions::<DefaultNormalizing>::default());
filter.entry("a").exclude();
filter.entry("a.b").include();
filter.entry("c.d").include();
filter.entry("c.d.e").exclude();
filter.entry("c.d.g*").exclude();
assert!(filter.get("x").is_none());
let a = filter.get("a").unwrap();
assert_eq!(a.setting(), IncludeExcludeSetting::Exclude);
let ab = a.get("b").unwrap();
assert_eq!(ab.setting(), IncludeExcludeSetting::Include);
let ab = filter.get("a.b").unwrap();
assert_eq!(ab.setting(), IncludeExcludeSetting::Include);
let ac = filter.get("a.c").unwrap();
assert_eq!(ac.setting(), IncludeExcludeSetting::Exclude);
let c = filter.get("c").unwrap();
assert_eq!(c.setting(), IncludeExcludeSetting::Unspecified);
let cd = c.get("d").unwrap();
assert_eq!(cd.setting(), IncludeExcludeSetting::Include);
let cd = filter.get("c.d").unwrap();
assert_eq!(cd.setting(), IncludeExcludeSetting::Include);
assert!(c.get("e").is_none());
assert!(filter.get("c.e").is_none());
let cde = cd.get("e").unwrap();
assert_eq!(cde.setting(), IncludeExcludeSetting::Exclude);
let cde = filter.get("c.d.e").unwrap();
assert_eq!(cde.setting(), IncludeExcludeSetting::Exclude);
let cde = filter.get("c.d.e").unwrap();
assert_eq!(cde.setting(), IncludeExcludeSetting::Exclude);
let cdf = cd.get("f").unwrap();
assert_eq!(cdf.setting(), IncludeExcludeSetting::Include);
let cdf = filter.get("c.d.f").unwrap();
assert_eq!(cdf.setting(), IncludeExcludeSetting::Include);
let cdef = filter.get("c.d.e.f").unwrap();
assert_eq!(cdef.setting(), IncludeExcludeSetting::Exclude);
let cdg = filter.get("c.d.g").unwrap();
assert_eq!(cdg.setting(), IncludeExcludeSetting::Exclude);
let cdg2 = filter.get("c.d.g2").unwrap();
assert_eq!(cdg2.setting(), IncludeExcludeSetting::Exclude);
let filter = filter.excluded();
assert_eq!(filter.get("x").unwrap().setting(), IncludeExcludeSetting::Exclude);
let a = filter.get("a").unwrap();
assert_eq!(a.setting(), IncludeExcludeSetting::Exclude);
let ab = a.get("b").unwrap();
assert_eq!(ab.setting(), IncludeExcludeSetting::Exclude);
let ab = filter.get("a.b").unwrap();
assert_eq!(ab.setting(), IncludeExcludeSetting::Exclude);
let ac = filter.get("a.c").unwrap();
assert_eq!(ac.setting(), IncludeExcludeSetting::Exclude);
let c = filter.get("c").unwrap();
assert_eq!(c.setting(), IncludeExcludeSetting::Exclude);
let cd = c.get("d").unwrap();
assert_eq!(cd.setting(), IncludeExcludeSetting::Exclude);
let cd = filter.get("c.d").unwrap();
assert_eq!(cd.setting(), IncludeExcludeSetting::Exclude);
assert_eq!(c.get("e").unwrap().setting(), IncludeExcludeSetting::Exclude);
assert_eq!(filter.get("c.e").unwrap().setting(), IncludeExcludeSetting::Exclude);
let cde = cd.get("e").unwrap();
assert_eq!(cde.setting(), IncludeExcludeSetting::Exclude);
let cde = filter.get("c.d.e").unwrap();
assert_eq!(cde.setting(), IncludeExcludeSetting::Exclude);
let cde = filter.get("c.d.e").unwrap();
assert_eq!(cde.setting(), IncludeExcludeSetting::Exclude);
let cdf = cd.get("f").unwrap();
assert_eq!(cdf.setting(), IncludeExcludeSetting::Exclude);
let cdf = filter.get("c.d.f").unwrap();
assert_eq!(cdf.setting(), IncludeExcludeSetting::Exclude);
let cdef = filter.get("c.d.e.f").unwrap();
assert_eq!(cdef.setting(), IncludeExcludeSetting::Exclude);
let cdg = filter.get("c.d.g").unwrap();
assert_eq!(cdg.setting(), IncludeExcludeSetting::Exclude);
let cdg2 = filter.get("c.d.g2").unwrap();
assert_eq!(cdg2.setting(), IncludeExcludeSetting::Exclude);
let filter = filter.included();
assert_eq!(filter.get("x").unwrap().setting(), IncludeExcludeSetting::Include);
}
}