diff options
author | eth3lbert <eth3lbert+dev@gmail.com> | 2023-08-18 23:10:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-18 08:10:47 -0700 |
commit | b6d8ecbd4f3f863b2c587ff367f5409787f7c2f8 (patch) | |
tree | ecbdb79ffa408a6a91766c9b905af8b924d94770 /src/version.rs | |
parent | 803a3a673ce6e26f6e92167c7309589cf5456202 (diff) |
feat: display commit info in --version (#558)
This improves --version output for #554.
Diffstat (limited to 'src/version.rs')
-rw-r--r-- | src/version.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 0000000000..8391e2674c --- /dev/null +++ b/src/version.rs @@ -0,0 +1,50 @@ +use std::fmt; + +use lazy_static::lazy_static; + +pub struct CommitInfo { + pub short_commit_hash: &'static str, + pub commit_hash: &'static str, + pub commit_date: &'static str, +} + +pub struct VersionInfo { + pub version: &'static str, + pub commit_info: Option<CommitInfo>, +} + +impl fmt::Display for VersionInfo { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.version)?; + + if let Some(ref ci) = &self.commit_info { + write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?; + } + Ok(()) + } +} + +lazy_static! { + pub static ref VERSION: String = version().to_string(); +} + +pub const fn version() -> VersionInfo { + let version = env!("CARGO_PKG_VERSION"); + let commit_info = match ( + option_env!("DFT_COMMIT_SHORT_HASH"), + option_env!("DFT_COMMIT_HASH"), + option_env!("DFT_COMMIT_DATE"), + ) { + (Some(short_commit_hash), Some(commit_hash), Some(commit_date)) => Some(CommitInfo { + short_commit_hash, + commit_hash, + commit_date, + }), + _ => None, + }; + + VersionInfo { + version, + commit_info, + } +} |