vergen_lib/
keys.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2022 vergen developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

/// The [`VergenKey`] enum to use based on the configured features.
#[cfg(any(
    feature = "build",
    feature = "cargo",
    feature = "git",
    feature = "rustc",
    feature = "si"
))]
pub(crate) mod vergen_key {
    #[cfg(feature = "build")]
    use crate::constants::{BUILD_DATE_NAME, BUILD_TIMESTAMP_NAME};
    #[cfg(feature = "cargo")]
    use crate::constants::{
        CARGO_DEBUG, CARGO_DEPENDENCIES, CARGO_FEATURES, CARGO_OPT_LEVEL, CARGO_TARGET_TRIPLE,
    };
    #[cfg(feature = "git")]
    use crate::constants::{
        GIT_BRANCH_NAME, GIT_COMMIT_AUTHOR_EMAIL, GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_COUNT,
        GIT_COMMIT_DATE_NAME, GIT_COMMIT_MESSAGE, GIT_COMMIT_TIMESTAMP_NAME, GIT_DESCRIBE_NAME,
        GIT_DIRTY_NAME, GIT_SHA_NAME,
    };
    #[cfg(feature = "rustc")]
    use crate::constants::{
        RUSTC_CHANNEL_NAME, RUSTC_COMMIT_DATE, RUSTC_COMMIT_HASH, RUSTC_HOST_TRIPLE_NAME,
        RUSTC_LLVM_VERSION, RUSTC_SEMVER_NAME,
    };
    #[cfg(feature = "si")]
    use crate::constants::{
        SYSINFO_CPU_BRAND, SYSINFO_CPU_CORE_COUNT, SYSINFO_CPU_FREQUENCY, SYSINFO_CPU_NAME,
        SYSINFO_CPU_VENDOR, SYSINFO_MEMORY, SYSINFO_NAME, SYSINFO_OS_VERSION, SYSINFO_USER,
    };

    /// The keys used in the [`crate::CargoRustcEnvMap`]
    #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
    pub enum VergenKey {
        /// The build date. (`VERGEN_BUILD_DATE`)
        #[cfg(feature = "build")]
        BuildDate,
        /// The build timestamp. (`VERGEN_BUILD_TIMESTAMP`)
        #[cfg(feature = "build")]
        BuildTimestamp,
        /// The cargo debug flag (`VERGEN_CARGO_DEBUG`)
        #[cfg(feature = "cargo")]
        CargoDebug,
        /// The cargo features (`VERGEN_CARGO_FEATURES`)
        #[cfg(feature = "cargo")]
        CargoFeatures,
        /// The cargo opt level (`VERGEN_CARGO_OPT_LEVEL`)
        #[cfg(feature = "cargo")]
        CargoOptLevel,
        /// The cargo target triple (`VERGEN_CARGO_TARGET_TRIPLE`)
        #[cfg(feature = "cargo")]
        CargoTargetTriple,
        /// The cargo dependencies (`VERGEN_CARGO_DEPENDENCIES`)
        #[cfg(feature = "cargo")]
        CargoDependencies,
        /// The current working branch name (`VERGEN_GIT_BRANCH`)
        #[cfg(feature = "git")]
        GitBranch,
        /// The commit author's email. (`VERGEN_GIT_COMMIT_AUTHOR_EMAIL`)
        #[cfg(feature = "git")]
        GitCommitAuthorEmail,
        /// The commit author's name. (`VERGEN_GIT_COMMIT_AUTHOR_NAME`)
        #[cfg(feature = "git")]
        GitCommitAuthorName,
        /// Number of commits in current branch. (`VERGEN_GIT_COMMIT_COUNT`)
        #[cfg(feature = "git")]
        GitCommitCount,
        /// The commit date. (`VERGEN_GIT_COMMIT_DATE`)
        #[cfg(feature = "git")]
        GitCommitDate,
        /// Commit message (`VERGEN_GIT_COMMIT_MESSAGE`)
        #[cfg(feature = "git")]
        GitCommitMessage,
        /// The commit timestamp. (`VERGEN_GIT_COMMIT_TIMESTAMP`)
        #[cfg(feature = "git")]
        GitCommitTimestamp,
        /// The semver version from the last git tag. (`VERGEN_GIT_SEMVER`)
        #[cfg(feature = "git")]
        GitDescribe,
        /// The latest commit SHA. (`VERGEN_GIT_SHA`)
        #[cfg(feature = "git")]
        GitSha,
        /// Whether the repository is dirty. (`VERGEN_GIT_DIRTY`)
        #[cfg(feature = "git")]
        GitDirty,
        /// The release channel of the rust compiler. (`VERGEN_RUSTC_CHANNEL`)
        #[cfg(feature = "rustc")]
        RustcChannel,
        /// The rustc commit date. (`VERGEN_RUSTC_COMMIT_DATE`)
        #[cfg(feature = "rustc")]
        RustcCommitDate,
        /// The rustc commit hash. (`VERGEN_RUSTC_COMMIT_HASH`)
        #[cfg(feature = "rustc")]
        RustcCommitHash,
        /// The host triple. (`VERGEN_HOST_TRIPLE`)
        #[cfg(feature = "rustc")]
        RustcHostTriple,
        /// The rustc LLVM version. (`VERGEN_RUSTC_LLVM_VERSION`)
        #[cfg(feature = "rustc")]
        RustcLlvmVersion,
        /// The version information of the rust compiler. (`VERGEN_RUSTC_SEMVER`)
        #[cfg(feature = "rustc")]
        RustcSemver,
        /// The sysinfo system name (`VERGEN_SYSINFO_NAME`)
        #[cfg(feature = "si")]
        SysinfoName,
        /// The sysinfo os version (`VERGEN_SYSINFO_OS_VERSION`)
        #[cfg(feature = "si")]
        SysinfoOsVersion,
        /// The sysinfo user name (`VERGEN_SYSINFO_USER`)
        #[cfg(feature = "si")]
        SysinfoUser,
        /// The sysinfo total memory (`VERGEN_SYSINFO_TOTAL_MEMORY`)
        #[cfg(feature = "si")]
        SysinfoMemory,
        /// The sysinfo cpu vendor (`VERGEN_SYSINFO_CPU_VENDOR`)
        #[cfg(feature = "si")]
        SysinfoCpuVendor,
        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_CORE_COUNT`)
        #[cfg(feature = "si")]
        SysinfoCpuCoreCount,
        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_NAME`)
        #[cfg(feature = "si")]
        SysinfoCpuName,
        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_BRAND`)
        #[cfg(feature = "si")]
        SysinfoCpuBrand,
        /// The sysinfo cpu core count (`VERGEN_SYSINFO_CPU_FREQUENCY`)
        #[cfg(feature = "si")]
        SysinfoCpuFrequency,
    }

    impl VergenKey {
        /// Get the name for the given key.
        #[must_use]
        pub fn name(self) -> &'static str {
            match self {
                #[cfg(feature = "build")]
                VergenKey::BuildDate => BUILD_DATE_NAME,
                #[cfg(feature = "build")]
                VergenKey::BuildTimestamp => BUILD_TIMESTAMP_NAME,
                #[cfg(feature = "cargo")]
                VergenKey::CargoDebug => CARGO_DEBUG,
                #[cfg(feature = "cargo")]
                VergenKey::CargoFeatures => CARGO_FEATURES,
                #[cfg(feature = "cargo")]
                VergenKey::CargoOptLevel => CARGO_OPT_LEVEL,
                #[cfg(feature = "cargo")]
                VergenKey::CargoTargetTriple => CARGO_TARGET_TRIPLE,
                #[cfg(feature = "cargo")]
                VergenKey::CargoDependencies => CARGO_DEPENDENCIES,
                #[cfg(feature = "git")]
                VergenKey::GitBranch => GIT_BRANCH_NAME,
                #[cfg(feature = "git")]
                VergenKey::GitCommitAuthorEmail => GIT_COMMIT_AUTHOR_EMAIL,
                #[cfg(feature = "git")]
                VergenKey::GitCommitAuthorName => GIT_COMMIT_AUTHOR_NAME,
                #[cfg(feature = "git")]
                VergenKey::GitCommitCount => GIT_COMMIT_COUNT,
                #[cfg(feature = "git")]
                VergenKey::GitCommitDate => GIT_COMMIT_DATE_NAME,
                #[cfg(feature = "git")]
                VergenKey::GitCommitMessage => GIT_COMMIT_MESSAGE,
                #[cfg(feature = "git")]
                VergenKey::GitCommitTimestamp => GIT_COMMIT_TIMESTAMP_NAME,
                #[cfg(feature = "git")]
                VergenKey::GitDescribe => GIT_DESCRIBE_NAME,
                #[cfg(feature = "git")]
                VergenKey::GitSha => GIT_SHA_NAME,
                #[cfg(feature = "git")]
                VergenKey::GitDirty => GIT_DIRTY_NAME,
                #[cfg(feature = "rustc")]
                VergenKey::RustcChannel => RUSTC_CHANNEL_NAME,
                #[cfg(feature = "rustc")]
                VergenKey::RustcCommitDate => RUSTC_COMMIT_DATE,
                #[cfg(feature = "rustc")]
                VergenKey::RustcCommitHash => RUSTC_COMMIT_HASH,
                #[cfg(feature = "rustc")]
                VergenKey::RustcHostTriple => RUSTC_HOST_TRIPLE_NAME,
                #[cfg(feature = "rustc")]
                VergenKey::RustcLlvmVersion => RUSTC_LLVM_VERSION,
                #[cfg(feature = "rustc")]
                VergenKey::RustcSemver => RUSTC_SEMVER_NAME,
                #[cfg(feature = "si")]
                VergenKey::SysinfoName => SYSINFO_NAME,
                #[cfg(feature = "si")]
                VergenKey::SysinfoOsVersion => SYSINFO_OS_VERSION,
                #[cfg(feature = "si")]
                VergenKey::SysinfoUser => SYSINFO_USER,
                #[cfg(feature = "si")]
                VergenKey::SysinfoMemory => SYSINFO_MEMORY,
                #[cfg(feature = "si")]
                VergenKey::SysinfoCpuVendor => SYSINFO_CPU_VENDOR,
                #[cfg(feature = "si")]
                VergenKey::SysinfoCpuCoreCount => SYSINFO_CPU_CORE_COUNT,
                #[cfg(feature = "si")]
                VergenKey::SysinfoCpuName => SYSINFO_CPU_NAME,
                #[cfg(feature = "si")]
                VergenKey::SysinfoCpuBrand => SYSINFO_CPU_BRAND,
                #[cfg(feature = "si")]
                VergenKey::SysinfoCpuFrequency => SYSINFO_CPU_FREQUENCY,
            }
        }
    }
}

/// The [`VergenKey`] enum to use when no features are configured.
#[cfg(not(any(
    feature = "build",
    feature = "cargo",
    feature = "git",
    feature = "rustc",
    feature = "si"
)))]
pub(crate) mod vergen_key {
    /// The [`VergenKey`] enum to use when no features are configured.
    #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
    pub enum VergenKey {
        /// An empty vergen key
        Empty,
    }

    impl VergenKey {
        /// Get the name for the given key.
        #[must_use]
        pub fn name(self) -> &'static str {
            match self {
                VergenKey::Empty => "",
            }
        }
    }
}

#[cfg(all(
    test,
    not(any(
        feature = "build",
        feature = "cargo",
        feature = "git",
        feature = "rustc",
        feature = "si"
    ))
))]
mod test {
    use super::vergen_key::VergenKey;
    use anyhow::Result;
    use std::{
        cmp::Ordering,
        collections::hash_map::DefaultHasher,
        hash::{Hash, Hasher},
        io::Write,
    };

    #[test]
    fn empty_name() {
        assert!(VergenKey::Empty.name().is_empty());
    }

    #[test]
    #[allow(clippy::clone_on_copy, clippy::redundant_clone)]
    fn vergen_key_clone_works() {
        let key = VergenKey::Empty;
        let another = key.clone();
        assert_eq!(another, key);
    }

    #[test]
    fn vergen_key_debug_works() -> Result<()> {
        let key = VergenKey::Empty;
        let mut buf = vec![];
        write!(buf, "{key:?}")?;
        assert!(!buf.is_empty());
        Ok(())
    }

    #[test]
    fn vergen_key_ord_works() {
        assert_eq!(VergenKey::Empty.cmp(&VergenKey::Empty), Ordering::Equal);
    }

    #[test]
    fn vergen_key_partial_ord_works() {
        assert_eq!(
            VergenKey::Empty.partial_cmp(&VergenKey::Empty),
            Some(Ordering::Equal)
        );
    }

    #[test]
    fn vergen_key_hash_works() {
        let mut hasher = DefaultHasher::new();
        VergenKey::Empty.hash(&mut hasher);
        assert_eq!(15_130_871_412_783_076_140, hasher.finish());
    }
}

#[cfg(all(test, all(feature = "build", feature = "cargo")))]
mod test {
    use super::vergen_key::VergenKey;
    use anyhow::Result;
    use std::{
        cmp::Ordering,
        collections::hash_map::DefaultHasher,
        hash::{Hash, Hasher},
        io::Write,
    };

    #[test]
    #[allow(clippy::clone_on_copy, clippy::redundant_clone)]
    fn vergen_key_clone_works() {
        let key = VergenKey::BuildDate;
        let another = key.clone();
        assert_eq!(another, key);
    }

    #[test]
    fn vergen_key_debug_works() -> Result<()> {
        let key = VergenKey::BuildDate;
        let mut buf = vec![];
        write!(buf, "{key:?}")?;
        assert!(!buf.is_empty());
        Ok(())
    }

    #[test]
    fn vergen_key_ord_works() {
        assert_eq!(
            VergenKey::CargoDebug.cmp(&VergenKey::BuildDate),
            Ordering::Greater
        );
    }

    #[test]
    fn vergen_key_partial_ord_works() {
        assert_eq!(
            VergenKey::CargoDebug.partial_cmp(&VergenKey::BuildDate),
            Some(Ordering::Greater)
        );
    }

    #[test]
    fn vergen_key_hash_works() {
        let mut hasher = DefaultHasher::new();
        VergenKey::BuildDate.hash(&mut hasher);
        assert_eq!(13_646_096_770_106_105_413, hasher.finish());
    }
}