use std::collections::HashMap;
use crate::{Key, Property, PropertySource, SubKeys};
#[derive(Debug)]
pub struct HashMapSource {
name: String,
map: HashMap<String, String>,
}
impl HashMapSource {
pub fn new(name: &'static str) -> Self {
Self {
name: name.to_owned(),
map: HashMap::new(),
}
}
pub fn set<K: Into<String>, V: Into<String>>(mut self, key: K, val: V) -> Self {
self.map.insert(key.into(), val.into());
self
}
pub fn set_all(mut self, map: HashMap<String, String>) -> Self {
self.map.extend(map);
self
}
}
impl PropertySource for HashMapSource {
#[inline]
fn name(&self) -> &str {
&self.name
}
#[inline]
fn get_property(&self, key: &Key<'_>) -> Option<Property<'_>> {
self.map.get(key.as_str()).map(|s| Property::S(s))
}
fn get_sub_keys<'a>(&'a self, prefix: &Key<'_>, sub_keys: &mut SubKeys<'a>) {
for key in self.map.keys() {
if let Some(k) = key.strip_prefix(prefix.as_str()) {
let pos = k.find('.').unwrap_or_else(|| k.len());
sub_keys.insert(&k[0..pos]);
}
}
}
fn is_empty(&self) -> bool {
self.map.is_empty()
}
}
pub fn system_environment() -> HashMapSource {
HashMapSource {
name: "SystemEnvironment".to_owned(),
map: std::env::vars().collect(),
}
}