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