-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: environment variable initialization (#243)
* Refactor file path handling and environment variable initialization
- Loading branch information
1 parent
34b9619
commit 994b74f
Showing
2 changed files
with
98 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! Environ.rs | ||
//! Defines modules and structs for handling environment variables and paths. | ||
use std::{env::VarError, path::PathBuf}; | ||
|
||
use nix::unistd; | ||
|
||
// The main struct for handling environment variables. | ||
// Contains the values of the environment variables in the form of PathBuffers. | ||
pub struct Env { | ||
pub data_home: PathBuf, | ||
pub home: PathBuf, | ||
pub runtime_dir: PathBuf, | ||
} | ||
|
||
/// Error type for the Env struct. | ||
/// Contains all the possible errors that can occur when trying to get an environment variable. | ||
#[derive(Debug)] | ||
pub enum EnvError { | ||
DataHomeNotSet, | ||
HomeNotSet, | ||
RuntimeDirNotSet, | ||
GenericError(String), | ||
} | ||
|
||
impl Env { | ||
/// Constructs a new Env struct. | ||
/// This function is called only once and the result is stored in a static variable. | ||
pub fn construct() -> Self { | ||
let home = match Self::get_env("HOME") { | ||
Ok(val) => val, | ||
Err(_) => { | ||
eprintln!("HOME Variable is not set, cannot fall back on hardcoded path for XDG_DATA_HOME."); | ||
std::process::exit(1); | ||
} | ||
}; | ||
|
||
let data_home = match Self::get_env("XDG_DATA_HOME") { | ||
Ok(val) => val, | ||
Err(e) => match e { | ||
EnvError::DataHomeNotSet => { | ||
log::warn!( | ||
"XDG_DATA_HOME Variable is not set, falling back on hardcoded path." | ||
); | ||
home.join(".local/share") | ||
} | ||
_ => panic!("Unexpected error: {:#?}", e), | ||
}, | ||
}; | ||
|
||
let runtime_dir = match Self::get_env("XDG_RUNTIME_DIR") { | ||
Ok(val) => val, | ||
Err(e) => match e { | ||
EnvError::RuntimeDirNotSet => { | ||
log::warn!( | ||
"XDG_RUNTIME_DIR Variable is not set, falling back on hardcoded path." | ||
); | ||
PathBuf::from(format!("/run/user/{}", unistd::Uid::current())) | ||
} | ||
_ => panic!("Unexpected error: {:#?}", e), | ||
}, | ||
}; | ||
|
||
Self { data_home, home, runtime_dir } | ||
} | ||
|
||
/// Actual interface to get the environment variable. | ||
fn get_env(name: &str) -> Result<PathBuf, EnvError> { | ||
match std::env::var(name) { | ||
Ok(val) => Ok(PathBuf::from(val)), | ||
Err(e) => match e { | ||
VarError::NotPresent => match name { | ||
"XDG_DATA_HOME" => Err(EnvError::DataHomeNotSet), | ||
"HOME" => Err(EnvError::HomeNotSet), | ||
"XDG_RUNTIME_DIR" => Err(EnvError::RuntimeDirNotSet), | ||
_ => Err(EnvError::GenericError(format!("{} not set", name))), | ||
}, | ||
VarError::NotUnicode(_) => { | ||
Err(EnvError::GenericError(format!("{} not unicode", name))) | ||
} | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters