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(&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, Duration); impl PathProject { 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())) } } impl AsRef for PathProject { fn as_ref(&self) -> &PathBuf { &self.0 } } impl AsRef for PathProject { fn as_ref(&self) -> &Duration { &self.1 } } #[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()?; std::io::stdout().write_all(&output.stdout) } }