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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{CaseMatching, Config, Matching, Scoring, UnicodeMatching};
/// A single pattern to match, parsed from syntax like `!^foo`
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Pattern {
/// Raw atom text, e.g. `!^foo`
pub pattern: String,
/// Haystacks matching this atom are excluded
pub negated: bool,
/// Text to match with the syntax stripped, e.g. `foo`
pub needle: String,
/// Configuration for this pattern
pub config: PatternConfig,
}
/// Matches the needle literally, without parsing any syntax, using the matching
/// mode from [`crate::Config::matching`]
impl From<&str> for Pattern {
fn from(needle: &str) -> Self {
Pattern::new(needle, PatternConfig::default())
}
}
impl From<String> for Pattern {
fn from(needle: String) -> Self {
needle.as_str().into()
}
}
impl From<&String> for Pattern {
fn from(needle: &String) -> Self {
needle.as_str().into()
}
}
impl Pattern {
/// Creates a pattern that matches the needle literally, without parsing any syntax.
/// Use [`Pattern::negated`] to exclude matching haystacks.
pub fn new(needle: &str, config: PatternConfig) -> Self {
Self {
pattern: needle.to_string(),
negated: false,
needle: needle.to_string(),
config,
}
}
/// Sets whether haystacks matching this pattern are excluded
pub fn negated(mut self, negated: bool) -> Self {
self.negated = negated;
self
}
/// Overrides [`crate::Config::matching`] for this pattern (see [`PatternConfig::matching`])
pub fn matching(mut self, matching: Option<Matching>) -> Self {
self.config = self.config.matching(matching);
self
}
/// Overrides [`crate::Config::max_typos`] for this pattern (see [`PatternConfig::max_typos`])
pub fn max_typos(mut self, max_typos: Option<u16>) -> Self {
self.config = self.config.max_typos(max_typos);
self
}
/// Overrides [`crate::Config::casing`] for this pattern (see [`PatternConfig::casing`])
pub fn casing(mut self, casing: Option<CaseMatching>) -> Self {
self.config = self.config.casing(casing);
self
}
/// Overrides [`crate::Config::unicode`] for this pattern (see [`PatternConfig::unicode`])
pub fn unicode(mut self, unicode: Option<UnicodeMatching>) -> Self {
self.config = self.config.unicode(unicode);
self
}
/// Overrides [`crate::Config::scoring`] for this pattern (see [`PatternConfig::scoring`])
pub fn scoring(mut self, scoring: Option<Scoring>) -> Self {
self.config = self.config.scoring(scoring);
self
}
/// Parses a single query atom, where special syntax changes the matching mode:
///
/// `foo` - `None` (defers to [`crate::Config::matching`])
/// `^foo` - [`Matching::Prefix`]
/// `foo$` - [`Matching::Suffix`]
/// `'foo` - [`Matching::Substring`]
/// `^foo$` - [`Matching::Exact`]
/// `!foo` - negated, [`Matching::Substring`] unless combined with the syntax above
///
/// Any special character can be escaped with a backslash, e.g. `\!foo`, `\^foo`,
/// `foo\$` or `\'foo` match the literal leading/trailing character, and `foo\ bar`
/// matches the literal space.
pub fn parse(atom: &str) -> Self {
// Tokenize chars, marking whether they're esacped or not
let mut tokens: Vec<(char, bool)> = Vec::with_capacity(atom.len());
let mut chars = atom.chars();
while let Some(c) = chars.next() {
match if c == '\\' { chars.next() } else { None } {
Some(escaped) => tokens.push((escaped, true)),
None => tokens.push((c, false)),
}
}
// Pops the first/last token if it's the unescaped operator `op`
fn strip_first(tokens: &mut &[(char, bool)], op: char) -> bool {
if let Some((&(c, false), rest)) = tokens.split_first()
&& c == op
{
*tokens = rest;
return true;
}
false
}
fn strip_last(tokens: &mut &[(char, bool)], op: char) -> bool {
if let Some((&(c, false), rest)) = tokens.split_last()
&& c == op
{
*tokens = rest;
return true;
}
false
}
let mut rest = tokens.as_slice();
let negated = strip_first(&mut rest, '!');
let prefix = strip_first(&mut rest, '^');
let substring = !prefix && strip_first(&mut rest, '\'');
let suffix = strip_last(&mut rest, '$');
// Escaped special characters collapse to the literal character; the backslash is
// kept before anything else (including another backslash)
let is_special = |c: char| matches!(c, '!' | '^' | '\'' | '$') || c.is_whitespace();
let mut needle = String::with_capacity(atom.len());
for &(c, escaped) in rest {
if escaped && !is_special(c) {
needle.push('\\');
}
needle.push(c);
}
let matching = match (prefix, suffix, substring) {
(true, true, _) => Some(Matching::Exact),
(true, false, _) => Some(Matching::Prefix),
(false, true, _) => Some(Matching::Suffix),
(false, false, true) => Some(Matching::Substring),
// Negating a fuzzy match excludes far more than users typically expect,
// so bare negated atoms match substrings, like fzf and nucleo
_ if negated => Some(Matching::Substring),
_ => None,
};
Self {
pattern: atom.to_string(),
negated,
needle,
config: PatternConfig::default().matching(matching),
}
}
/// Parses a query of whitespace separated atoms (see [`Pattern::parse`]), e.g.
/// `foo !^bar` matches haystacks that fuzzy match `foo` and don't start with `bar`.
/// Escape a literal space with a backslash, e.g. `foo\ bar` is a single atom.
/// Atoms with an empty needle, e.g. `!` or `^$`, are dropped.
///
/// The returned patterns carry only the [`Matching`] mode derived from the syntax. Any
/// other per-pattern override is left as `None` and inherits the matcher's [`Config`].
/// Set other [`PatternConfig`] fields on the results to override per-pattern.
/// For example, setting the max typos based on needle length:
///
/// ```
/// use frizbee::{Config, Matcher, Pattern};
///
/// let patterns = Pattern::parse_query("foo longerneedle")
/// .into_iter()
/// .map(|pattern| {
/// let max_typos = (pattern.needle.len() / 4) as u16;
/// pattern.max_typos(Some(max_typos))
/// })
/// .collect::<Vec<_>>();
///
/// let mut matcher = Matcher::from_patterns(&patterns, &Config::default());
/// ```
pub fn parse_query(query: &str) -> Vec<Pattern> {
let mut patterns = vec![];
let mut start: Option<usize> = None;
let mut escaped = false;
let mut push = |atom: &str| {
let pattern = Self::parse(atom);
if !pattern.needle.is_empty() {
patterns.push(pattern);
}
};
for (i, c) in query.char_indices() {
if escaped {
// Previous char was an escaping backslash; keep this char in the atom
escaped = false;
} else if c == '\\' {
start.get_or_insert(i);
escaped = true;
} else if c.is_whitespace() {
if let Some(s) = start.take() {
push(&query[s..i]);
}
} else if start.is_none() {
start = Some(i);
}
}
if let Some(s) = start {
push(&query[s..]);
}
patterns
}
}
/// Per-pattern overrides for the matcher's [`Config`]. Every field is optional and falls
/// back to the matcher's [`Config`] when left as `None` (see [`PatternConfig::resolve`])
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct PatternConfig {
/// Per-pattern override for [`crate::Config::max_typos`]; `None` inherits it.
///
/// Because [`crate::Config::max_typos`] is itself `Option`, there is no way to request
/// unlimited typos for a single pattern while the matcher's config sets a limit.
pub max_typos: Option<u16>,
/// Per-pattern override for [`crate::Config::casing`]; `None` inherits it.
pub casing: Option<CaseMatching>,
/// Per-pattern override for [`crate::Config::unicode`]; `None` inherits it.
pub unicode: Option<UnicodeMatching>,
/// Per-pattern override for [`crate::Config::matching`]; `None` inherits it.
pub matching: Option<Matching>,
/// Per-pattern override for [`crate::Config::scoring`]; `None` inherits it.
pub scoring: Option<Scoring>,
}
impl PatternConfig {
/// Resolves this pattern's overrides against the matcher's [`Config`], using the
/// matcher's value for any field left as `None`. The returned config's `sort` is
/// always the matcher's, as result ordering isn't a per-pattern concern.
pub fn resolve(&self, config: &Config) -> Config {
Config {
max_typos: self.max_typos.or(config.max_typos),
casing: self.casing.unwrap_or(config.casing),
unicode: self.unicode.unwrap_or(config.unicode),
matching: self.matching.unwrap_or(config.matching),
scoring: self
.scoring
.clone()
.unwrap_or_else(|| config.scoring.clone()),
sort: config.sort,
}
}
/// Sets the matching mode
pub fn matching(mut self, matching: Option<Matching>) -> Self {
self.matching = matching;
self
}
/// Sets the maximum number of typos allowed
pub fn max_typos(mut self, max_typos: Option<u16>) -> Self {
self.max_typos = max_typos;
self
}
/// Sets the casing mode
pub fn casing(mut self, casing: Option<CaseMatching>) -> Self {
self.casing = casing;
self
}
/// Sets the unicode mode
pub fn unicode(mut self, unicode: Option<UnicodeMatching>) -> Self {
self.unicode = unicode;
self
}
/// Sets the scoring
pub fn scoring(mut self, scoring: Option<Scoring>) -> Self {
self.scoring = scoring;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_parse(atom: &str, needle: &str, matching: Option<Matching>, negated: bool) {
let pattern = Pattern::parse(atom);
assert_eq!(pattern.pattern, atom);
assert_eq!(pattern.needle, needle, "atom: {atom:?}");
assert_eq!(pattern.config.matching, matching, "atom: {atom:?}");
assert_eq!(pattern.negated, negated, "atom: {atom:?}");
}
#[test]
fn parse_selects_matching_mode() {
assert_parse("foo", "foo", None, false);
assert_parse("^foo", "foo", Some(Matching::Prefix), false);
assert_parse("foo$", "foo", Some(Matching::Suffix), false);
assert_parse("'foo", "foo", Some(Matching::Substring), false);
assert_parse("^foo$", "foo", Some(Matching::Exact), false);
}
#[test]
fn parse_negation() {
// Bare negated atoms match substrings, like fzf and nucleo
assert_parse("!foo", "foo", Some(Matching::Substring), true);
assert_parse("!^foo", "foo", Some(Matching::Prefix), true);
assert_parse("!foo$", "foo", Some(Matching::Suffix), true);
assert_parse("!'foo", "foo", Some(Matching::Substring), true);
assert_parse("!^foo$", "foo", Some(Matching::Exact), true);
}
#[test]
fn parse_escapes_special_syntax() {
assert_parse("\\^foo", "^foo", None, false);
assert_parse("foo\\$", "foo$", None, false);
assert_parse("\\'foo", "'foo", None, false);
assert_parse("\\!foo", "!foo", None, false);
assert_parse("foo\\ bar", "foo bar", None, false);
assert_parse("!\\^foo", "^foo", Some(Matching::Substring), true);
assert_parse("!\\!foo", "!foo", Some(Matching::Substring), true);
}
#[test]
fn parse_escaped_backslash_before_operator() {
assert_parse("foo\\\\$", "foo\\\\", Some(Matching::Suffix), false);
// Backslashes before non-special characters are literal
assert_parse("foo\\bar", "foo\\bar", None, false);
assert_parse("foo\\", "foo\\", None, false);
// The first two backslashes pair up, the third escapes the space
assert_parse("a\\\\\\ b", "a\\\\ b", None, false);
}
#[test]
fn parse_query_splits_atoms() {
let patterns = Pattern::parse_query("foo !^bar");
assert_eq!(patterns.len(), 2);
assert_eq!(patterns[0], Pattern::parse("foo"));
assert_eq!(patterns[1], Pattern::parse("!^bar"));
let patterns = Pattern::parse_query(" foo \t bar ");
assert_eq!(patterns.len(), 2);
assert_eq!(patterns[0].needle, "foo");
assert_eq!(patterns[1].needle, "bar");
}
#[test]
fn parse_query_escaped_space() {
let patterns = Pattern::parse_query("foo\\ bar baz");
assert_eq!(patterns.len(), 2);
assert_eq!(patterns[0].needle, "foo bar");
assert_eq!(patterns[1].needle, "baz");
}
#[test]
fn parse_query_escaped_backslash_before_space_splits() {
// The escaped backslash consumes the escape, so the space still separates atoms
let patterns = Pattern::parse_query("foo\\\\ bar");
assert_eq!(patterns.len(), 2);
assert_eq!(patterns[0].needle, "foo\\\\");
assert_eq!(patterns[1].needle, "bar");
}
#[test]
fn parse_query_drops_empty_atoms() {
assert!(Pattern::parse_query("").is_empty());
assert!(Pattern::parse_query(" ").is_empty());
assert!(Pattern::parse_query("! ^$ '").is_empty());
}
}