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
use std::{env, error, fmt, io, path::PathBuf};

#[cfg(unix)]
mod unix;
#[cfg(unix)]
pub use unix::executables;

#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub use windows::executables;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Executable {
    pub name: String,
    pub path: PathBuf,
}

#[derive(Debug)]
pub enum ExeError {
    Io(io::Error),
    Env(env::VarError),
}

impl fmt::Display for ExeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ExeError::Io(ref e) => e.fmt(f),
            ExeError::Env(ref e) => e.fmt(f),
        }
    }
}

impl error::Error for ExeError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            ExeError::Io(ref e) => Some(e),
            ExeError::Env(ref e) => Some(e),
        }
    }
}

impl From<io::Error> for ExeError {
    fn from(err: io::Error) -> ExeError {
        ExeError::Io(err)
    }
}

impl From<env::VarError> for ExeError {
    fn from(err: env::VarError) -> ExeError {
        ExeError::Env(err)
    }
}