diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-05-04 19:43:08 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-05-04 19:43:08 -0500 |
commit | 5da4fb25f9bb72771c7f2798daa6339be0bc3900 (patch) | |
tree | c30639c7a95e1d94baff498d247eed43fdb3728e /src/project.rs | |
parent | 589cd987217755e1da7acbc304c373a75a9f7db5 (diff) |
refactor: join iterators into a single walk
Diffstat (limited to 'src/project.rs')
-rw-r--r-- | src/project.rs | 57 |
1 files changed, 11 insertions, 46 deletions
diff --git a/src/project.rs b/src/project.rs index 2b3e028..b71a3b8 100644 --- a/src/project.rs +++ b/src/project.rs @@ -1,58 +1,23 @@ use std::{ - ops::{Deref, DerefMut}, + convert::Infallible, path::PathBuf, time::{Duration, SystemTime}, }; -use tracing::warn; +pub trait Generator { + type Error: std::error::Error; -pub mod path; - -#[cfg(feature = "git")] -pub mod git; - -pub trait ProjectParser { - fn parse(&self, path_buf: PathBuf) -> Result<Project, Box<dyn std::error::Error>>; -} - -#[derive(Default)] -pub struct ProjectParserGroup { - pub parsers: Vec<Box<dyn ProjectParser>>, + fn generate(self) -> Result<Vec<Project>, Self::Error>; } -impl ProjectParserGroup { - pub fn new() -> Self { - Self::default() - } - - pub fn parse(&self, path_buf: std::path::PathBuf) -> Option<Project> { - if self.parsers.is_empty() { - return path_buf.try_into().ok(); - } - - self.iter() - .map(|p| p.parse(path_buf.to_owned())) - .inspect(|res| { - if let Err(err) = res { - warn!(%err, "Parser failed to match"); - } - }) - .flatten() - .reduce(|max, p| p.max(max)) - } -} - -impl Deref for ProjectParserGroup { - type Target = Vec<Box<dyn ProjectParser>>; - - fn deref(&self) -> &Self::Target { - &self.parsers - } -} +impl<T> Generator for T +where + T: IntoIterator<Item = Project>, +{ + type Error = Infallible; -impl DerefMut for ProjectParserGroup { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.parsers + fn generate(self) -> Result<Vec<Project>, Self::Error> { + Ok(self.into_iter().collect()) } } |