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};
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    /// 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 cargo debug flag (`VERGEN_CARGO_DEBUG`)
51        #[cfg(feature = "cargo")]
52        CargoDebug,
53        /// The cargo features (`VERGEN_CARGO_FEATURES`)
54        #[cfg(feature = "cargo")]
55        CargoFeatures,
56        /// The cargo opt level (`VERGEN_CARGO_OPT_LEVEL`)
57        #[cfg(feature = "cargo")]
58        CargoOptLevel,
59        /// The cargo target triple (`VERGEN_CARGO_TARGET_TRIPLE`)
60        #[cfg(feature = "cargo")]
61        CargoTargetTriple,
62        /// The cargo dependencies (`VERGEN_CARGO_DEPENDENCIES`)
63        #[cfg(feature = "cargo")]
64        CargoDependencies,
65        /// The current working branch name (`VERGEN_GIT_BRANCH`)
66        #[cfg(feature = "git")]
67        GitBranch,
68        /// The commit author's email. (`VERGEN_GIT_COMMIT_AUTHOR_EMAIL`)
69        #[cfg(feature = "git")]
70        GitCommitAuthorEmail,
71        /// The commit author's name. (`VERGEN_GIT_COMMIT_AUTHOR_NAME`)
72        #[cfg(feature = "git")]
73        GitCommitAuthorName,
74        /// Number of commits in current branch. (`VERGEN_GIT_COMMIT_COUNT`)
75        #[cfg(feature = "git")]
76        GitCommitCount,
77        /// The commit date. (`VERGEN_GIT_COMMIT_DATE`)
78        #[cfg(feature = "git")]
79        GitCommitDate,
80        /// Commit message (`VERGEN_GIT_COMMIT_MESSAGE`)
81        #[cfg(feature = "git")]
82        GitCommitMessage,
83        /// The commit timestamp. (`VERGEN_GIT_COMMIT_TIMESTAMP`)
84        #[cfg(feature = "git")]
85        GitCommitTimestamp,
86        /// The semver version from the last git tag. (`VERGEN_GIT_SEMVER`)
87        #[cfg(feature = "git")]
88        GitDescribe,
89        /// The latest commit SHA. (`VERGEN_GIT_SHA`)
90        #[cfg(feature = "git")]
91        GitSha,
92        /// Whether the repository is dirty. (`VERGEN_GIT_DIRTY`)
93        #[cfg(feature = "git")]
94        GitDirty,
95        /// The release channel of the rust compiler. (`VERGEN_RUSTC_CHANNEL`)
96        #[cfg(feature = "rustc")]
97        RustcChannel,
98        /// The rustc commit date. (`VERGEN_RUSTC_COMMIT_DATE`)
99        #[cfg(feature = "rustc")]
100        RustcCommitDate,
101        /// The rustc commit hash. (`VERGEN_RUSTC_COMMIT_HASH`)
102        #[cfg(feature = "rustc")]
103        RustcCommitHash,
104        /// The host triple. (`VERGEN_HOST_TRIPLE`)
105        #[cfg(feature = "rustc")]
106        RustcHostTriple,
107        /// The rustc LLVM version. (`VERGEN_RUSTC_LLVM_VERSION`)
108        #[cfg(feature = "rustc")]
109        RustcLlvmVersion,
110        /// The version information of the rust compiler. (`VERGEN_RUSTC_SEMVER`)
111        #[cfg(feature = "rustc")]
112        RustcSemver,
113        /// The sysinfo system name (`VERGEN_SYSINFO_NAME`)
114        #[cfg(feature = "si")]
115        SysinfoName,
116        /// The sysinfo os version (`VERGEN_SYSINFO_OS_VERSION`)
117        #[cfg(feature = "si")]
118        SysinfoOsVersion,
119        /// The sysinfo user name (`VERGEN_SYSINFO_USER`)
120        #[cfg(feature = "si")]
121        SysinfoUser,
122        /// The sysinfo total memory (`VERGEN_SYSINFO_TOTAL_MEMORY`)
123        #[cfg(feature = "si")]
124        SysinfoMemory,
125        /// The sysinfo cpu vendor (`VERGEN_SYSINFO_CPU_VENDOR`)
126        #[cfg(feature = "si")]
127        SysinfoCpuVendor,
128        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_CORE_COUNT`)
129        #[cfg(feature = "si")]
130        SysinfoCpuCoreCount,
131        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_NAME`)
132        #[cfg(feature = "si")]
133        SysinfoCpuName,
134        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_BRAND`)
135        #[cfg(feature = "si")]
136        SysinfoCpuBrand,
137        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_FREQUENCY`)
138        #[cfg(feature = "si")]
139        SysinfoCpuFrequency,
140    }
141
142    impl VergenKey {
143        /// Get the name for the given key.
144        #[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/// The [`VergenKey`] enum to use when no features are configured.
217#[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    /// The [`VergenKey`] enum to use when no features are configured.
226    #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
227    pub enum VergenKey {
228        /// An empty vergen key
229        Empty,
230    }
231
232    impl VergenKey {
233        /// Get the name for the given key.
234        #[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}