Struct broot::content_search::Needle
source · pub struct Needle { /* private fields */ }
Expand description
a strict (non fuzzy, case sensitive) pattern which may be searched in file contents
Implementations§
source§impl Needle
impl Needle
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Examples found in repository?
More examples
src/content_search/needle.rs (line 222)
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
pub fn get_match<P: AsRef<Path>>(
&self,
hay_path: P,
desired_len: usize,
) -> Option<ContentMatch> {
let hay = match get_mmap(hay_path) {
Ok(hay) => hay,
_ => { return None; }
};
match self.search_mmap(&hay) {
ContentSearchResult::Found { pos } => {
Some(ContentMatch::build(&hay, pos, self.as_str(), desired_len))
}
_ => None,
}
}
sourcepub fn search<P: AsRef<Path>>(&self, hay_path: P) -> Result<ContentSearchResult>
pub fn search<P: AsRef<Path>>(&self, hay_path: P) -> Result<ContentSearchResult>
determine whether the file contains the needle
Examples found in repository?
src/pattern/content_pattern.rs (line 47)
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
pub fn score_of(&self, candidate: Candidate) -> Option<i32> {
if !candidate.regular_file {
return None;
}
match self.needle.search(candidate.path) {
Ok(ContentSearchResult::Found { .. }) => Some(1),
Ok(ContentSearchResult::NotFound) => None,
Ok(ContentSearchResult::NotSuitable) => {
None
}
Err(e) => {
debug!("error while scanning {:?} : {:?}", &candidate.path, e);
None
}
}
}
/// get the line of the first match, if any
pub fn get_match_line_count(
&self,
path: &Path,
) -> Option<usize> {
if let Ok(ContentSearchResult::Found { pos }) = self.needle.search(path) {
line_count_at_pos(path, pos).ok()
} else {
None
}
}