#[derive(Debug, strum::Display, Clone, Copy)]
pub enum Env {
#[strum(serialize = "CNF_ALIAS_PRIVILEGED")]
AliasPrivileged,
#[strum(serialize = "CNF_ALIAS_EXECUTABLE")]
AliasExecutable,
#[strum(serialize = "CNF_RECURSION_DEPTH")]
RecursionDepth,
#[strum(serialize = "CNF_LOG_LEVEL")]
LogLevel,
#[strum(serialize = "RUST_LOG")]
RustLog,
}
impl Env {
pub fn is_set(&self) -> bool {
self.get_raw().is_some()
}
pub fn get_raw(&self) -> Option<std::ffi::OsString> {
std::env::var_os(self.to_string())
}
#[allow(unsafe_code)]
pub unsafe fn set_raw(&self, value: impl AsRef<std::ffi::OsStr>) {
unsafe { std::env::set_var(self.to_string(), value) }
}
pub fn get<T: std::str::FromStr>(&self) -> Option<T> {
self.get_raw()
.and_then(|s| s.into_string().ok())
.and_then(|s| T::from_str(s.as_ref()).ok())
}
#[allow(unsafe_code)]
pub unsafe fn set<T: std::fmt::Display>(&self, value: T) {
unsafe { self.set_raw(value.to_string()) }
}
}