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
/// Cache location options.
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum CacheConfig {
/// Stores cache in the current working directory.
#[serde(rename = "local")]
Local,
/// Stores cache in the project's `target` directory (default).
#[default]
#[serde(rename = "target")]
Target,
/// Stores cache in the system's local configuration directory.
#[serde(rename = "global")]
Global,
/// Stores cache in a user-specified file path.
#[serde(rename = "file")]
File(std::path::PathBuf),
}
impl CacheConfig {
/// Returns the root directory for the cache.
pub fn root(&self) -> std::path::PathBuf {
match self {
Self::Local => std::env::current_dir().unwrap(),
Self::Target => {
let dir_original = std::env::current_dir().unwrap();
let mut dir = dir_original.clone();
// Search for Cargo.toml in parent directories to locate project root.
loop {
if let Ok(true) = std::fs::exists(dir.join("Cargo.toml")) {
return dir.join("target");
}
if !dir.pop() {
break;
}
}
dir_original.join("target")
}
Self::Global => dirs::config_local_dir().unwrap(),
Self::File(path_buf) => path_buf.clone(),
}
}
}