summaryrefslogtreecommitdiffstats
path: root/src/project.rs
blob: e6faf663240225aab2c0bead5c10aa97c2ad9e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::{
    io::Write,
    path::PathBuf,
    process::{Command, Stdio},
    time::{Duration, SystemTime},
};

pub use self::error::Error;
pub use self::git::GitProject;
pub use self::path::PathProject;

mod error;
mod git;
mod path;

pub type ProjectItem = Box<dyn Project<Error = Error>>;

pub trait Project: Timestamp {
    fn to_path_buf(&self) -> &PathBuf;
}

impl<T> Project for T
where
    T: Timestamp,
    T: AsRef<PathBuf>,
{
    fn to_path_buf(&self) -> &PathBuf {
        self.as_ref()
    }
}

pub trait Timestamp {
    type Error;

    fn timestamp(&self) -> Result<Duration, Self::Error>;
}

impl<T> Timestamp for T
where
    T: AsRef<PathBuf>,
{
    type Error = Error;

    fn timestamp(&self) -> Result<Duration, Self::Error> {
        self.as_ref()
            .metadata()?
            .modified()?
            .duration_since(SystemTime::UNIX_EPOCH)
            .map_err(Into::into)
    }
}

pub trait Preview {
    type Error;

    fn preview(&self) -> Result<(), Self::Error>;
}

impl<T> Preview for T
where
    T: AsRef<PathBuf>,
{
    type Error = std::io::Error;

    fn preview(&self) -> Result<(), Self::Error> {
        let output = Command::new("ls")
            .arg("-l")
            .arg("-a")
            .arg(self.to_path_buf())
            .stdout(Stdio::piped())
            .output()?;

        std::io::stdout().write_all(&output.stdout)
    }
}