pub struct FileSearcher { /* private fields */ }Expand description
A file searcher that finds text patterns in files.
FileSearcher provides methods to search for text patterns in files within a directory hierarchy.
It supports both fixed string and regex-based search patterns, and can handle various search
options like case sensitivity and whole word matching.
This struct is the main entry point for file searching operations in frep-core.
Implementations§
Source§impl FileSearcher
impl FileSearcher
Sourcepub fn new(config: FileSearcherConfig) -> Self
pub fn new(config: FileSearcherConfig) -> Self
Creates a new FileSearcher from the given configuration.
This method processes the configuration options and prepares the search pattern.
If whole_word or match_case options are set, the search pattern is adjusted
accordingly by wrapping it in appropriate regex patterns.
Sourcepub fn walk_files<F>(&self, cancelled: Option<&AtomicBool>, file_handler: F)
pub fn walk_files<F>(&self, cancelled: Option<&AtomicBool>, file_handler: F)
Walks through files in the configured directory and processes matches.
This method traverses the filesystem starting from the root_dir specified in the FileSearcher,
respecting the configured overrides (include/exclude patterns) and hidden file settings.
It uses parallel processing when possible for better performance.
§Parameters
-
cancelled- An optional atomic boolean that can be used to signal cancellation from another thread. If this is set totrueduring execution, the search will stop as soon as possible. -
file_handler- A closure that returns aFileVisitor. The returnedFileVisitoris a function that processes search results for each file with matches.
§Example
use std::{
sync::{atomic::AtomicBool, mpsc},
path::PathBuf,
};
use regex::Regex;
use ignore::{WalkState, overrides::Override};
use frep_core::search::{FileSearcher, FileSearcherConfig, SearchResult, SearchType};
let config = FileSearcherConfig {
search: SearchType::Pattern(Regex::new("pattern").unwrap()),
replace: "replacement".to_string(),
whole_word: false,
match_case: true,
overrides: Override::empty(),
root_dir: PathBuf::from("."),
include_hidden: false,
};
let searcher = FileSearcher::new(config);
let cancelled = AtomicBool::new(false);
searcher.walk_files(Some(&cancelled), move || {
Box::new(move |results| {
if process(results).is_err() {
WalkState::Quit
} else {
WalkState::Continue
}
})
});
fn process(results: Vec<SearchResult>) -> anyhow::Result<()> {
println!("{results:?}");
Ok(())
}Trait Implementations§
Source§impl Clone for FileSearcher
impl Clone for FileSearcher
Source§fn clone(&self) -> FileSearcher
fn clone(&self) -> FileSearcher
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more