38 lines
1.2 KiB
Rust
38 lines
1.2 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,
|
|
v.options.iter().map(String::as_str).collect::<Vec<_>>().join(", ")
|
|
);
|
|
if let Ok(value) = std::env::var(&v.key) {
|
|
if !v.options.contains(&value) {
|
|
panic!("Invalid value {:?} for variable {:?} (expected one of {:?})", value, v.key, v.options);
|
|
} else if value != v.options[0] {
|
|
println!("# (overridden, default {})", v.options[0]);
|
|
}
|
|
println!("{}={}", v.key, value);
|
|
self.variables.insert(v.key.to_owned(), value);
|
|
} else {
|
|
println!("{}=", v.key);
|
|
}
|
|
}
|
|
Some(true)
|
|
}
|
|
}
|