From 09f51336df00b38928c4b42782687012b9bfae39 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Tue, 29 Nov 2022 00:07:33 -0600 Subject: feat: create `ProjectParserGroup` to group parsers --- src/search/entry.rs | 57 +++++++++++------------------------------------------ 1 file changed, 11 insertions(+), 46 deletions(-) (limited to 'src/search') diff --git a/src/search/entry.rs b/src/search/entry.rs index 1e49a67..6cd601c 100644 --- a/src/search/entry.rs +++ b/src/search/entry.rs @@ -1,8 +1,8 @@ use ignore::{Walk, WalkBuilder}; -use tracing::{error, warn}; +use tracing::error; use crate::{ - project::{path::PathMatcher, ProjectParser}, + project::{path::PathMatcher, ProjectParser, ProjectParserGroup}, search::ProjectItem, }; @@ -11,26 +11,10 @@ pub use config::Config; mod config; pub struct Entry { - path_parser: Option, - - #[cfg(feature = "git")] - git_parser: Option, - + parsers: ProjectParserGroup, iter: Walk, } -impl std::fmt::Debug for Entry { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut debug = f.debug_struct("Entry"); - debug.field("path_matcher", &self.path_parser); - - #[cfg(feature = "git")] - debug.field("git_matcher", &self.git_parser); - - debug.finish() - } -} - impl Entry { fn new(config: &Config) -> Self { let iter = WalkBuilder::new(&config.path_buf) @@ -39,37 +23,18 @@ impl Entry { .hidden(!config.hidden) .build(); - Self { - iter, - path_parser: config.pattern.as_ref().map(|s| PathMatcher(s.to_owned())), + let mut parsers = ProjectParserGroup::new(); - #[cfg(feature = "git")] - git_parser: config.git.then_some(crate::project::git::GitMatcher), - } - } -} - -impl ProjectParser for Entry { - #[tracing::instrument] - fn parse_project(&self, path_buf: std::path::PathBuf) -> Option { - #[cfg(feature = "git")] - if let Some(p) = self - .git_parser - .as_ref() - .and_then(|m| m.parse_project(path_buf.to_owned())) - { - return Some(p); + if let Some(s) = config.pattern.as_ref() { + parsers.push(Box::new(PathMatcher(s.to_owned()))); }; - if let Some(p) = self - .path_parser - .as_ref() - .and_then(|m| m.parse_project(path_buf)) - { - return Some(p); + #[cfg(feature = "git")] + if config.git { + parsers.push(Box::new(crate::project::git::GitMatcher)); }; - None + Self { parsers, iter } } } @@ -84,7 +49,7 @@ impl Iterator for Entry { fn next(&mut self) -> Option { match self.iter.next()? { - Ok(dir_entry) => self.parse_project(dir_entry.into_path()), + Ok(dir_entry) => self.parsers.parse(dir_entry.into_path()), Err(err) => { error!(%err, "Ignoring errored path"); None -- cgit v1.2.3-70-g09d2