1use std::collections::HashMap;
2use std::env;
3use std::mem;
4
5pub struct EnvVars {
6 #[allow(dead_code)]
7 storage: Vec<(String, String)>,
8 vars: HashMap<&'static str, &'static str>,
9}
10
11impl EnvVars {
12 pub fn build() -> Self {
13 let storage = env::vars()
14 .filter(|(_, value)| !value.trim().is_empty())
15 .map(|(key, value)| (key.to_uppercase(), value))
16 .collect::<Vec<_>>();
17 let vars = unsafe { Self::build_vars(&storage) };
18 Self { storage, vars }
19 }
20
21 #[inline(always)]
22 pub fn get_var(&self, name: &str) -> Option<&str> {
23 let val = self.vars.get(&name)?;
24 Some(*val)
25 }
26
27 unsafe fn build_vars(raw: &[(String, String)]) -> HashMap<&'static str, &'static str> {
28 raw.iter()
29 .map(|(key, val)| {
30 let key_ref: &str = key;
31 let key_ptr: &'static str = mem::transmute(key_ref);
32 let val_ref: &str = val;
33 let val_ptr: &'static str = mem::transmute(val_ref);
34 (key_ptr, val_ptr)
35 })
36 .collect()
37 }
38}