robonix_cli/
build_info.rs1use serde::Serialize;
6
7use crate::output::short_sha;
8
9#[derive(Serialize)]
10pub struct BuildInfo {
11 pub version: &'static str,
12 pub git_sha: Option<&'static str>,
13 pub git_describe: Option<&'static str>,
14 pub git_commit_date: Option<&'static str>,
15 pub git_dirty: Option<bool>,
16 pub build_date: Option<&'static str>,
17 pub rustc: Option<&'static str>,
18 pub target: Option<&'static str>,
19}
20
21fn known(value: &'static str) -> Option<&'static str> {
23 (value != "VERGEN_IDEMPOTENT_OUTPUT").then_some(value)
24}
25
26impl BuildInfo {
27 pub fn get() -> Self {
28 Self {
29 version: env!("CARGO_PKG_VERSION"),
30 git_sha: known(env!("VERGEN_GIT_SHA")),
31 git_describe: known(env!("VERGEN_GIT_DESCRIBE")),
32 git_commit_date: known(env!("VERGEN_GIT_COMMIT_DATE")),
33 git_dirty: known(env!("VERGEN_GIT_DIRTY")).map(|d| d == "true"),
34 build_date: known(env!("VERGEN_BUILD_DATE")),
35 rustc: known(env!("VERGEN_RUSTC_SEMVER")),
36 target: known(env!("VERGEN_CARGO_TARGET_TRIPLE")),
37 }
38 }
39
40 pub fn summary(&self) -> String {
42 let or_unknown = |v: Option<&'static str>| v.unwrap_or("unknown");
43 let dirty = if self.git_dirty == Some(true) {
44 ", dirty"
45 } else {
46 ""
47 };
48 format!(
49 "{} ({} {}{dirty}, built {}, rustc {}, {})",
50 self.version,
51 or_unknown(self.git_sha.map(short_sha)),
52 or_unknown(self.git_commit_date),
53 or_unknown(self.build_date),
54 or_unknown(self.rustc),
55 or_unknown(self.target),
56 )
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn summary_shows_known_and_unknown_facts() {
66 let mut info = BuildInfo {
67 version: "0.1.0",
68 git_sha: None,
69 git_describe: None,
70 git_commit_date: None,
71 git_dirty: None,
72 build_date: Some("2026-09-30"),
73 rustc: Some("1.95.0"),
74 target: Some("x86_64-unknown-linux-gnu"),
75 };
76 assert_eq!(
77 info.summary(),
78 "0.1.0 (unknown unknown, built 2026-09-30, rustc 1.95.0, x86_64-unknown-linux-gnu)"
79 );
80 info.git_sha = Some("d94598851f2a0c3e");
81 info.git_commit_date = Some("2026-09-28");
82 info.git_dirty = Some(true);
83 assert_eq!(
84 info.summary(),
85 "0.1.0 (d9459885 2026-09-28, dirty, built 2026-09-30, rustc 1.95.0, x86_64-unknown-linux-gnu)"
86 );
87 }
88}