Struct broot::pattern::FuzzyPattern

source ·
pub struct FuzzyPattern { /* private fields */ }
Expand description

A pattern for fuzzy matching

Implementations§

build a pattern which will later be usable for fuzzy search. A pattern should be reused

Examples found in repository?
src/pattern/pattern.rs (line 52)
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
    pub fn new(
        raw_expr: &BeTree<PatternOperator, PatternParts>,
        search_modes: &SearchModeMap,
        content_search_max_file_size: usize,
    ) -> Result<Self, PatternError> {
        let expr: BeTree<PatternOperator, Pattern> = raw_expr
            .try_map_atoms::<_, PatternError, _>(|pattern_parts| {
                let core = pattern_parts.core();
                Ok(
                    if core.is_empty() {
                        Pattern::None
                    } else {
                        let parts_mode = pattern_parts.mode();
                        let mode = search_modes.search_mode(parts_mode)?;
                        let flags = pattern_parts.flags();
                        match mode {
                            SearchMode::NameExact => Self::NameExact(
                                ExactPattern::from(core)
                            ),
                            SearchMode::NameFuzzy => Self::NameFuzzy(
                                FuzzyPattern::from(core)
                            ),
                            SearchMode::NameRegex => Self::NameRegex(
                                RegexPattern::from(core, flags.unwrap_or(""))?
                            ),
                            SearchMode::NameTokens => Self::NameTokens(
                                TokPattern::new(core)
                            ),
                            SearchMode::PathExact => Self::PathExact(
                                ExactPattern::from(core)
                            ),
                            SearchMode::PathFuzzy => Self::PathFuzzy(
                                FuzzyPattern::from(core)
                            ),
                            SearchMode::PathRegex => Self::PathRegex(
                                RegexPattern::from(core, flags.unwrap_or(""))?
                            ),
                            SearchMode::PathTokens => Self::PathTokens(
                                TokPattern::new(core)
                            ),
                            SearchMode::ContentExact => Self::ContentExact(
                                ContentExactPattern::new(core, content_search_max_file_size)
                            ),
                            SearchMode::ContentRegex => Self::ContentRegex(
                                ContentRegexPattern::new(
                                    core,
                                    flags.unwrap_or(""),
                                    content_search_max_file_size,
                                )?
                            ),
                        }
                    }
                )
            })?;
        Ok(if expr.is_empty() {
            Pattern::None
        } else if expr.is_atomic() {
            expr.atoms().pop().unwrap()
        } else {
            Self::Composite(CompositePattern::new(expr))
        })
    }

an “empty” pattern is one which accepts everything because it has no discriminant

Examples found in repository?
src/pattern/pattern.rs (line 203)
199
200
201
202
203
204
205
206
207
208
209
210
    pub fn is_empty(&self) -> bool {
        match self {
            Self::NameExact(ep) | Self::PathExact(ep) => ep.is_empty(),
            Self::ContentExact(ep) => ep.is_empty(),
            Self::NameFuzzy(fp) | Self::PathFuzzy(fp) => fp.is_empty(),
            Self::NameRegex(rp) | Self::PathRegex(rp) => rp.is_empty(),
            Self::ContentRegex(rp) => rp.is_empty(),
            Self::NameTokens(tp) | Self::PathTokens(tp) => tp.is_empty(),
            Self::Composite(cp) => cp.is_empty(),
            Self::None => true,
        }
    }

return a match if the pattern can be found in the candidate string. The algorithm tries to return the best one. For example if you search “abc” in “ababca-abc”, the returned match would be at the end.

Examples found in repository?
src/pattern/fuzzy_pattern.rs (line 210)
209
210
211
212
    pub fn score_of(&self, candidate: &str) -> Option<i32> {
        self.find(candidate)
            .map(|nm| nm.score)
    }
More examples
Hide additional examples
src/pattern/pattern.rs (line 123)
117
118
119
120
121
122
123
124
125
126
127
128
129
    pub fn search_string(
        &self,
        candidate: &str,
    ) -> Option<NameMatch> {
        match self {
            Self::NameExact(ep) | Self::PathExact(ep) => ep.find(candidate),
            Self::NameFuzzy(fp) | Self::PathFuzzy(fp) => fp.find(candidate),
            Self::NameRegex(rp) | Self::PathRegex(rp) => rp.find(candidate),
            Self::NameTokens(tp) | Self::PathTokens(tp) => tp.find(candidate),
            Self::Composite(cp) => cp.search_string(candidate),
            _ => None,
        }
    }

compute the score of the best match

Examples found in repository?
src/pattern/pattern.rs (line 162)
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
    pub fn score_of(&self, candidate: Candidate) -> Option<i32> {
        match self {
            Self::NameExact(ep) => ep.score_of(candidate.name),
            Self::NameFuzzy(fp) => fp.score_of(candidate.name),
            Self::NameRegex(rp) => rp.find(candidate.name).map(|m| m.score),
            Self::NameTokens(tp) => tp.score_of(candidate.name),
            Self::PathExact(ep) => ep.score_of(candidate.subpath),
            Self::PathFuzzy(fp) => fp.score_of(candidate.subpath),
            Self::PathRegex(rp) => rp.find(candidate.subpath).map(|m| m.score),
            Self::PathTokens(tp) => tp.score_of(candidate.subpath),
            Self::ContentExact(cp) => cp.score_of(candidate),
            Self::ContentRegex(cp) => cp.score_of(candidate),
            Self::Composite(cp) => cp.score_of(candidate),
            Self::None => Some(1),
        }
    }

    pub fn score_of_string(&self, candidate: &str) -> Option<i32> {
        match self {
            Self::NameExact(ep) => ep.score_of(candidate),
            Self::NameFuzzy(fp) => fp.score_of(candidate),
            Self::NameRegex(rp) => rp.find(candidate).map(|m| m.score),
            Self::NameTokens(tp) => tp.score_of(candidate),
            Self::PathExact(ep) => ep.score_of(candidate),
            Self::PathFuzzy(fp) => fp.score_of(candidate),
            Self::PathRegex(rp) => rp.find(candidate).map(|m| m.score),
            Self::PathTokens(tp) => tp.score_of(candidate),
            Self::ContentExact(_) => None, // this isn't suitable
            Self::ContentRegex(_) => None, // this isn't suitable
            Self::Composite(cp) => cp.score_of_string(candidate),
            Self::None => Some(1),
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.