hyprshell_core_lib/
path.rs

1use std::env;
2use std::path::PathBuf;
3use tracing::trace;
4
5pub fn get_default_config_path() -> 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.toml");
12    if path.exists() {
13        trace!("Found config file at {path:?}");
14        return path;
15    }
16
17    path.set_extension("ron");
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("toml");
39    path
40}
41
42#[must_use]
43pub fn get_default_css_path() -> 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/// # Panics
76/// if neither `XDG_DATA_HOME` nor HOME is set
77///
78/// returns `XDG_DATA_HOME` or `$HOME/.local/share`
79#[must_use]
80pub fn get_data_home() -> PathBuf {
81    env::var_os("XDG_DATA_HOME")
82        .map(PathBuf::from)
83        .or_else(|| {
84            env::var_os("HOME")
85                .map(|home| PathBuf::from(format!("{}/.local/share", home.to_string_lossy())))
86        })
87        .expect("Failed to get config dir (XDG_DATA_HOME or HOME not set)")
88}
89
90/// # Panics
91/// if neither `XDG_CACHE_HOME` nor HOME is set
92///
93/// Returns `XDG_CACHE_HOME` or `$HOME/.cache`
94#[must_use]
95pub fn get_cache_home() -> PathBuf {
96    env::var_os("XDG_CACHE_HOME")
97        .map(PathBuf::from)
98        .or_else(|| {
99            env::var_os("HOME")
100                .map(|home| PathBuf::from(format!("{}/.cache", home.to_string_lossy())))
101        })
102        .expect("Failed to get config dir (XDG_CACHE_HOME or HOME not set)")
103}
104
105/// # Panics
106/// if neither `XDG_CONFIG_HOME` nor HOME is set
107///
108/// Returns `XDG_CONFIG_HOME` or `$HOME/.config`
109#[must_use]
110pub fn get_config_home() -> PathBuf {
111    env::var_os("XDG_CONFIG_HOME")
112        .map(PathBuf::from)
113        .or_else(|| {
114            env::var_os("HOME")
115                .map(|home| PathBuf::from(format!("{}/.config", home.to_string_lossy())))
116        })
117        .expect("Failed to get config dir (XDG_CONFIG_HOME or HOME not set)")
118}
119
120/// Returns `XDG_CONFIG_DIRS` or `/etc/xdg/`
121#[must_use]
122pub fn get_config_dirs() -> Vec<PathBuf> {
123    env::var_os("XDG_CONFIG_DIRS").map_or_else(
124        || vec![PathBuf::from("/etc/xdg/")],
125        |val| env::split_paths(&val).collect(),
126    )
127}
128
129/// Returns `XDG_DATA_DIRS` or `/usr/local/share` and `/usr/share`
130#[must_use]
131pub fn get_data_dirs() -> Vec<PathBuf> {
132    env::var_os("XDG_DATA_DIRS").map_or_else(
133        || {
134            vec![
135                PathBuf::from("/usr/local/share"),
136                PathBuf::from("/usr/share"),
137            ]
138        },
139        |val| env::split_paths(&val).collect(),
140    )
141}