pub const BUILD_PROVENANCE_BLAKE3: [u8; 32] = decode_hex(env!("GGEN_BUILD_PROVENANCE_HEX"));
pub const BUILD_PROVENANCE_HEX: &str = env!("GGEN_BUILD_PROVENANCE_HEX");
pub const BUILD_COMMIT_SHA: &str = env!("GGEN_BUILD_COMMIT_SHA");
pub const BUILD_SOURCE: &str = env!("GGEN_BUILD_SOURCE");
pub const BUILD_STRATA_PACKS: &str = env!("GGEN_BUILD_STRATA_PACKS");
pub const BUILD_ONTOLOGY_TRIPLES: u32 = parse_u32(env!("GGEN_BUILD_ONTOLOGY_TRIPLES"));
const fn parse_u32(s: &str) -> u32 {
let bytes = s.as_bytes();
let mut n: u32 = 0;
let mut i = 0;
while i < bytes.len() {
let c = bytes[i];
if !c.is_ascii_digit() {
return 0;
}
n = n * 10 + (c - b'0') as u32;
i += 1;
}
n
}
#[must_use]
pub fn describe() -> String {
format!(
"ggen {}\nsource: {}\ncommit: {}\nstrata_packs: {}\nontology_triples: {}\nbuild_provenance_blake3: {}",
env!("CARGO_PKG_VERSION"),
BUILD_SOURCE,
BUILD_COMMIT_SHA,
BUILD_STRATA_PACKS,
BUILD_ONTOLOGY_TRIPLES,
BUILD_PROVENANCE_HEX,
)
}
const fn hex_val(c: u8) -> u8 {
match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 10,
_ => 0,
}
}
const fn decode_hex(s: &str) -> [u8; 32] {
let bytes = s.as_bytes();
let mut out = [0u8; 32];
let mut i = 0;
while i < 32 {
out[i] = hex_val(bytes[2 * i]) * 16 + hex_val(bytes[2 * i + 1]);
i += 1;
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn const_round_trips_through_hex() {
const HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";
let mut hex = String::with_capacity(64);
for b in BUILD_PROVENANCE_BLAKE3 {
hex.push(HEX_DIGITS[(b >> 4) as usize] as char);
hex.push(HEX_DIGITS[(b & 0x0f) as usize] as char);
}
assert_eq!(hex, BUILD_PROVENANCE_HEX);
}
}