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
use crate::CompactStr;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Dir {
pub prefix: DirPrefix,
pub salt: CompactStr,
pub path: CompactStr,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CacheDir {
pub prefix: DirPrefix,
pub path: CompactStr,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Include {
pub prefix: DirPrefix,
pub ignore_missing: bool,
pub path: CompactStr,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RemapDir {
pub prefix: DirPrefix,
pub as_path: CompactStr,
pub salt: CompactStr,
pub path: CompactStr,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DirPrefix {
Default,
Cwd,
Xdg,
Relative,
}
parse_enum! {
DirPrefix,
(Default, "default"),
(Cwd, "cwd"),
(Xdg, "xdg"),
(Relative, "relative"),
}
impl Default for DirPrefix {
fn default() -> Self {
DirPrefix::Default
}
}
macro_rules! define_calculate_path {
($ty:ident, $xdg_env:expr, $xdg_fallback:expr) => {
impl $ty {
pub const XDG_ENV: &'static str = $xdg_env;
pub const XDG_FALLBACK_PATH: &'static str = $xdg_fallback;
pub fn calculate_path<P: AsRef<std::path::Path> + ?Sized>(
&self,
config_file_path: &P,
) -> std::path::PathBuf {
match self.prefix {
DirPrefix::Default => self.path.as_str().into(),
DirPrefix::Cwd => std::path::Path::new(".").join(self.path.as_str()),
DirPrefix::Relative => match config_file_path.as_ref().parent() {
Some(parent) => parent.join(self.path.as_str()),
None => std::path::Path::new(".").join(self.path.as_str()),
},
DirPrefix::Xdg => std::path::PathBuf::from(
std::env::var($xdg_env).unwrap_or_else(|_| $xdg_fallback.into()),
)
.join(self.path.as_str()),
}
}
}
};
}
define_calculate_path!(Dir, "XDG_DATA_HOME", "~/.local/share");
define_calculate_path!(CacheDir, "XDG_CACHE_HOME", "~/.cache");
define_calculate_path!(Include, "XDG_CONFIG_HOME", "~/.config");
define_calculate_path!(RemapDir, "XDG_CONFIG_HOME", "~/.config");