Skip to main content

basalt_tui/
version.rs

1use std::fmt;
2
3#[derive(Default)]
4pub struct VersionInfo {
5    pub version: &'static str,
6    pub hash: Option<&'static str>,
7    pub short_hash: Option<&'static str>,
8    pub date: Option<&'static str>,
9}
10
11impl VersionInfo {
12    pub const fn from_env() -> Self {
13        Self {
14            version: env!("CARGO_PKG_VERSION"),
15            hash: option_env!("BASALT_COMMIT_HASH"),
16            short_hash: option_env!("BASALT_COMMIT_SHORT_HASH"),
17            date: option_env!("BASALT_COMMIT_DATE"),
18        }
19    }
20}
21
22impl fmt::Display for VersionInfo {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "{}", self.version)?;
25        match (self.short_hash, self.date) {
26            (None, _) => Ok(()),
27            (Some(short_hash), None) => write!(f, " ({})", short_hash),
28            (Some(short_hash), Some(date)) => write!(f, " ({} {})", short_hash, date),
29        }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn with_commit_info() {
39        let info = VersionInfo {
40            version: "0.12.5",
41            hash: Some("abc123def0123456789"),
42            short_hash: Some("abc123def"),
43            date: Some("2026-05-15"),
44        };
45        assert_eq!(info.to_string(), "0.12.5 (abc123def 2026-05-15)");
46    }
47
48    #[test]
49    fn without_commit_info() {
50        let info = VersionInfo {
51            version: "0.12.5",
52            ..Default::default()
53        };
54        assert_eq!(info.to_string(), "0.12.5");
55    }
56}