From da884530e2b3e0b9a5bef9abcf683a970b93bd6b Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Mon, 28 Nov 2022 16:09:29 -0600 Subject: feat: add git and preview (default) features --- src/project/path.rs | 78 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 28 deletions(-) (limited to 'src/project/path.rs') diff --git a/src/project/path.rs b/src/project/path.rs index 14ce308..03bcac6 100644 --- a/src/project/path.rs +++ b/src/project/path.rs @@ -1,12 +1,39 @@ -use ignore::DirEntry; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; +use std::time::{Duration, SystemTime}; +use tracing::debug; + +use super::{ProjectItem, ProjectParser}; + +#[derive(Debug, Clone)] +pub struct PathMatcher(pub String); + +impl ProjectParser for PathMatcher { + #[tracing::instrument] + fn parse_project(&self, path_buf: PathBuf) -> Option { + if path_buf.join(&self.0).exists() { + Some(Box::new(PathProject::new(path_buf))) + } else { + debug!("Failed to match pattern in directory"); + None + } + } +} #[derive(Debug, PartialEq, Eq, Clone, Default)] -pub struct PathProject(PathBuf); +pub struct PathProject(PathBuf, Duration); impl PathProject { - fn new(path_buf: PathBuf) -> Self { - Self(path_buf) + pub fn new(path_buf: PathBuf) -> Self { + let modified = Self::get_modified(&path_buf).unwrap_or_default(); + Self(path_buf, modified) + } + + fn get_modified(path_buf: &Path) -> Result { + path_buf + .metadata()? + .modified()? + .duration_since(SystemTime::UNIX_EPOCH) + .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string())) } } @@ -16,32 +43,27 @@ impl AsRef for PathProject { } } -impl TryFrom<(&String, DirEntry)> for PathProject { - type Error = String; - - fn try_from((pattern, dir_entry): (&String, DirEntry)) -> Result { - dir_entry - .path() - .join(pattern) - .exists() - .then(|| Self::from(dir_entry.to_owned())) - .ok_or_else(|| { - format!( - "Pattern `{:?}` not found in path: `{:?}`", - pattern, dir_entry - ) - }) +impl AsRef for PathProject { + fn as_ref(&self) -> &Duration { + &self.1 } } -impl From for PathProject { - fn from(value: DirEntry) -> Self { - Self::new(value.into_path()) - } -} +#[cfg(feature = "preview")] +impl super::Preview for PathProject { + type Error = std::io::Error; + + fn preview(&self) -> Result<(), Self::Error> { + use std::io::Write; + use std::process::{Command, Stdio}; + + let output = Command::new("ls") + .arg("-l") + .arg("-a") + .arg(&self.0) + .stdout(Stdio::piped()) + .output()?; -impl From for PathBuf { - fn from(value: PathProject) -> Self { - value.0 + std::io::stdout().write_all(&output.stdout) } } -- cgit v1.2.3-70-g09d2