diff --git a/src/main.rs b/src/main.rs index 568876e..1c9ff4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod error; mod exit_code; mod interface; mod parser; +mod variables; fn main() { control::run(); diff --git a/src/variables.rs b/src/variables.rs new file mode 100644 index 0000000..88277b1 --- /dev/null +++ b/src/variables.rs @@ -0,0 +1,50 @@ +use std::collections::HashMap; +use std::env; + +struct Variables { + variables: HashMap, +} + +impl Variables { + fn get(&self, key: &str) -> String { + let var_from_map = self.variables.get(key); + + let value: String; + if var_from_map.is_none() { + let var_in_env = env::var(key); + + value = match var_in_env { + Ok(value) => value, + Err(_e) => String::new(), + } + } else { + value = var_from_map.unwrap().clone(); + } + + value + } + + fn unset(&mut self, key: &str) { + let old_value = self.variables.remove(key); + + if old_value.is_none() { + env::remove_var(key); + } + } + + fn set(&mut self, key: &str, value: &str) { + self.variables + .insert(String::from(key), String::from(value)); + } + + fn export(&mut self, key: &str) { + let var_to_export = self.variables.get(key); + + if var_to_export.is_some() { + env::set_var(key, var_to_export.unwrap()); + self.variables.remove(key); + } else { + env::set_var(key, ""); + } + } +}