Struct broot::pattern::TokPattern
source · pub struct TokPattern { /* private fields */ }
Expand description
a list of tokens we want to find, non overlapping and in any order, in strings
Implementations§
source§impl TokPattern
impl TokPattern
sourcepub fn new(pattern: &str) -> Self
pub fn new(pattern: &str) -> Self
Examples found in repository?
src/pattern/pattern.rs (line 58)
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 206)
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>
note that it should not be called on empty patterns
Examples found in repository?
src/pattern/pattern.rs (line 125)
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 Note that it should not be called on empty patterns
Examples found in repository?
src/pattern/pattern.rs (line 164)
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 TokPattern
impl Clone for TokPattern
source§fn clone(&self) -> TokPattern
fn clone(&self) -> TokPattern
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 moresource§impl Debug for TokPattern
impl Debug for TokPattern
source§impl PartialEq<TokPattern> for TokPattern
impl PartialEq<TokPattern> for TokPattern
source§fn eq(&self, other: &TokPattern) -> bool
fn eq(&self, other: &TokPattern) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.