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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use logix_vfs::LogixVfs;

use crate::{
    error::{ParseError, PathError, Result, SourceSpan, Wanted},
    parser::LogixParser,
    token::{Literal, StrLit, Token},
    type_trait::{LogixTypeDescriptor, LogixValueDescriptor, Value},
    types::{FullPath, NameOnlyPath, ValidPath},
    LogixType,
};
use std::{
    borrow::Cow,
    ffi::OsStr,
    fmt,
    path::{Path, PathBuf},
};

#[derive(Clone, Debug, PartialEq, Eq)]
enum Location {
    Path(FullPath),
    Name(NameOnlyPath),
}

/// The environment used when resolving executable paths
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ExecutableEnv<'a> {
    pub path_env: Option<Cow<'a, OsStr>>,
}

impl<'a> ExecutableEnv<'a> {
    const DEFAULT: ExecutableEnv<'static> = ExecutableEnv { path_env: None };

    pub fn which(&self, name_or_path: impl AsRef<OsStr>) -> Option<FullPath> {
        if let Some(path_env) = self.path_env.as_deref() {
            which::which_in_global(name_or_path, Some(path_env))
                .ok()?
                .next()
                .map(FullPath::try_from)?
                .ok()
        } else {
            which::which_global(name_or_path)
                .ok()
                .map(FullPath::try_from)?
                .ok()
        }
    }
}

/// A path to an executable. This is either a full path, or a filename. If it is a
/// relative path it will fail to avoid issues. For example, imagine what happens
/// if EDITOR is set to a relative path.
#[derive(Clone, PartialEq, Eq)]
pub struct ExecutablePath {
    loc: Location,
}

impl ExecutablePath {
    pub fn as_path(&self) -> &Path {
        match &self.loc {
            Location::Path(v) => v.as_path(),
            Location::Name(v) => v.as_path(),
        }
    }

    pub fn which(&self, env: Option<&ExecutableEnv>) -> Option<FullPath> {
        env.unwrap_or(&ExecutableEnv::DEFAULT).which(self)
    }

    pub fn join(&self, path: impl AsRef<Path>) -> Result<Self, PathError> {
        Ok(Self {
            loc: match &self.loc {
                Location::Path(v) => v.join(path).map(Location::Path)?,
                Location::Name(v) => v.join(path).map(Location::Name)?,
            },
        })
    }

    pub fn with_file_name(&self, name: impl AsRef<OsStr>) -> Self {
        Self {
            loc: match &self.loc {
                Location::Path(v) => Location::Path(v.with_file_name(name)),
                Location::Name(v) => Location::Name(v.with_file_name(name)),
            },
        }
    }

    pub fn with_extension(&self, ext: impl AsRef<OsStr>) -> Self {
        Self {
            loc: match &self.loc {
                Location::Path(v) => Location::Path(v.with_extension(ext)),
                Location::Name(v) => Location::Name(v.with_extension(ext)),
            },
        }
    }
}

impl TryFrom<PathBuf> for ExecutablePath {
    type Error = PathError;

    fn try_from(path: PathBuf) -> Result<Self, PathError> {
        match ValidPath::try_from(path)? {
            ValidPath::Full(path) => Ok(Self {
                loc: Location::Path(path),
            }),
            ValidPath::Name(name) => Ok(Self {
                loc: Location::Name(name),
            }),
            ValidPath::Rel(_) => Err(PathError::NotFullOrNameOnly),
        }
    }
}

impl From<ExecutablePath> for PathBuf {
    fn from(v: ExecutablePath) -> PathBuf {
        match v.loc {
            Location::Path(v) => v.into(),
            Location::Name(v) => v.into(),
        }
    }
}

impl_path_type_traits!(
    ExecutablePath,
    "The name of or full path to an executable file"
);