Skip to main content

vergen_lib/
keys.rs

1// Copyright (c) 2022 vergen developers
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9/// The [`VergenKey`] enum to use based on the configured features.
10#[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    /// The keys used in the [`crate::CargoRustcEnvMap`]
42    #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
43    pub enum VergenKey {
44        /// The build date. (`VERGEN_BUILD_DATE`)
45        #[cfg(feature = "build")]
46        BuildDate,
47        /// The build timestamp. (`VERGEN_BUILD_TIMESTAMP`)
48        #[cfg(feature = "build")]
49        BuildTimestamp,
50        /// The build timestamp as Unix seconds. (`VERGEN_BUILD_TIMESTAMP_UNIX`)
51        #[cfg(feature = "build")]
52        BuildTimestampUnix,
53        /// The cargo debug flag (`VERGEN_CARGO_DEBUG`)
54        #[cfg(feature = "cargo")]
55        CargoDebug,
56        /// The cargo features (`VERGEN_CARGO_FEATURES`)
57        #[cfg(feature = "cargo")]
58        CargoFeatures,
59        /// The cargo opt level (`VERGEN_CARGO_OPT_LEVEL`)
60        #[cfg(feature = "cargo")]
61        CargoOptLevel,
62        /// The cargo target triple (`VERGEN_CARGO_TARGET_TRIPLE`)
63        #[cfg(feature = "cargo")]
64        CargoTargetTriple,
65        /// The cargo dependencies (`VERGEN_CARGO_DEPENDENCIES`)
66        #[cfg(feature = "cargo")]
67        CargoDependencies,
68        /// The current working branch name (`VERGEN_GIT_BRANCH`)
69        #[cfg(feature = "git")]
70        GitBranch,
71        /// The commit author's email. (`VERGEN_GIT_COMMIT_AUTHOR_EMAIL`)
72        #[cfg(feature = "git")]
73        GitCommitAuthorEmail,
74        /// The commit author's name. (`VERGEN_GIT_COMMIT_AUTHOR_NAME`)
75        #[cfg(feature = "git")]
76        GitCommitAuthorName,
77        /// Number of commits in current branch. (`VERGEN_GIT_COMMIT_COUNT`)
78        #[cfg(feature = "git")]
79        GitCommitCount,
80        /// The commit date. (`VERGEN_GIT_COMMIT_DATE`)
81        #[cfg(feature = "git")]
82        GitCommitDate,
83        /// Commit message (`VERGEN_GIT_COMMIT_MESSAGE`)
84        #[cfg(feature = "git")]
85        GitCommitMessage,
86        /// The commit timestamp. (`VERGEN_GIT_COMMIT_TIMESTAMP`)
87        #[cfg(feature = "git")]
88        GitCommitTimestamp,
89        /// The commit timestamp as Unix seconds. (`VERGEN_GIT_COMMIT_TIMESTAMP_UNIX`)
90        #[cfg(feature = "git")]
91        GitCommitTimestampUnix,
92        /// The semver version from the last git tag. (`VERGEN_GIT_SEMVER`)
93        #[cfg(feature = "git")]
94        GitDescribe,
95        /// The latest commit SHA. (`VERGEN_GIT_SHA`)
96        #[cfg(feature = "git")]
97        GitSha,
98        /// Whether the repository is dirty. (`VERGEN_GIT_DIRTY`)
99        #[cfg(feature = "git")]
100        GitDirty,
101        /// The release channel of the rust compiler. (`VERGEN_RUSTC_CHANNEL`)
102        #[cfg(feature = "rustc")]
103        RustcChannel,
104        /// The rustc commit date. (`VERGEN_RUSTC_COMMIT_DATE`)
105        #[cfg(feature = "rustc")]
106        RustcCommitDate,
107        /// The rustc commit hash. (`VERGEN_RUSTC_COMMIT_HASH`)
108        #[cfg(feature = "rustc")]
109        RustcCommitHash,
110        /// The host triple. (`VERGEN_HOST_TRIPLE`)
111        #[cfg(feature = "rustc")]
112        RustcHostTriple,
113        /// The rustc LLVM version. (`VERGEN_RUSTC_LLVM_VERSION`)
114        #[cfg(feature = "rustc")]
115        RustcLlvmVersion,
116        /// The version information of the rust compiler. (`VERGEN_RUSTC_SEMVER`)
117        #[cfg(feature = "rustc")]
118        RustcSemver,
119        /// The sysinfo system name (`VERGEN_SYSINFO_NAME`)
120        #[cfg(feature = "si")]
121        SysinfoName,
122        /// The sysinfo os version (`VERGEN_SYSINFO_OS_VERSION`)
123        #[cfg(feature = "si")]
124        SysinfoOsVersion,
125        /// The sysinfo user name (`VERGEN_SYSINFO_USER`)
126        #[cfg(feature = "si")]
127        SysinfoUser,
128        /// The sysinfo total memory (`VERGEN_SYSINFO_TOTAL_MEMORY`)
129        #[cfg(feature = "si")]
130        SysinfoMemory,
131        /// The sysinfo cpu vendor (`VERGEN_SYSINFO_CPU_VENDOR`)
132        #[cfg(feature = "si")]
133        SysinfoCpuVendor,
134        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_CORE_COUNT`)
135        #[cfg(feature = "si")]
136        SysinfoCpuCoreCount,
137        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_NAME`)
138        #[cfg(feature = "si")]
139        SysinfoCpuName,
140        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_BRAND`)
141        #[cfg(feature = "si")]
142        SysinfoCpuBrand,
143        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_FREQUENCY`)
144        #[cfg(feature = "si")]
145        SysinfoCpuFrequency,
146    }
147
148    impl VergenKey {
149        /// Get the name for the given key.
150        #[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/// The [`VergenKey`] enum to use when no features are configured.
227#[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    /// The [`VergenKey`] enum to use when no features are configured.
236    #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
237    pub enum VergenKey {
238        /// An empty vergen key
239        Empty,
240    }
241
242    impl VergenKey {
243        /// Get the name for the given key.
244        #[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        // Assert hashing is deterministic rather than a hard-coded digest: the
311        // derived enum-discriminant hash is pointer-width dependent, so a fixed
312        // value only holds on 64-bit targets (see #494).
313        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        // Assert hashing is deterministic rather than a hard-coded digest: the
368        // derived enum-discriminant hash is pointer-width dependent, so a fixed
369        // value only holds on 64-bit targets (see #494).
370        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}