diff --git a/src/cmd/query.rs b/src/cmd/query.rs index e086e09..95c8a88 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -78,7 +78,7 @@ impl Query { fn get_stream<'a>(&self, db: &'a mut Database, now: Epoch) -> Result> { 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 + 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 + 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() }) } }