use crate::{CargoRustcEnvMap, CargoWarning, VergenKey, constants::VERGEN_IDEMPOTENT_DEFAULT};
use std::env;
#[cfg_attr(
feature = "build",
doc = r" add_default_map_entry(false, VergenKey::BuildDate, &mut map, &mut warning);
assert_eq!(1, map.len());
assert_eq!(1, warning.len());"
)]
pub fn add_default_map_entry(
idempotent: bool,
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 if idempotent {
add_map_entry(key, VERGEN_IDEMPOTENT_DEFAULT, map);
warnings.push(format!("{} set to default", key.name()));
} else {
warnings.push(format!("Unable to set {}", 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()
}
#[cfg(feature = "vcs_info")]
#[must_use]
pub fn vcs_info() -> Option<(String, bool)> {
let manifest_dir = env::var_os("CARGO_MANIFEST_DIR")?;
let path = std::path::Path::new(&manifest_dir).join(".cargo_vcs_info.json");
let contents = std::fs::read_to_string(path).ok()?;
let json: serde_json::Value = serde_json::from_str(&contents).ok()?;
let git = json.get("git")?;
let sha = git.get("sha1")?.as_str()?.to_string();
let dirty = git
.get("dirty")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false);
Some((sha, dirty))
}