first pass at env variable component

This commit is contained in:
lifning 2021-12-10 18:57:25 -08:00
parent 9de6008ef5
commit 2259265107
3 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,34 @@
use std::collections::BTreeMap;
use crate::prelude::*;
#[derive(Default)]
pub struct EnvironmentVariableComponent {
variables: BTreeMap<String, String>,
}
impl RetroComponent for EnvironmentVariableComponent {}
impl RetroCallbacks for EnvironmentVariableComponent {
fn get_variable(&mut self, key: &str) -> Option<String> {
self.variables.get(key).cloned()
}
fn set_variables(&mut self, variables: &[Variable2]) -> Option<bool> {
for v in variables {
println!("# {}", v.description);
let value = if let Ok(x) = std::env::var(&v.key) {
if !v.options.contains(&x) {
panic!("Invalid value {:?} for variable {:?} (expected one of {:?})", x, v.key, v.options);
} else if x != v.options[0] {
println!("# (overridden, default {})", v.options[0]);
}
x
} else {
v.options[0].clone()
};
println!("{}={} # {:?}", v.key, value, v.options);
self.variables.insert(v.key.to_owned(), value);
}
Some(true)
}
}

View File

@ -0,0 +1,5 @@
mod env;
mod store;
pub use env::EnvironmentVariableComponent;
pub use store::VariableStoreComponent;