conmonrs/
version.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
//! Generic version information for conmon

#![allow(clippy::uninlined_format_args)]
#![allow(clippy::needless_raw_string_hashes)]

use getset::CopyGetters;
use shadow_rs::shadow;

shadow!(build);

#[derive(CopyGetters, Debug, Default, Eq, PartialEq)]
#[getset(get_copy = "pub")]
/// The version structure.
pub struct Version {
    /// Specifies if the output should contain verbose debug information.
    verbose: bool,

    /// The current crate version.
    version: &'static str,

    /// The tag of the build, empty if not available.
    tag: &'static str,

    /// The git commit SHA of the build.
    commit: &'static str,

    /// The build date string.
    build_date: &'static str,

    /// The target triple string.
    target: &'static str,

    /// The used Rust version.
    rust_version: &'static str,

    /// The used Cargo version.
    cargo_version: &'static str,

    /// The cargo dependency tree, only available in verbose output.
    cargo_tree: &'static str,
}

impl Version {
    /// Create a new Version instance.
    pub fn new(verbose: bool) -> Self {
        Self {
            verbose,
            version: build::PKG_VERSION,
            tag: build::TAG,
            commit: build::COMMIT_HASH,
            build_date: build::BUILD_TIME,
            target: build::BUILD_TARGET,
            rust_version: build::RUST_VERSION,
            cargo_version: build::CARGO_VERSION,
            cargo_tree: if verbose { build::CARGO_TREE } else { "" },
        }
    }

    /// Print the version information to stdout.
    pub fn print(&self) {
        println!("version: {}", self.version());
        println!(
            "tag: {}",
            if self.tag().is_empty() {
                "none"
            } else {
                self.tag()
            }
        );
        println!("commit: {}", self.commit());
        println!("build: {}", self.build_date());
        println!("target: {}", self.target());
        println!("{}", self.rust_version());
        println!("{}", self.cargo_version());

        if self.verbose() {
            println!("\ncargo tree: {}", self.cargo_tree());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn version_test() {
        let v = Version::new(false);
        assert_eq!(v.version(), build::PKG_VERSION);
        assert_eq!(v.tag(), build::TAG);
        assert_eq!(v.commit(), build::COMMIT_HASH);
        assert_eq!(v.build_date(), build::BUILD_TIME);
        assert_eq!(v.target(), build::BUILD_TARGET);
        assert_eq!(v.rust_version(), build::RUST_VERSION);
        assert_eq!(v.cargo_version(), build::CARGO_VERSION);
        assert!(v.cargo_tree().is_empty());

        v.print();
    }

    #[test]
    fn version_test_verbose() {
        let v = Version::new(true);
        assert_eq!(v.cargo_tree(), build::CARGO_TREE);
    }
}