Struct broot::pattern::FuzzyPattern
source · pub struct FuzzyPattern { /* private fields */ }
Expand description
A pattern for fuzzy matching
Implementations§
source§impl FuzzyPattern
impl FuzzyPattern
sourcepub fn from(pat: &str) -> Self
pub fn from(pat: &str) -> Self
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))
})
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
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,
}
}
sourcepub fn find(&self, candidate: &str) -> Option<NameMatch>
pub fn find(&self, candidate: &str) -> Option<NameMatch>
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?
More 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,
}
}
sourcepub fn score_of(&self, candidate: &str) -> Option<i32>
pub fn score_of(&self, candidate: &str) -> Option<i32>
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§
source§impl Clone for FuzzyPattern
impl Clone for FuzzyPattern
source§fn clone(&self) -> FuzzyPattern
fn clone(&self) -> FuzzyPattern
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more