1#[cfg(any(
11 feature = "build",
12 feature = "cargo",
13 feature = "git",
14 feature = "rustc",
15 feature = "si"
16))]
17pub(crate) mod vergen_key {
18 #[cfg(feature = "build")]
19 use crate::constants::{BUILD_DATE_NAME, BUILD_TIMESTAMP_NAME, BUILD_TIMESTAMP_UNIX_NAME};
20 #[cfg(feature = "cargo")]
21 use crate::constants::{
22 CARGO_DEBUG, CARGO_DEPENDENCIES, CARGO_FEATURES, CARGO_OPT_LEVEL, CARGO_TARGET_TRIPLE,
23 };
24 #[cfg(feature = "git")]
25 use crate::constants::{
26 GIT_BRANCH_NAME, GIT_COMMIT_AUTHOR_EMAIL, GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_COUNT,
27 GIT_COMMIT_DATE_NAME, GIT_COMMIT_MESSAGE, GIT_COMMIT_TIMESTAMP_NAME,
28 GIT_COMMIT_TIMESTAMP_UNIX_NAME, GIT_DESCRIBE_NAME, GIT_DIRTY_NAME, GIT_SHA_NAME,
29 };
30 #[cfg(feature = "rustc")]
31 use crate::constants::{
32 RUSTC_CHANNEL_NAME, RUSTC_COMMIT_DATE, RUSTC_COMMIT_HASH, RUSTC_HOST_TRIPLE_NAME,
33 RUSTC_LLVM_VERSION, RUSTC_SEMVER_NAME,
34 };
35 #[cfg(feature = "si")]
36 use crate::constants::{
37 SYSINFO_CPU_BRAND, SYSINFO_CPU_CORE_COUNT, SYSINFO_CPU_FREQUENCY, SYSINFO_CPU_NAME,
38 SYSINFO_CPU_VENDOR, SYSINFO_MEMORY, SYSINFO_NAME, SYSINFO_OS_VERSION, SYSINFO_USER,
39 };
40
41 #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
43 pub enum VergenKey {
44 #[cfg(feature = "build")]
46 BuildDate,
47 #[cfg(feature = "build")]
49 BuildTimestamp,
50 #[cfg(feature = "build")]
52 BuildTimestampUnix,
53 #[cfg(feature = "cargo")]
55 CargoDebug,
56 #[cfg(feature = "cargo")]
58 CargoFeatures,
59 #[cfg(feature = "cargo")]
61 CargoOptLevel,
62 #[cfg(feature = "cargo")]
64 CargoTargetTriple,
65 #[cfg(feature = "cargo")]
67 CargoDependencies,
68 #[cfg(feature = "git")]
70 GitBranch,
71 #[cfg(feature = "git")]
73 GitCommitAuthorEmail,
74 #[cfg(feature = "git")]
76 GitCommitAuthorName,
77 #[cfg(feature = "git")]
79 GitCommitCount,
80 #[cfg(feature = "git")]
82 GitCommitDate,
83 #[cfg(feature = "git")]
85 GitCommitMessage,
86 #[cfg(feature = "git")]
88 GitCommitTimestamp,
89 #[cfg(feature = "git")]
91 GitCommitTimestampUnix,
92 #[cfg(feature = "git")]
94 GitDescribe,
95 #[cfg(feature = "git")]
97 GitSha,
98 #[cfg(feature = "git")]
100 GitDirty,
101 #[cfg(feature = "rustc")]
103 RustcChannel,
104 #[cfg(feature = "rustc")]
106 RustcCommitDate,
107 #[cfg(feature = "rustc")]
109 RustcCommitHash,
110 #[cfg(feature = "rustc")]
112 RustcHostTriple,
113 #[cfg(feature = "rustc")]
115 RustcLlvmVersion,
116 #[cfg(feature = "rustc")]
118 RustcSemver,
119 #[cfg(feature = "si")]
121 SysinfoName,
122 #[cfg(feature = "si")]
124 SysinfoOsVersion,
125 #[cfg(feature = "si")]
127 SysinfoUser,
128 #[cfg(feature = "si")]
130 SysinfoMemory,
131 #[cfg(feature = "si")]
133 SysinfoCpuVendor,
134 #[cfg(feature = "si")]
136 SysinfoCpuCoreCount,
137 #[cfg(feature = "si")]
139 SysinfoCpuName,
140 #[cfg(feature = "si")]
142 SysinfoCpuBrand,
143 #[cfg(feature = "si")]
145 SysinfoCpuFrequency,
146 }
147
148 impl VergenKey {
149 #[must_use]
151 pub fn name(self) -> &'static str {
152 match self {
153 #[cfg(feature = "build")]
154 VergenKey::BuildDate => BUILD_DATE_NAME,
155 #[cfg(feature = "build")]
156 VergenKey::BuildTimestamp => BUILD_TIMESTAMP_NAME,
157 #[cfg(feature = "build")]
158 VergenKey::BuildTimestampUnix => BUILD_TIMESTAMP_UNIX_NAME,
159 #[cfg(feature = "cargo")]
160 VergenKey::CargoDebug => CARGO_DEBUG,
161 #[cfg(feature = "cargo")]
162 VergenKey::CargoFeatures => CARGO_FEATURES,
163 #[cfg(feature = "cargo")]
164 VergenKey::CargoOptLevel => CARGO_OPT_LEVEL,
165 #[cfg(feature = "cargo")]
166 VergenKey::CargoTargetTriple => CARGO_TARGET_TRIPLE,
167 #[cfg(feature = "cargo")]
168 VergenKey::CargoDependencies => CARGO_DEPENDENCIES,
169 #[cfg(feature = "git")]
170 VergenKey::GitBranch => GIT_BRANCH_NAME,
171 #[cfg(feature = "git")]
172 VergenKey::GitCommitAuthorEmail => GIT_COMMIT_AUTHOR_EMAIL,
173 #[cfg(feature = "git")]
174 VergenKey::GitCommitAuthorName => GIT_COMMIT_AUTHOR_NAME,
175 #[cfg(feature = "git")]
176 VergenKey::GitCommitCount => GIT_COMMIT_COUNT,
177 #[cfg(feature = "git")]
178 VergenKey::GitCommitDate => GIT_COMMIT_DATE_NAME,
179 #[cfg(feature = "git")]
180 VergenKey::GitCommitMessage => GIT_COMMIT_MESSAGE,
181 #[cfg(feature = "git")]
182 VergenKey::GitCommitTimestamp => GIT_COMMIT_TIMESTAMP_NAME,
183 #[cfg(feature = "git")]
184 VergenKey::GitCommitTimestampUnix => GIT_COMMIT_TIMESTAMP_UNIX_NAME,
185 #[cfg(feature = "git")]
186 VergenKey::GitDescribe => GIT_DESCRIBE_NAME,
187 #[cfg(feature = "git")]
188 VergenKey::GitSha => GIT_SHA_NAME,
189 #[cfg(feature = "git")]
190 VergenKey::GitDirty => GIT_DIRTY_NAME,
191 #[cfg(feature = "rustc")]
192 VergenKey::RustcChannel => RUSTC_CHANNEL_NAME,
193 #[cfg(feature = "rustc")]
194 VergenKey::RustcCommitDate => RUSTC_COMMIT_DATE,
195 #[cfg(feature = "rustc")]
196 VergenKey::RustcCommitHash => RUSTC_COMMIT_HASH,
197 #[cfg(feature = "rustc")]
198 VergenKey::RustcHostTriple => RUSTC_HOST_TRIPLE_NAME,
199 #[cfg(feature = "rustc")]
200 VergenKey::RustcLlvmVersion => RUSTC_LLVM_VERSION,
201 #[cfg(feature = "rustc")]
202 VergenKey::RustcSemver => RUSTC_SEMVER_NAME,
203 #[cfg(feature = "si")]
204 VergenKey::SysinfoName => SYSINFO_NAME,
205 #[cfg(feature = "si")]
206 VergenKey::SysinfoOsVersion => SYSINFO_OS_VERSION,
207 #[cfg(feature = "si")]
208 VergenKey::SysinfoUser => SYSINFO_USER,
209 #[cfg(feature = "si")]
210 VergenKey::SysinfoMemory => SYSINFO_MEMORY,
211 #[cfg(feature = "si")]
212 VergenKey::SysinfoCpuVendor => SYSINFO_CPU_VENDOR,
213 #[cfg(feature = "si")]
214 VergenKey::SysinfoCpuCoreCount => SYSINFO_CPU_CORE_COUNT,
215 #[cfg(feature = "si")]
216 VergenKey::SysinfoCpuName => SYSINFO_CPU_NAME,
217 #[cfg(feature = "si")]
218 VergenKey::SysinfoCpuBrand => SYSINFO_CPU_BRAND,
219 #[cfg(feature = "si")]
220 VergenKey::SysinfoCpuFrequency => SYSINFO_CPU_FREQUENCY,
221 }
222 }
223 }
224}
225
226#[cfg(not(any(
228 feature = "build",
229 feature = "cargo",
230 feature = "git",
231 feature = "rustc",
232 feature = "si"
233)))]
234pub(crate) mod vergen_key {
235 #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
237 pub enum VergenKey {
238 Empty,
240 }
241
242 impl VergenKey {
243 #[must_use]
245 pub fn name(self) -> &'static str {
246 match self {
247 VergenKey::Empty => "",
248 }
249 }
250 }
251}
252
253#[cfg(all(
254 test,
255 not(any(
256 feature = "build",
257 feature = "cargo",
258 feature = "git",
259 feature = "rustc",
260 feature = "si"
261 ))
262))]
263mod test {
264 use super::vergen_key::VergenKey;
265 use anyhow::Result;
266 use std::{
267 cmp::Ordering,
268 collections::hash_map::DefaultHasher,
269 hash::{Hash, Hasher},
270 io::Write,
271 };
272
273 #[test]
274 fn empty_name() {
275 assert!(VergenKey::Empty.name().is_empty());
276 }
277
278 #[test]
279 #[allow(clippy::clone_on_copy, clippy::redundant_clone)]
280 fn vergen_key_clone_works() {
281 let key = VergenKey::Empty;
282 let another = key.clone();
283 assert_eq!(another, key);
284 }
285
286 #[test]
287 fn vergen_key_debug_works() -> Result<()> {
288 let key = VergenKey::Empty;
289 let mut buf = vec![];
290 write!(buf, "{key:?}")?;
291 assert!(!buf.is_empty());
292 Ok(())
293 }
294
295 #[test]
296 fn vergen_key_ord_works() {
297 assert_eq!(VergenKey::Empty.cmp(&VergenKey::Empty), Ordering::Equal);
298 }
299
300 #[test]
301 fn vergen_key_partial_ord_works() {
302 assert_eq!(
303 VergenKey::Empty.partial_cmp(&VergenKey::Empty),
304 Some(Ordering::Equal)
305 );
306 }
307
308 #[test]
309 fn vergen_key_hash_works() {
310 let mut first = DefaultHasher::new();
314 VergenKey::Empty.hash(&mut first);
315 let mut second = DefaultHasher::new();
316 VergenKey::Empty.hash(&mut second);
317 assert_eq!(first.finish(), second.finish());
318 }
319}
320
321#[cfg(all(test, all(feature = "build", feature = "cargo")))]
322mod test {
323 use super::vergen_key::VergenKey;
324 use anyhow::Result;
325 use std::{
326 cmp::Ordering,
327 collections::hash_map::DefaultHasher,
328 hash::{Hash, Hasher},
329 io::Write,
330 };
331
332 #[test]
333 #[allow(clippy::clone_on_copy, clippy::redundant_clone)]
334 fn vergen_key_clone_works() {
335 let key = VergenKey::BuildDate;
336 let another = key.clone();
337 assert_eq!(another, key);
338 }
339
340 #[test]
341 fn vergen_key_debug_works() -> Result<()> {
342 let key = VergenKey::BuildDate;
343 let mut buf = vec![];
344 write!(buf, "{key:?}")?;
345 assert!(!buf.is_empty());
346 Ok(())
347 }
348
349 #[test]
350 fn vergen_key_ord_works() {
351 assert_eq!(
352 VergenKey::CargoDebug.cmp(&VergenKey::BuildDate),
353 Ordering::Greater
354 );
355 }
356
357 #[test]
358 fn vergen_key_partial_ord_works() {
359 assert_eq!(
360 VergenKey::CargoDebug.partial_cmp(&VergenKey::BuildDate),
361 Some(Ordering::Greater)
362 );
363 }
364
365 #[test]
366 fn vergen_key_hash_works() {
367 let mut first = DefaultHasher::new();
371 VergenKey::BuildDate.hash(&mut first);
372 let mut second = DefaultHasher::new();
373 VergenKey::BuildDate.hash(&mut second);
374 assert_eq!(first.finish(), second.finish());
375
376 let mut other = DefaultHasher::new();
377 VergenKey::CargoDebug.hash(&mut other);
378 assert_ne!(first.finish(), other.finish());
379 }
380}