nym_bin_common/build_information/
mod.rs1use serde::{Deserialize, Serialize};
8use std::fmt::{Display, Formatter};
9
10#[derive(Debug)]
11pub struct BinaryBuildInformation {
12 pub binary_name: &'static str,
14
15 pub build_timestamp: &'static str,
18
19 pub build_version: &'static str,
22
23 pub commit_sha: &'static str,
26
27 pub commit_timestamp: &'static str,
30
31 pub commit_branch: &'static str,
34
35 pub rustc_version: &'static str,
38
39 pub rustc_channel: &'static str,
42
43 pub cargo_profile: &'static str,
47
48 pub cargo_triple: &'static str,
51}
52
53impl BinaryBuildInformation {
54 pub const fn new(binary_name: &'static str, build_version: &'static str) -> Self {
56 let cargo_debug = env!("VERGEN_CARGO_DEBUG");
57 let cargo_profile = if const_str::equal!(cargo_debug, "true") {
58 "debug"
59 } else {
60 "release"
61 };
62
63 BinaryBuildInformation {
64 binary_name,
65 build_timestamp: env!("VERGEN_BUILD_TIMESTAMP"),
66 build_version,
67 commit_sha: env!("VERGEN_GIT_SHA"),
68 commit_timestamp: env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
69 commit_branch: env!("VERGEN_GIT_BRANCH"),
70 rustc_version: env!("VERGEN_RUSTC_SEMVER"),
71 rustc_channel: env!("VERGEN_RUSTC_CHANNEL"),
72 cargo_profile,
73 cargo_triple: env!("VERGEN_CARGO_TARGET_TRIPLE"),
74 }
75 }
76
77 pub const fn new_with_local_vergen(
79 binary_name: &'static str,
80 build_timestamp: &'static str,
81 build_version: &'static str,
82 commit_sha: &'static str,
83 commit_timestamp: &'static str,
84 commit_branch: &'static str,
85 ) -> Self {
86 let cargo_debug = env!("VERGEN_CARGO_DEBUG");
87 let cargo_profile = if const_str::equal!(cargo_debug, "true") {
88 "debug"
89 } else {
90 "release"
91 };
92
93 BinaryBuildInformation {
94 binary_name,
95 build_timestamp,
96 build_version,
97 commit_sha,
98 commit_timestamp,
99 commit_branch,
100 rustc_version: env!("VERGEN_RUSTC_SEMVER"),
101 rustc_channel: env!("VERGEN_RUSTC_CHANNEL"),
102 cargo_profile,
103 cargo_triple: env!("VERGEN_CARGO_TARGET_TRIPLE"),
104 }
105 }
106
107 pub fn to_owned(&self) -> BinaryBuildInformationOwned {
108 BinaryBuildInformationOwned {
109 binary_name: self.binary_name.to_owned(),
110 build_timestamp: self.build_timestamp.to_owned(),
111 build_version: self.build_version.to_owned(),
112 commit_sha: self.commit_sha.to_owned(),
113 commit_timestamp: self.commit_timestamp.to_owned(),
114 commit_branch: self.commit_branch.to_owned(),
115 rustc_version: self.rustc_version.to_owned(),
116 rustc_channel: self.rustc_channel.to_owned(),
117 cargo_profile: self.cargo_profile.to_owned(),
118 cargo_triple: self.cargo_triple.to_owned(),
119 }
120 }
121
122 pub fn pretty_print(&self) -> String {
123 self.to_owned().to_string()
124 }
125}
126
127#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
128#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
129#[cfg_attr(feature = "bin_info_schema", derive(schemars::JsonSchema))]
130pub struct BinaryBuildInformationOwned {
131 pub binary_name: String,
133
134 pub build_timestamp: String,
137
138 pub build_version: String,
141
142 pub commit_sha: String,
145
146 pub commit_timestamp: String,
149
150 pub commit_branch: String,
153
154 pub rustc_version: String,
157
158 pub rustc_channel: String,
161
162 pub cargo_profile: String,
166
167 #[serde(default = "unknown")]
170 pub cargo_triple: String,
171}
172
173fn unknown() -> String {
174 "unknown".to_string()
175}
176
177impl Display for BinaryBuildInformationOwned {
178 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
179 write!(
180 f,
181 r#"
182{:<20}{}
183{:<20}{}
184{:<20}{}
185{:<20}{}
186{:<20}{}
187{:<20}{}
188{:<20}{}
189{:<20}{}
190{:<20}{}
191"#,
192 "Binary Name:",
193 self.binary_name,
194 "Build Timestamp:",
195 self.build_timestamp,
196 "Build Version:",
197 self.build_version,
198 "Commit SHA:",
199 self.commit_sha,
200 "Commit Date:",
201 self.commit_timestamp,
202 "Commit Branch:",
203 self.commit_branch,
204 "rustc Version:",
205 self.rustc_version,
206 "rustc Channel:",
207 self.rustc_channel,
208 "cargo Profile:",
209 self.cargo_profile,
210 )
211 }
212}
213
214#[macro_export]
216macro_rules! bin_info {
217 () => {
218 $crate::build_information::BinaryBuildInformation::new(
219 env!("CARGO_PKG_NAME"),
220 env!("CARGO_PKG_VERSION"),
221 )
222 };
223}
224
225#[macro_export]
226macro_rules! bin_info_owned {
227 () => {
228 $crate::build_information::BinaryBuildInformation::new(
229 env!("CARGO_PKG_NAME"),
230 env!("CARGO_PKG_VERSION"),
231 )
232 .to_owned()
233 };
234}
235
236#[macro_export]
239macro_rules! bin_info_local_vergen {
240 () => {
241 $crate::build_information::BinaryBuildInformation::new_with_local_vergen(
242 env!("CARGO_PKG_NAME"),
243 env!("VERGEN_BUILD_TIMESTAMP"),
244 env!("CARGO_PKG_VERSION"),
245 env!("VERGEN_GIT_SHA"),
246 env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
247 env!("VERGEN_GIT_BRANCH"),
248 )
249 };
250}
251
252#[macro_export]
253macro_rules! bin_info_local_vergen_owned {
254 () => {
255 $crate::build_information::BinaryBuildInformation::new_with_local_vergen(
256 env!("CARGO_PKG_NAME"),
257 env!("VERGEN_BUILD_TIMESTAMP"),
258 env!("CARGO_PKG_VERSION"),
259 env!("VERGEN_GIT_SHA"),
260 env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
261 env!("VERGEN_GIT_BRANCH"),
262 )
263 };
264}