1use serde::Serialize;
4use std::sync::LazyLock;
5
6#[rustfmt::skip] const GIT_BRANCH: &str = env!("BUILD_GIT_BRANCH");
7#[allow(dead_code)]
8#[rustfmt::skip] const GIT_HASH: &str = env!("BUILD_GIT_HASH");
9#[rustfmt::skip] const GIT_DESCRIBE: &str = env!("BUILD_GIT_DESCRIBE");
10#[rustfmt::skip] const GIT_DATE: &str = env!("BUILD_GIT_DATE");
11#[rustfmt::skip] const BUILD_DEBUG: &str = env!("BUILD_CARGO_DEBUG");
12#[rustfmt::skip] const BUILD_TARGET: &str = env!("BUILD_CARGO_TARGET");
13
14pub fn rustc_version() -> &'static str {
16 static RUSTC_VERSION: LazyLock<String> = LazyLock::new(|| {
17 let meta = rustc_version_runtime::version_meta();
18 format!("{} {:?}", meta.short_version_string, meta.channel)
19 });
20
21 &RUSTC_VERSION
22}
23
24pub fn build_profile() -> &'static str {
26 if BUILD_DEBUG == "true" {
27 "debug"
28 } else {
29 "release"
30 }
31}
32
33#[rustfmt::skip]
35pub fn version_info() -> &'static str {
36 static VERSION_INFO: LazyLock<String> = LazyLock::new(||{
37 let prof = build_profile();
38 let rustc = rustc_version();
39
40 format!(
41"Build: {prof} ({BUILD_TARGET})
42Version: {GIT_BRANCH} {GIT_DESCRIBE} {GIT_DATE}
43{rustc}"
44 )
45 });
46
47 &VERSION_INFO
48}
49
50#[derive(Debug, Serialize)]
51pub struct VersionInfo {
52 pub build: String,
53 pub version: String,
54 pub rustc: String,
55}
56
57pub fn version_info_struct() -> &'static VersionInfo {
59 static VERSION_INFO: LazyLock<VersionInfo> = LazyLock::new(|| {
60 let prof = build_profile();
61 let rustc = rustc_version();
62
63 VersionInfo {
64 build: format!("{prof} ({BUILD_TARGET})"),
65 version: format!("{GIT_BRANCH} {GIT_DESCRIBE} {GIT_DATE}"),
66 rustc: rustc.to_string(),
67 }
68 });
69
70 &VERSION_INFO
71}
72
73pub fn version_info_vec() -> &'static Vec<String> {
75 static VERSION_INFO_VEC: LazyLock<Vec<String>> = LazyLock::new(|| {
76 let prof = build_profile();
77 let rustc = rustc_version();
78 vec![
79 format!("Build: {prof} ({BUILD_TARGET})"),
80 format!("Branch: {GIT_BRANCH}"),
81 format!("Version: {GIT_DESCRIBE}"),
82 format!("Last Updated: {GIT_DATE}"),
83 rustc.to_string(),
84 ]
85 });
86
87 &VERSION_INFO_VEC
88}