|
1 | | -use std::{collections::HashMap, path::PathBuf}; |
| 1 | +use std::{borrow::Cow, collections::HashMap, env, path::PathBuf}; |
2 | 2 |
|
3 | 3 | #[derive(Debug, Clone)] |
4 | 4 | pub struct Env { |
5 | 5 | pub pairs: HashMap<String, String>, |
6 | 6 | } |
7 | 7 |
|
8 | 8 | impl Env { |
| 9 | + /// Parses an environment string into key-value pairs. |
9 | 10 | fn parse_env(env: &str) -> HashMap<String, String> { |
10 | | - let mut pairs = HashMap::new(); |
11 | | - for line in env.lines() { |
12 | | - let mut parts = line.splitn(2, '='); |
13 | | - if let (Some(key), Some(value)) = (parts.next(), parts.next()) { |
14 | | - pairs.insert(key.to_string(), value.to_string()); |
15 | | - } |
16 | | - } |
17 | | - pairs |
| 11 | + env.lines() |
| 12 | + .filter_map(|line| { |
| 13 | + let mut parts = line.splitn(2, '='); |
| 14 | + Some((parts.next()?.to_string(), parts.next()?.to_string())) |
| 15 | + }) |
| 16 | + .collect() |
18 | 17 | } |
19 | 18 |
|
20 | | - /// Construct the env from the environment variables |
| 19 | + /// Constructs an environment structure from the given string or system environment variables. |
21 | 20 | pub fn construct(env: Option<&str>) -> Self { |
22 | | - let env = match env { |
23 | | - Some(env) => env.to_string(), |
24 | | - None => "".to_string(), |
25 | | - }; |
26 | | - let pairs = Self::parse_env(&env); |
| 21 | + let pairs = env.map(Self::parse_env).unwrap_or_else(|| env::vars().collect()); |
27 | 22 | Self { pairs } |
28 | 23 | } |
29 | 24 |
|
| 25 | + /// Fetches the HOME directory path. |
30 | 26 | pub fn fetch_home(&self) -> Option<PathBuf> { |
31 | | - let home = match self.pairs.get("HOME") { |
32 | | - Some(it) => it, |
33 | | - None => return None, |
34 | | - }; |
35 | | - Some(PathBuf::from(home)) |
| 27 | + self.pairs.get("HOME").map(PathBuf::from) |
36 | 28 | } |
37 | 29 |
|
| 30 | + /// Fetches the XDG config path. |
38 | 31 | pub fn fetch_xdg_config_path(&self) -> PathBuf { |
39 | | - let default = match self.fetch_home() { |
40 | | - Some(x) => x.join(".config"), |
41 | | - None => PathBuf::from("/etc"), |
42 | | - } |
43 | | - .to_string_lossy() |
44 | | - .to_string(); |
45 | | - let xdg_config_home = self.pairs.get("XDG_CONFIG_HOME").unwrap_or(&default); |
| 32 | + let default = self |
| 33 | + .fetch_home() |
| 34 | + .map(|home| home.join(".config")) |
| 35 | + .unwrap_or_else(|| PathBuf::from("/etc")) |
| 36 | + .to_string_lossy() // Convert PathBuf -> Cow<'_, str> |
| 37 | + .into_owned(); |
| 38 | + |
| 39 | + let xdg_config_home = |
| 40 | + self.pairs.get("XDG_CONFIG_HOME").map(String::as_str).unwrap_or(&default); |
46 | 41 |
|
47 | 42 | PathBuf::from(xdg_config_home).join("swhkd").join("swhkdrc") |
48 | 43 | } |
49 | 44 |
|
| 45 | + /// Fetches the XDG data path. |
50 | 46 | pub fn fetch_xdg_data_path(&self) -> PathBuf { |
51 | | - let default = match self.fetch_home() { |
52 | | - Some(x) => x.join(".local").join("share"), |
53 | | - None => PathBuf::from("/etc"), |
54 | | - } |
55 | | - .to_string_lossy() |
56 | | - .to_string(); |
57 | | - let xdg_config_home = self.pairs.get("XDG_DATA_HOME").unwrap_or(&default); |
| 47 | + let default = self |
| 48 | + .fetch_home() |
| 49 | + .map(|home| home.join(".local/share")) |
| 50 | + .unwrap_or_else(|| PathBuf::from("/etc")) |
| 51 | + .to_string_lossy() |
| 52 | + .into_owned(); |
| 53 | + |
| 54 | + let xdg_data_home = self.pairs.get("XDG_DATA_HOME").map(String::as_str).unwrap_or(&default); |
58 | 55 |
|
59 | | - PathBuf::from(xdg_config_home) |
| 56 | + PathBuf::from(xdg_data_home) |
60 | 57 | } |
61 | 58 |
|
| 59 | + /// Fetches the XDG runtime directory path for the given user ID. |
62 | 60 | pub fn xdg_runtime_dir(&self, uid: u32) -> PathBuf { |
63 | 61 | let default = format!("/run/user/{}", uid); |
64 | | - let xdg_runtime_dir = self.pairs.get("XDG_RUNTIME_DIR").unwrap_or(&default); |
| 62 | + |
| 63 | + let xdg_runtime_dir = |
| 64 | + self.pairs.get("XDG_RUNTIME_DIR").map(String::as_str).unwrap_or(&default); |
| 65 | + |
65 | 66 | PathBuf::from(xdg_runtime_dir) |
66 | 67 | } |
67 | 68 | } |
0 commit comments