verinfo/
lib.rs

1//! バージョン情報。
2
3use std::sync::LazyLock;
4
5#[rustfmt::skip] const GIT_BRANCH:    &str = env!("BUILD_GIT_BRANCH");
6#[allow(dead_code)]
7#[rustfmt::skip] const GIT_HASH:      &str = env!("BUILD_GIT_HASH");
8#[rustfmt::skip] const GIT_DESCRIBE:  &str = env!("BUILD_GIT_DESCRIBE");
9#[rustfmt::skip] const GIT_DATE:      &str = env!("BUILD_GIT_DATE");
10#[rustfmt::skip] const BUILD_DEBUG:   &str = env!("BUILD_CARGO_DEBUG");
11#[rustfmt::skip] const BUILD_TARGET:  &str = env!("BUILD_CARGO_TARGET");
12
13/// rustc コンパイラバージョン "major.minor.patch"
14pub fn rustc_version() -> &'static str {
15    static RUSTC_VERSION: LazyLock<String> = LazyLock::new(|| {
16        let meta = rustc_version_runtime::version_meta();
17        format!("{} {:?}", meta.short_version_string, meta.channel)
18    });
19
20    &RUSTC_VERSION
21}
22
23/// ビルドプロファイルを "debug" または "release" で返す。
24pub fn build_profile() -> &'static str {
25    if BUILD_DEBUG == "true" {
26        "debug"
27    } else {
28        "release"
29    }
30}
31
32/// バージョン情報を読みやすい形の複数行文字列で返す。
33#[rustfmt::skip]
34pub fn version_info() -> &'static str {
35    static VERSION_INFO: LazyLock<String> = LazyLock::new(||{
36        let prof = build_profile();
37        let rustc = rustc_version();
38
39        format!(
40"Build: {prof}
41Branch: {GIT_BRANCH} {GIT_DESCRIBE} {GIT_DATE}
42{rustc}"
43    )
44    });
45
46    &VERSION_INFO
47}
48
49/// バージョン情報を文字列ベクタの形で返す。
50pub fn version_info_vec() -> &'static Vec<String> {
51    static VERSION_INFO_VEC: LazyLock<Vec<String>> = LazyLock::new(|| {
52        let prof = build_profile();
53        let rustc = rustc_version();
54        vec![
55            format!("Build: {BUILD_TARGET} {prof}"),
56            format!("Branch: {GIT_BRANCH}"),
57            format!("Version: {GIT_DESCRIBE}"),
58            format!("Last Updated: {GIT_DATE}"),
59            rustc.to_string(),
60        ]
61    });
62
63    &VERSION_INFO_VEC
64}