config.rs: better env variable matching

This commit is contained in:
Emma Tebibyte 2023-03-25 16:23:26 -04:00
parent 8fa2bd97af
commit 7b4d68e963
Signed by: emma
GPG Key ID: 6D661C738815E7DD
1 changed files with 41 additions and 24 deletions

View File

@ -57,31 +57,48 @@ pub struct PageMeta {
pub fn get_path(xdg: &str) -> Result<String, (String, u32)> {
let dir: String;
if unsafe { libc::geteuid() } == 0 {
dir = match xdg {
"XDG_CACHE_HOME" => "/var".to_owned(),
"XDG_CONFIG_HOME" => "/etc".to_owned(),
_ => panic!("nya"),
};
} else {
dir = match env::var(xdg) {
Ok(var) => var,
Err(_) => {
match env::var("HOME") {
Ok(home) => format!("{}/.config", home),
Err(_) => {
return Err((
"Unable to determine path to current users home directory."
.to_string(),
EX_UNAVAILABLE
));
},
}
},
};
}
let home_dir = match env::var("HOME").ok() {
Some(home) => home,
None => {
return Err((
"Unable to determine path to current users home directory."
.to_string(),
EX_UNAVAILABLE
));
}
};
let mut root = false;
if unsafe { libc::geteuid() } == 0 { root = true; }
dir = match (root, env::var(xdg).ok()) {
(false, Some(var)) => var,
(su, None) => {
match xdg {
"XDG_CACHE_HOME" => {
if su { "/var".to_owned() }
else { format!("{}/.cache", home_dir) }
},
"XDG_CONFIG_HOME" => {
if su { "/etc".to_owned() }
else { format!("{}/.config", home_dir) }
},
"XDG_DATA_HOME" => {
if su { "/usr/share".to_owned() }
else { format!("{}/.local/share", home_dir) }
},
_ => {
return Err(
(format!("{}: Unimplemented variable.", xdg), EX_SOFTWARE)
);
},
}
},
_ => {
panic!("nya");
},
};
Ok(format!("{}/plaque", dir))
}