diff options
Diffstat (limited to 'src/search/entry.rs')
-rw-r--r-- | src/search/entry.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/search/entry.rs b/src/search/entry.rs new file mode 100644 index 0000000..6e94b35 --- /dev/null +++ b/src/search/entry.rs @@ -0,0 +1,57 @@ +use ignore::{DirEntry, Walk}; +use tracing::error; + +use crate::{ + project::{GitProject, PathProject}, + Project, +}; + +pub use config::Config; + +mod config; + +pub struct Entry { + config: Config, + iter: Walk, +} + +impl std::fmt::Debug for Entry { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SearchPath") + .field("config", &self.config) + .finish() + } +} + +impl Entry { + pub fn match_project(&self, dir_entry: DirEntry) -> Option<Box<dyn Project>> { + if self.config.git { + if let Ok(git) = GitProject::try_from(dir_entry.to_owned()) { + return Some(Box::new(git)); + }; + }; + + if let Some(pattern) = &self.config.pattern { + if let Ok(proj) = PathProject::try_from((pattern, dir_entry)) { + return Some(Box::new(proj)); + }; + }; + + None + } +} + +impl Iterator for Entry { + type Item = Box<dyn Project>; + + #[tracing::instrument] + fn next(&mut self) -> Option<Self::Item> { + match self.iter.next()? { + Ok(dir_entry) => self.match_project(dir_entry), + Err(err) => { + error!(%err, "Ignoring errored path"); + self.next() + } + } + } +} |