Skip to content

Commit

Permalink
improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
JojiiOfficial committed Jun 28, 2024
1 parent 7fb37dc commit a3ff6b2
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/daemon/buds_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ impl Config {

// Create missing folders and return the config file
pub async fn get_config_file() -> Result<PathBuf, String> {
let conf_home: PathBuf = match get_xdg_config() {
Some(x) => x,
None => get_home_dir().unwrap().join(".config")
};
let conf_dir: PathBuf = conf_home.join("livebuds");
let conf_home = get_xdg_config()
.or_else(|| get_home_dir().map(|i| i.join(".config")))
.expect("Can't determine home directory!");
let conf_dir = conf_home.join("livebuds");

if !conf_dir.exists().await {
fs::create_dir_all(&conf_dir)
Expand All @@ -181,17 +180,19 @@ impl Config {
}

pub fn get_xdg_config() -> Option<PathBuf> {
std::env::var_os("XDG_CONFIG_HOME")
.and_then(|xdg| if xdg.is_empty() { None } else { Some(xdg) })
.or(None)
.map(PathBuf::from)
try_env_var("XDG_CONFIG_HOME")
}

pub fn get_home_dir() -> Option<PathBuf> {
std::env::var_os("HOME")
.and_then(|home| if home.is_empty() { None } else { Some(home) })
.or(None)
.map(PathBuf::from)
try_env_var("HOME")
}

fn try_env_var(name: &str) -> Option<PathBuf>{
let xdg = std::env::var_os(name)?;
if xdg.is_empty(){
return None;
}
Some(PathBuf::from(xdg))
}

impl BudsConfig {
Expand Down

0 comments on commit a3ff6b2

Please sign in to comment.