summaryrefslogtreecommitdiffstats
path: root/src/search
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-11-29 00:07:33 -0600
committerToby Vincent <tobyv13@gmail.com>2022-11-29 00:23:45 -0600
commit09f51336df00b38928c4b42782687012b9bfae39 (patch)
tree743257228256e81ed28783b4fb316bbff5939c65 /src/search
parentda884530e2b3e0b9a5bef9abcf683a970b93bd6b (diff)
feat: create `ProjectParserGroup` to group parsers
Diffstat (limited to 'src/search')
-rw-r--r--src/search/entry.rs57
1 files changed, 11 insertions, 46 deletions
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<PathMatcher>,
-
- #[cfg(feature = "git")]
- git_parser: Option<crate::project::git::GitMatcher>,
-
+ 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<ProjectItem> {
- #[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<Self::Item> {
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