crud_path/
common.rs

1use crate::DELIMITER;
2use std::ffi::OsStr;
3
4pub(crate) fn exec<S, I>(cmd: S, args: I) -> bool
5where
6    I: IntoIterator<Item = S>,
7    S: AsRef<OsStr>,
8{
9    std::process::Command::new(cmd)
10        .envs(std::env::vars())
11        .args(args)
12        .output()
13        .is_ok()
14}
15
16pub fn get_path() -> Vec<String> {
17    let path = std::env::var("PATH")
18        .expect("Failed to get PATH")
19        .to_string();
20    path.split(DELIMITER)
21        .map(|s| s.replace("\\", "/").to_string())
22        .collect()
23}
24
25pub fn has_path(path: &str) -> bool {
26    #[cfg(windows)]
27    let path = to_win_path(path).replace("\\", "/");
28    get_path()
29        .iter()
30        .any(|i| i.eq_ignore_ascii_case(&path))
31}
32
33// c:/a/b -> C:\a\b
34pub fn to_win_path(path: &str) -> String {
35    let mut path = path.replace("/", "\\");
36    if let Some(s) = path.as_mut_str().get_mut(0..3) {
37        if s.ends_with(":\\") {
38            s.make_ascii_uppercase();
39        }
40    }
41    path
42}
43
44pub fn is_msys() -> bool {
45    std::env::var("MSYSTEM").is_ok()
46}
47
48// C:\a\b -> /c/a/b
49pub fn to_msys_path(path: &str) -> String {
50    let mut path = path.replace("\\", "/");
51    if let Some(s) = path.as_mut_str().get_mut(0..3) {
52        if s.len() == 3 && s.ends_with(":/") {
53            unsafe {
54                let p = s.as_mut_ptr();
55                let name = (*p).to_ascii_lowercase();
56                *p = b'/';
57                *(p.wrapping_add(1)) = name;
58                *(p.wrapping_add(2)) = b'/';
59            };
60        }
61    }
62    path
63}
64
65#[cfg(test)]
66mod test {
67    use super::get_path;
68
69    #[test]
70    fn test_get_path() {
71        let path = get_path();
72        assert!(!path.is_empty())
73    }
74}