thru_base/version.rs
1//! Version string utilities for thru binaries.
2//!
3//! Provides a macro to generate version strings with git info.
4//! The macro must be used because env vars are resolved per-crate at compile time.
5
6/// Generate a version string with git info.
7///
8/// Returns format: "0.1.0+abc1234" or "0.1.0+abc1234-dirty"
9///
10/// # Example
11/// ```ignore
12/// use thru_base::get_version;
13/// let version: &'static str = get_version!();
14/// ```
15#[macro_export]
16macro_rules! get_version {
17 () => {{
18 use std::sync::OnceLock;
19 static VERSION: OnceLock<&'static str> = OnceLock::new();
20
21 *VERSION.get_or_init(|| {
22 let base = env!("CARGO_PKG_VERSION");
23 let sha = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
24 let sha_short = if sha.len() > 7 { &sha[..7] } else { sha };
25 let dirty = option_env!("VERGEN_GIT_DIRTY")
26 .map(|d| if d == "true" { "-dirty" } else { "" })
27 .unwrap_or("");
28 let version = format!("{}+{}{}", base, sha_short, dirty);
29 Box::leak(version.into_boxed_str())
30 })
31 }};
32}