fix: remove IO on transformed_keywords

This commit is contained in:
Azalea Colburn 2025-03-16 00:47:53 -07:00
parent 57520a15a3
commit 1259156382
No known key found for this signature in database
1 changed files with 13 additions and 11 deletions

View File

@ -78,7 +78,7 @@ impl Query {
fn get_stream<'a>(&self, db: &'a mut Database, now: Epoch) -> Result<Stream<'a>> {
let mut options = StreamOptions::new(now)
.with_keywords(self.transfomed_keywords())
.with_keywords(self.transformed_keywords())
.with_exclude(config::exclude_dirs()?);
if !self.all {
let resolve_symlinks = config::resolve_symlinks();
@ -119,19 +119,21 @@ impl Query {
}
/// ## Returns
/// `self.keywords` with file paths transformed into their parent directory
/// paths and directory paths unchanged
/// `self.keywords` with file paths transformed into
/// their parent directory paths, directory paths unchanged
///
/// ## Warning
/// Clones self.keywords
fn transfomed_keywords(&self) -> impl Iterator<Item = String> + use<'_> {
/// ## Notes
/// - Heap allocates an iterator of Strings with length of self.keywords
/// - Only uses sans-IO Path methods
fn transformed_keywords(&self) -> impl Iterator<Item = String> + use<'_> {
self.keywords.iter().map(|keyword| {
if std::path::Path::new(keyword).is_file() {
let dirs: Vec<&str> = keyword.split("/").collect();
dirs.split_last().unwrap().1.join("/").to_string()
} else {
keyword.to_string()
let path = std::path::Path::new(keyword);
match path.parent() {
None => keyword,
Some(path) if path.to_str() == Some("") => keyword,
Some(path) => path.to_str().unwrap(),
}
.to_string()
})
}
}