conmonrs/
version.rs

1//! Generic version information for conmon
2
3#![allow(clippy::uninlined_format_args)]
4#![allow(clippy::needless_raw_string_hashes)]
5
6use getset::CopyGetters;
7use shadow_rs::shadow;
8
9shadow!(build);
10
11#[derive(CopyGetters, Debug, Default, Eq, PartialEq)]
12#[getset(get_copy = "pub")]
13/// The version structure.
14pub struct Version {
15    /// Specifies if the output should contain verbose debug information.
16    verbose: bool,
17
18    /// The current crate version.
19    version: &'static str,
20
21    /// The tag of the build, empty if not available.
22    tag: &'static str,
23
24    /// The git commit SHA of the build.
25    commit: &'static str,
26
27    /// The build date string.
28    build_date: &'static str,
29
30    /// The target triple string.
31    target: &'static str,
32
33    /// The used Rust version.
34    rust_version: &'static str,
35
36    /// The used Cargo version.
37    cargo_version: &'static str,
38
39    /// The cargo dependency tree, only available in verbose output.
40    cargo_tree: &'static str,
41}
42
43impl Version {
44    /// Create a new Version instance.
45    pub fn new(verbose: bool) -> Self {
46        Self {
47            verbose,
48            version: build::PKG_VERSION,
49            tag: build::TAG,
50            commit: build::COMMIT_HASH,
51            build_date: build::BUILD_TIME,
52            target: build::BUILD_TARGET,
53            rust_version: build::RUST_VERSION,
54            cargo_version: build::CARGO_VERSION,
55            cargo_tree: if verbose { build::CARGO_TREE } else { "" },
56        }
57    }
58
59    /// Print the version information to stdout.
60    pub fn print(&self) {
61        println!("version: {}", self.version());
62        println!(
63            "tag: {}",
64            if self.tag().is_empty() {
65                "none"
66            } else {
67                self.tag()
68            }
69        );
70        println!("commit: {}", self.commit());
71        println!("build: {}", self.build_date());
72        println!("target: {}", self.target());
73        println!("{}", self.rust_version());
74        println!("{}", self.cargo_version());
75
76        if self.verbose() {
77            println!("\ncargo tree: {}", self.cargo_tree());
78        }
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn version_test() {
88        let v = Version::new(false);
89        assert_eq!(v.version(), build::PKG_VERSION);
90        assert_eq!(v.tag(), build::TAG);
91        assert_eq!(v.commit(), build::COMMIT_HASH);
92        assert_eq!(v.build_date(), build::BUILD_TIME);
93        assert_eq!(v.target(), build::BUILD_TARGET);
94        assert_eq!(v.rust_version(), build::RUST_VERSION);
95        assert_eq!(v.cargo_version(), build::CARGO_VERSION);
96        assert!(v.cargo_tree().is_empty());
97
98        v.print();
99    }
100
101    #[test]
102    fn version_test_verbose() {
103        let v = Version::new(true);
104        assert_eq!(v.cargo_tree(), build::CARGO_TREE);
105    }
106}