1#![allow(clippy::print_stdout)]
16
17use std::fmt::Display;
18
19shadow_rs::shadow!(build);
20
21#[derive(Clone, Debug, PartialEq)]
22pub struct BuildInfo {
23 pub branch: &'static str,
24 pub commit: &'static str,
25 pub commit_short: &'static str,
26 pub clean: bool,
27 pub source_time: &'static str,
28 pub build_time: &'static str,
29 pub rustc: &'static str,
30 pub target: &'static str,
31 pub version: &'static str,
32}
33
34impl Display for BuildInfo {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 write!(
37 f,
38 "{}",
39 [
40 format!("branch: {}", self.branch),
41 format!("commit: {}", self.commit),
42 format!("commit_short: {}", self.commit_short),
43 format!("clean: {}", self.clean),
44 format!("version: {}", self.version),
45 ]
46 .join("\n")
47 )
48 }
49}
50
51#[derive(Clone, Debug, PartialEq)]
52#[cfg_attr(feature = "codec", derive(serde::Serialize, serde::Deserialize))]
53pub struct OwnedBuildInfo {
54 pub branch: String,
55 pub commit: String,
56 pub commit_short: String,
57 pub clean: bool,
58 pub source_time: String,
59 pub build_time: String,
60 pub rustc: String,
61 pub target: String,
62 pub version: String,
63}
64
65impl From<BuildInfo> for OwnedBuildInfo {
66 fn from(info: BuildInfo) -> Self {
67 OwnedBuildInfo {
68 branch: info.branch.to_string(),
69 commit: info.commit.to_string(),
70 commit_short: info.commit_short.to_string(),
71 clean: info.clean,
72 source_time: info.source_time.to_string(),
73 build_time: info.build_time.to_string(),
74 rustc: info.rustc.to_string(),
75 target: info.target.to_string(),
76 version: info.version.to_string(),
77 }
78 }
79}
80
81impl Display for OwnedBuildInfo {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 write!(
84 f,
85 "{}",
86 [
87 format!("branch: {}", self.branch),
88 format!("commit: {}", self.commit),
89 format!("commit_short: {}", self.commit_short),
90 format!("clean: {}", self.clean),
91 format!("version: {}", self.version),
92 ]
93 .join("\n")
94 )
95 }
96}
97
98pub const fn build_info() -> BuildInfo {
99 BuildInfo {
100 branch: build::BRANCH,
101 commit: build::COMMIT_HASH,
102 commit_short: build::SHORT_COMMIT,
103 clean: build::GIT_CLEAN,
104 source_time: env!("SOURCE_TIMESTAMP"),
105 build_time: env!("BUILD_TIMESTAMP"),
106 rustc: build::RUST_VERSION,
107 target: build::BUILD_TARGET,
108 version: build::PKG_VERSION,
109 }
110}
111
112const BUILD_INFO: BuildInfo = build_info();
113
114pub const fn version() -> &'static str {
115 const_format::formatcp!(
116 "\nbranch: {}\ncommit: {}\nclean: {}\nversion: {}",
117 BUILD_INFO.branch,
118 BUILD_INFO.commit,
119 BUILD_INFO.clean,
120 BUILD_INFO.version,
121 )
122}
123
124pub const fn short_version() -> &'static str {
125 const BRANCH: &str = BUILD_INFO.branch;
126 const COMMIT_ID: &str = BUILD_INFO.commit_short;
127
128 #[allow(clippy::const_is_empty)]
130 if !BRANCH.is_empty() {
131 const_format::formatcp!("{}-{}", BRANCH, COMMIT_ID)
132 } else {
133 COMMIT_ID
134 }
135}