From c7319caf38ff22e67f6fb625411ed554cf3b1f92 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 27 Nov 2022 19:23:11 -0600 Subject: feat: improve Project trait --- src/project/git.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'src/project/git.rs') diff --git a/src/project/git.rs b/src/project/git.rs index f1c4d5a..00cd000 100644 --- a/src/project/git.rs +++ b/src/project/git.rs @@ -1,8 +1,11 @@ -use crate::{Error, Project}; use git2::{BranchType, Repository}; use ignore::DirEntry; use std::{cmp::Ordering, path::PathBuf, time::Duration}; +use crate::project::Error; +use crate::project::Timestamp; +use crate::Project; + #[derive(Debug, Clone)] pub struct GitProject { path_buf: PathBuf, @@ -11,19 +14,19 @@ pub struct GitProject { impl GitProject { fn new(path_buf: PathBuf) -> Result { - let repository = Repository::open(&path_buf)?; - let latest_commit = Self::latest_commit(&repository); + let latest_commit = Self::latest_commit(&path_buf).ok(); Ok(Self { path_buf, latest_commit, }) } - fn latest_commit(repository: &Repository) -> Option { - let mut branches = repository.branches(Some(BranchType::Local)).ok()?; + fn latest_commit(path_buf: &PathBuf) -> Result { + let repository = Repository::open(path_buf)?; + let mut branches = repository.branches(Some(BranchType::Local))?; branches .try_fold(0, |latest, branch| { - let branch = branch?.0; + let (branch, _) = branch?; let name = branch .name()? @@ -35,17 +38,24 @@ impl GitProject { .map(|c| (c.time().seconds() as u64).max(latest)) }) .map(Duration::from_secs) - .ok() + .map_err(Into::into) } } -impl Project for GitProject { - fn timestamp(&self) -> Option { - self.latest_commit +impl Timestamp for GitProject { + type Error = Error; + + fn timestamp(&self) -> Result { + match self.latest_commit { + Some(t) => Ok(t), + None => Self::latest_commit(&self.path_buf), + } } +} - fn to_path_buf(&self) -> PathBuf { - self.path_buf.to_owned() +impl Project for GitProject { + fn to_path_buf(&self) -> &PathBuf { + &self.path_buf } } -- cgit v1.2.3-70-g09d2