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
use std::fmt::{Display, Formatter, Result as FmtResult};

include!(concat!(env!("OUT_DIR"), "/build_time.rs"));

/// Contains info about current build:
/// name, version, commit and build time (if built with `--release` flag).
/// To get current info, use `build_info` helper.
#[derive(Debug, Clone)]
pub struct BuildInfo {
    name: &'static str,
    version: &'static str,
    commit: &'static str,
    build_time: &'static str,
}

impl BuildInfo {
    /// Creates struct with basic build information.
    pub fn new() -> Self {
        Self {
            name: env!("CARGO_PKG_NAME"),
            version: env!("CARGO_PKG_VERSION"),
            commit: option_env!("PEARL_COMMIT_HASH").unwrap_or("hash-undefined"),
            build_time: BUILD_TIME,
        }
    }

    /// Get a reference to the build info's name.
    pub fn name(&self) -> &'static str {
        self.name
    }

    /// Get a reference to the build info's version.
    pub fn version(&self) -> &'static str {
        self.version
    }

    /// Get a reference to the build info's commit.
    pub fn commit(&self) -> &'static str {
        self.commit
    }

    /// Get a reference to the build info's build time.
    pub fn build_time(&self) -> &'static str {
        self.build_time
    }
}

impl Display for BuildInfo {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        writeln!(
            f,
            "{} {} (commit: {}, built on: {})",
            self.name, self.version, self.commit, self.build_time
        )
    }
}

#[test]
fn print_build_info() {
    println!("{}", BuildInfo::new());
}