pub fn get_env_with_prefix(key: &str) -> Option<String> {
std::env::var(format!("TIDEWAY_{}", key))
.or_else(|_| std::env::var(key))
.ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_env_with_prefix() {
unsafe {
std::env::set_var("TIDEWAY_TEST_VAR", "prefixed_value");
}
assert_eq!(
get_env_with_prefix("TEST_VAR"),
Some("prefixed_value".to_string())
);
unsafe {
std::env::remove_var("TIDEWAY_TEST_VAR");
}
unsafe {
std::env::set_var("FALLBACK_VAR", "unprefixed_value");
}
assert_eq!(
get_env_with_prefix("FALLBACK_VAR"),
Some("unprefixed_value".to_string())
);
unsafe {
std::env::remove_var("FALLBACK_VAR");
}
assert_eq!(get_env_with_prefix("NON_EXISTENT_VAR"), None);
}
}