#[derive(Copy, Clone, Debug)]
pub struct BuildInfo {
pub crate_name: &'static str,
pub features: &'static str,
pub version: super::CrateVersion,
pub rustc_version: &'static str,
pub llvm_version: &'static str,
pub git_hash: &'static str,
pub git_branch: &'static str,
pub is_in_rerun_workspace: bool,
pub target_triple: &'static str,
pub datetime: &'static str,
}
impl BuildInfo {
pub fn git_hash_or_tag(&self) -> String {
if self.git_hash.is_empty() {
format!("v{}", self.version)
} else {
self.git_hash.to_owned()
}
}
pub fn short_git_hash(&self) -> &str {
if self.git_hash.is_empty() {
""
} else {
&self.git_hash[..7]
}
}
pub fn is_final(&self) -> bool {
self.version.meta.is_none()
}
}
impl std::fmt::Display for BuildInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
crate_name,
features,
version,
rustc_version,
llvm_version,
git_hash,
git_branch,
is_in_rerun_workspace: _,
target_triple,
datetime,
} = self;
let rustc_version = (!rustc_version.is_empty()).then(|| format!("rustc {rustc_version}"));
let llvm_version = (!llvm_version.is_empty()).then(|| format!("LLVM {llvm_version}"));
write!(f, "{crate_name} {version}")?;
if !features.is_empty() {
write!(f, " ({features})")?;
}
if let Some(rustc_version) = rustc_version {
write!(f, " [{rustc_version}")?;
if let Some(llvm_version) = llvm_version {
write!(f, ", {llvm_version}")?;
}
write!(f, "]")?;
}
if !target_triple.is_empty() {
write!(f, " {target_triple}")?;
}
if !git_branch.is_empty() {
write!(f, " {git_branch}")?;
}
if !git_hash.is_empty() {
let git_hash: String = git_hash.chars().take(7).collect(); write!(f, " {git_hash}")?;
}
if !datetime.is_empty() {
write!(f, ", built {datetime}")?;
}
if cfg!(debug_assertions) {
write!(f, " (debug)")?;
}
Ok(())
}
}
use crate::CrateVersion;
impl CrateVersion {
pub fn try_parse_from_build_info_string(s: impl AsRef<str>) -> Result<Self, String> {
let s = Box::leak(s.as_ref().to_owned().into_boxed_str());
let parts = s.split_whitespace().collect::<Vec<_>>();
if parts.len() < 2 {
return Err(format!("{s:?} is not a valid BuildInfo string"));
}
Self::try_parse(parts[1]).map_err(ToOwned::to_owned)
}
}
#[test]
fn crate_version_from_build_info_string() {
let build_info = BuildInfo {
crate_name: "re_build_info",
features: "default extra",
version: CrateVersion {
major: 0,
minor: 10,
patch: 0,
meta: Some(crate::crate_version::Meta::DevAlpha {
alpha: 7,
commit: None,
}),
},
rustc_version: "1.76.0 (d5c2e9c34 2023-09-13)",
llvm_version: "16.0.5",
git_hash: "",
git_branch: "",
is_in_rerun_workspace: true,
target_triple: "x86_64-unknown-linux-gnu",
datetime: "",
};
let build_info_str = build_info.to_string();
{
let expected_crate_version = build_info.version;
let crate_version = CrateVersion::try_parse_from_build_info_string(&build_info_str);
assert_eq!(
crate_version,
Ok(expected_crate_version),
"Failed to parse {build_info_str:?}"
);
}
}