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)]
132#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
133#[cfg_attr(feature = "bin_info_schema", derive(schemars::JsonSchema))]
134pub struct BinaryBuildInformationOwned {
135 pub binary_name: String,
137
138 pub build_timestamp: String,
141
142 pub build_version: String,
145
146 pub commit_sha: String,
149
150 pub commit_timestamp: String,
153
154 pub commit_branch: String,
157
158 pub rustc_version: String,
161
162 pub rustc_channel: String,
165
166 pub cargo_profile: String,
170
171 #[serde(default = "unknown")]
174 pub cargo_triple: String,
175}
176
177fn unknown() -> String {
178 "unknown".to_string()
179}
180
181impl Display for BinaryBuildInformationOwned {
182 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
183 write!(
184 f,
185 r#"
186{:<20}{}
187{:<20}{}
188{:<20}{}
189{:<20}{}
190{:<20}{}
191{:<20}{}
192{:<20}{}
193{:<20}{}
194{:<20}{}
195"#,
196 "Binary Name:",
197 self.binary_name,
198 "Build Timestamp:",
199 self.build_timestamp,
200 "Build Version:",
201 self.build_version,
202 "Commit SHA:",
203 self.commit_sha,
204 "Commit Date:",
205 self.commit_timestamp,
206 "Commit Branch:",
207 self.commit_branch,
208 "rustc Version:",
209 self.rustc_version,
210 "rustc Channel:",
211 self.rustc_channel,
212 "cargo Profile:",
213 self.cargo_profile,
214 )
215 }
216}
217
218#[macro_export]
220macro_rules! bin_info {
221 () => {
222 $crate::build_information::BinaryBuildInformation::new(
223 env!("CARGO_PKG_NAME"),
224 env!("CARGO_PKG_VERSION"),
225 )
226 };
227}
228
229#[macro_export]
230macro_rules! bin_info_owned {
231 () => {
232 $crate::build_information::BinaryBuildInformation::new(
233 env!("CARGO_PKG_NAME"),
234 env!("CARGO_PKG_VERSION"),
235 )
236 .to_owned()
237 };
238}
239
240#[macro_export]
243macro_rules! bin_info_local_vergen {
244 () => {
245 $crate::build_information::BinaryBuildInformation::new_with_local_vergen(
246 env!("CARGO_PKG_NAME"),
247 env!("VERGEN_BUILD_TIMESTAMP"),
248 env!("CARGO_PKG_VERSION"),
249 env!("VERGEN_GIT_SHA"),
250 env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
251 env!("VERGEN_GIT_BRANCH"),
252 )
253 };
254}
255
256#[macro_export]
257macro_rules! bin_info_local_vergen_owned {
258 () => {
259 $crate::build_information::BinaryBuildInformation::new_with_local_vergen(
260 env!("CARGO_PKG_NAME"),
261 env!("VERGEN_BUILD_TIMESTAMP"),
262 env!("CARGO_PKG_VERSION"),
263 env!("VERGEN_GIT_SHA"),
264 env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
265 env!("VERGEN_GIT_BRANCH"),
266 )
267 };
268}