hyprshell_core_lib/
path.rs

1use std::env;
2use std::path::PathBuf;
3use tracing::trace;
4
5pub fn get_default_config_file() -> PathBuf {
6    let mut path = get_config_home();
7    #[cfg(debug_assertions)]
8    path.push("hyprshell.debug/");
9    #[cfg(not(debug_assertions))]
10    path.push("hyprshell/");
11    path.push("config.ron");
12    if path.exists() {
13        trace!("Found config file at {path:?}");
14        return path;
15    }
16
17    path.set_extension("toml");
18    if path.exists() {
19        trace!("Found config file at {path:?}");
20        return path;
21    }
22
23    path.set_extension("json");
24    if path.exists() {
25        trace!("Found config file at {path:?}");
26        return path;
27    }
28
29    #[cfg(feature = "json5_config")]
30    {
31        path.set_extension("json5");
32        if path.exists() {
33            trace!("Found config file at {path:?}");
34            return path;
35        }
36    }
37
38    path.set_extension("ron");
39    path
40}
41
42#[must_use]
43pub fn get_default_css_file() -> PathBuf {
44    let mut path = get_config_home();
45
46    #[cfg(debug_assertions)]
47    path.push("hyprshell.debug/styles.css");
48    #[cfg(not(debug_assertions))]
49    path.push("hyprshell/styles.css");
50    path
51}
52
53#[must_use]
54pub fn get_default_data_dir() -> PathBuf {
55    let mut path = get_data_home();
56
57    #[cfg(debug_assertions)]
58    path.push("hyprshell.debug");
59    #[cfg(not(debug_assertions))]
60    path.push("hyprshell");
61    path
62}
63
64#[must_use]
65pub fn get_default_cache_dir() -> PathBuf {
66    let mut path = get_cache_home();
67
68    #[cfg(debug_assertions)]
69    path.push("hyprshell.debug");
70    #[cfg(not(debug_assertions))]
71    path.push("hyprshell");
72    path
73}
74
75#[must_use]
76pub fn get_default_system_data_dir() -> PathBuf {
77    let mut path = get_system_data_home();
78
79    #[cfg(debug_assertions)]
80    path.push("hyprshell.debug");
81    #[cfg(not(debug_assertions))]
82    path.push("hyprshell");
83    path
84}
85
86/// # Panics
87/// if neither `XDG_DATA_HOME` nor HOME is set
88///
89/// returns `XDG_DATA_HOME` or `$HOME/.local/share`
90#[must_use]
91pub fn get_data_home() -> PathBuf {
92    env::var_os("XDG_DATA_HOME")
93        .map(PathBuf::from)
94        .or_else(|| {
95            env::var_os("HOME")
96                .map(|home| PathBuf::from(format!("{}/.local/share", home.to_string_lossy())))
97        })
98        .expect("Failed to get config dir (XDG_DATA_HOME or HOME not set)")
99}
100
101/// returns `/usr/share`
102#[must_use]
103pub fn get_system_data_home() -> PathBuf {
104    PathBuf::from("/usr/share")
105}
106
107/// # Panics
108/// if neither `XDG_CACHE_HOME` nor HOME is set
109///
110/// Returns `XDG_CACHE_HOME` or `$HOME/.cache`
111#[must_use]
112pub fn get_cache_home() -> PathBuf {
113    env::var_os("XDG_CACHE_HOME")
114        .map(PathBuf::from)
115        .or_else(|| {
116            env::var_os("HOME")
117                .map(|home| PathBuf::from(format!("{}/.cache", home.to_string_lossy())))
118        })
119        .expect("Failed to get config dir (XDG_CACHE_HOME or HOME not set)")
120}
121
122/// # Panics
123/// if neither `XDG_CONFIG_HOME` nor HOME is set
124///
125/// Returns `XDG_CONFIG_HOME` or `$HOME/.config`
126#[must_use]
127pub fn get_config_home() -> PathBuf {
128    env::var_os("XDG_CONFIG_HOME")
129        .map(PathBuf::from)
130        .or_else(|| {
131            env::var_os("HOME")
132                .map(|home| PathBuf::from(format!("{}/.config", home.to_string_lossy())))
133        })
134        .expect("Failed to get config dir (XDG_CONFIG_HOME or HOME not set)")
135}
136
137/// Returns `XDG_CONFIG_DIRS` or `/etc/xdg/`
138#[must_use]
139pub fn get_config_dirs() -> Vec<PathBuf> {
140    env::var_os("XDG_CONFIG_DIRS").map_or_else(
141        || vec![PathBuf::from("/etc/xdg/")],
142        |val| env::split_paths(&val).collect(),
143    )
144}
145
146/// Returns `XDG_DATA_DIRS` or `/usr/local/share` and `/usr/share`
147#[must_use]
148pub fn get_data_dirs() -> Vec<PathBuf> {
149    env::var_os("XDG_DATA_DIRS").map_or_else(
150        || {
151            vec![
152                PathBuf::from("/usr/local/share"),
153                PathBuf::from("/usr/share"),
154            ]
155        },
156        |val| env::split_paths(&val).collect(),
157    )
158}