pub struct FileSearcher { /* private fields */ }Implementations§
Source§impl FileSearcher
impl FileSearcher
Source§impl FileSearcher
impl FileSearcher
pub fn new( search_config: ParsedSearchConfig, dir_config: ParsedDirConfig, ) -> Self
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, ParsedSearchConfig, ParsedDirConfig, SearchResult, SearchType};
let search_config = ParsedSearchConfig {
search: SearchType::Pattern(Regex::new("pattern").unwrap()),
replace: "replacement".to_string(),
};
let dir_config = ParsedDirConfig {
overrides: Override::empty(),
root_dir: PathBuf::from("."),
include_hidden: false,
};
let searcher = FileSearcher::new(search_config, dir_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(())
}Sourcepub fn walk_files_and_replace(&self, cancelled: Option<&AtomicBool>) -> usize
pub fn walk_files_and_replace(&self, cancelled: Option<&AtomicBool>) -> usize
Walks through files in the configured directory and replaces 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 replaces all matches of the search pattern with the replacement text in each file.
§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.
§Returns
The number of files that had replacements performed in them.
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