ferretro/ferretro_components/src/provided/stdlib/variables/env.rs

35 lines
1.1 KiB
Rust

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)
}
}