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
use directories_next::{BaseDirs, ProjectDirs, UserDirs};
use std::path::{Path, PathBuf};

pub fn get_default_config_path() -> PathBuf {
    if let Some(project_dirs) = ProjectDirs::from("dev", "reynn", "vers") {
        project_dirs.config_dir().to_path_buf()
    } else if let Some(base_dirs) = BaseDirs::new() {
        base_dirs.config_dir().join("vers")
    } else if let Some(user_dirs) = UserDirs::new() {
        user_dirs.home_dir().join(".config").join("vers")
    } else {
        panic!("Unable to determine a base directory for user configs")
    }
}

pub fn get_tool_link_path(base_path: &'_ Path, tool_alias: &'_ str) -> PathBuf {
    base_path.join(tool_alias)
}

pub fn get_tool_version_download_dir(
    base_path: &'_ Path,
    name: &'_ str,
    version: &'_ str,
) -> PathBuf {
    get_tool_download_dir(base_path, name).join(version)
}

pub fn get_tool_download_dir(base_path: &'_ Path, name: &'_ str) -> PathBuf {
    base_path
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("tools")
        .join(name)
}

#[cfg(test)]
mod test {
    use {
        super::*,
        std::path::{Path, PathBuf},
        test_case::test_case,
    };

    #[test_case(
        Path::new("/home/test/.config/vers/envs/global"),
        "cli/cli",
        Path::new("/home/test/.config/vers/tools/cli/cli").to_path_buf() ; "cli/cli (linux)"
    )]
    #[test_case(
        Path::new("/Users/test/Library/Application Support/dev.reynn.vers/envs/global"),
        "neovim/neovim",
        Path::new("/Users/test/Library/Application Support/dev.reynn.vers/tools/neovim/neovim").to_path_buf() ; "neovim/neovim (osx)"
    )]
    pub fn test_tool_download_dir(input_path: &'_ Path, tool_name: &'_ str, expected: PathBuf) {
        assert_eq!(get_tool_download_dir(input_path, tool_name), expected)
    }

    #[test_case(
        Path::new("/home/test/.config/vers/envs/global"),
        "cli/cli",
        "v2.14.3",
        Path::new("/home/test/.config/vers/tools/cli/cli/v2.14.3").to_path_buf() ; "cli/cli@v2.14.3 (linux)"
    )]
    #[test_case(
        Path::new("/Users/test/Library/Application Support/dev.reynn.vers/envs/global"),
        "cli/cli",
        "v2.14.3",
        Path::new("/Users/test/Library/Application Support/dev.reynn.vers/tools/cli/cli/v2.14.3").to_path_buf() ; "cli/cli@v2.14.3 (osx)"
    )]
    pub fn test_get_tool_version_download_dir(
        input: &'_ Path,
        tool_name: &'_ str,
        version: &'_ str,
        expected: PathBuf,
    ) {
        assert_eq!(
            get_tool_version_download_dir(input, tool_name, version),
            expected
        )
    }

    #[test_case(
        Path::new("/home/test/.config/vers/envs/global"),
        "cli",
        Path::new("/home/test/.config/vers/envs/global/cli").to_path_buf() ;
        "global cli"
    )]
    #[test_case(
        Path::new("/home/test/.config/vers/envs/default"),
        "gh",
        Path::new("/home/test/.config/vers/envs/default/gh").to_path_buf() ;
        "default gh"
    )]
    pub fn test_get_tool_link_path(input: &'_ Path, tool_alias: &'_ str, expected: PathBuf) {
        assert_eq!(get_tool_link_path(input, tool_alias), expected)
    }
}