aboutsummaryrefslogtreecommitdiffstats
path: root/src/project/path.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-11-28 16:09:29 -0600
committerToby Vincent <tobyv13@gmail.com>2022-11-28 16:09:29 -0600
commitda884530e2b3e0b9a5bef9abcf683a970b93bd6b (patch)
tree84653f0fc4e643bb2d2c6d6a1eff79fdd94fbf07 /src/project/path.rs
parentf7eeef26d5a251c2a925d18d288fec2fa205f59d (diff)
feat: add git and preview (default) features
Diffstat (limited to 'src/project/path.rs')
-rw-r--r--src/project/path.rs78
1 files changed, 50 insertions, 28 deletions
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<ProjectItem> {
+ 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<Duration, std::io::Error> {
+ 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<PathBuf> for PathProject {
}
}
-impl TryFrom<(&String, DirEntry)> for PathProject {
- type Error = String;
-
- fn try_from((pattern, dir_entry): (&String, DirEntry)) -> Result<Self, Self::Error> {
- 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<Duration> for PathProject {
+ fn as_ref(&self) -> &Duration {
+ &self.1
}
}
-impl From<DirEntry> 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<PathProject> for PathBuf {
- fn from(value: PathProject) -> Self {
- value.0
+ std::io::stdout().write_all(&output.stdout)
}
}