use crate::{constants::VERGEN_IDEMPOTENT_DEFAULT, CargoRustcEnvMap, CargoWarning, VergenKey};
use std::env;
#[cfg_attr(
feature = "build",
doc = r##" add_default_map_entry(VergenKey::BuildDate, &mut map, &mut warning);
assert_eq!(1, map.len());
assert_eq!(1, warning.len());"##
)]
pub fn add_default_map_entry(
key: VergenKey,
map: &mut CargoRustcEnvMap,
warnings: &mut CargoWarning,
) {
if let Ok(value) = env::var(key.name()) {
add_map_entry(key, value, map);
warnings.push(format!("{} overidden", key.name()));
} else {
add_map_entry(key, VERGEN_IDEMPOTENT_DEFAULT, map);
warnings.push(format!("{} set to default", key.name()));
}
}
#[cfg_attr(
feature = "build",
doc = r##"add_map_entry(VergenKey::BuildDate, "test", &mut map);
assert_eq!(1, map.len());"##
)]
pub fn add_map_entry<T>(key: VergenKey, value: T, map: &mut CargoRustcEnvMap)
where
T: Into<String>,
{
let _old = map.insert(key, value.into());
}
#[cfg_attr(
feature = "build",
doc = r##"_ = map.insert(VergenKey::BuildDate, VERGEN_IDEMPOTENT_DEFAULT.to_string());"##
)]
#[cfg_attr(feature = "build", doc = r##"assert_eq!(1, count_idempotent(&map));"##)]
#[must_use]
pub fn count_idempotent(map: &CargoRustcEnvMap) -> usize {
map.values()
.filter(|x| *x == VERGEN_IDEMPOTENT_DEFAULT)
.count()
}