aboutsummaryrefslogtreecommitdiffstats
path: root/src
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
parentda884530e2b3e0b9a5bef9abcf683a970b93bd6b (diff)
feat: create `ProjectParserGroup` to group parsers
Diffstat (limited to 'src')
-rw-r--r--src/project.rs15
-rw-r--r--src/project/git.rs2
-rw-r--r--src/project/path.rs2
-rw-r--r--src/search/entry.rs57
4 files changed, 24 insertions, 52 deletions
diff --git a/src/project.rs b/src/project.rs
index 5486dc1..18c2083 100644
--- a/src/project.rs
+++ b/src/project.rs
@@ -8,6 +8,17 @@ pub mod path;
#[cfg(feature = "git")]
pub mod git;
+pub type ProjectParserGroup = Vec<Box<dyn ProjectParser>>;
+
+pub trait ProjectParser {
+ fn parse(&self, path_buf: PathBuf) -> Option<ProjectItem>;
+}
+
+impl ProjectParser for ProjectParserGroup {
+ fn parse(&self, path_buf: std::path::PathBuf) -> Option<ProjectItem> {
+ self.iter().find_map(|p| p.parse(path_buf.to_owned()))
+ }
+}
pub type ProjectItem = Box<dyn Project>;
@@ -25,10 +36,6 @@ where
}
}
-pub trait ProjectParser {
- fn parse_project(&self, path_buf: PathBuf) -> Option<ProjectItem>;
-}
-
pub trait Timestamp {
fn timestamp(&self) -> &Duration;
}
diff --git a/src/project/git.rs b/src/project/git.rs
index efe4ade..759f632 100644
--- a/src/project/git.rs
+++ b/src/project/git.rs
@@ -12,7 +12,7 @@ pub struct GitMatcher;
impl ProjectParser for GitMatcher {
#[tracing::instrument]
- fn parse_project(&self, path_buf: PathBuf) -> Option<super::ProjectItem> {
+ fn parse(&self, path_buf: PathBuf) -> Option<super::ProjectItem> {
match GitProject::new(path_buf) {
Ok(g) => Some(Box::new(g)),
Err(err) => {
diff --git a/src/project/path.rs b/src/project/path.rs
index 03bcac6..09eb557 100644
--- a/src/project/path.rs
+++ b/src/project/path.rs
@@ -9,7 +9,7 @@ pub struct PathMatcher(pub String);
impl ProjectParser for PathMatcher {
#[tracing::instrument]
- fn parse_project(&self, path_buf: PathBuf) -> Option<ProjectItem> {
+ fn parse(&self, path_buf: PathBuf) -> Option<ProjectItem> {
if path_buf.join(&self.0).exists() {
Some(Box::new(PathProject::new(path_buf)))
} else {
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