brinkhaustools/
version.rs

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::path::Path;
use std::time::Duration;

use serde_json::Value;

use crate::status::{StatusSource, StatusValue};

/// Loads version metadata from a JSON file.
pub struct VersionInformation {
    data: Value,
}

impl VersionInformation {
    /// Load version information from a JSON file.
    /// Returns defaults if the file is missing or invalid.
    pub fn load(path: impl AsRef<Path>) -> Self {
        let data = match std::fs::read_to_string(path.as_ref()) {
            Ok(contents) => serde_json::from_str(&contents).unwrap_or_else(|_| Self::defaults()),
            Err(_) => Self::defaults(),
        };
        Self { data }
    }

    /// Get the version string.
    pub fn version(&self) -> &str {
        self.data["Version"].as_str().unwrap_or("dev")
    }

    /// Get the full metadata.
    pub fn data(&self) -> &Value {
        &self.data
    }

    fn defaults() -> Value {
        serde_json::json!({
            "Project_Name": "Unknown",
            "Version": "dev",
            "Branch": "Unknown",
            "CI_Time": "Unknown",
            "Architecture": "Unknown",
        })
    }
}

impl StatusSource for VersionInformation {
    fn get_status(&self) -> Value {
        serde_json::json!({
            "Version": StatusValue::text(self.version()).label("status.version.version"),
            "Branch": StatusValue::text(self.data["Branch"].as_str().unwrap_or("Unknown"))
                .label("status.version.branch"),
            "CI-Time": StatusValue::text(self.data["CI_Time"].as_str().unwrap_or("Unknown"))
                .label("status.version.ciTime"),
        })
    }

    fn get_name(&self) -> &str {
        "Version information"
    }

    fn refresh_interval(&self) -> Duration {
        Duration::from_secs(3600) // version rarely changes
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_load_valid_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("version.json");
        fs::write(
            &path,
            r#"{"Version": "1.2.3", "Branch": "main", "CI_Time": "2026-03-15"}"#,
        )
        .unwrap();
        let v = VersionInformation::load(&path);
        assert_eq!(v.version(), "1.2.3");
        assert_eq!(v.data()["Branch"], "main");
    }

    #[test]
    fn test_missing_file_defaults() {
        let v = VersionInformation::load("nonexistent_file.json");
        assert_eq!(v.version(), "dev");
        assert_eq!(v.data()["Branch"], "Unknown");
    }

    #[test]
    fn test_status_source_impl() {
        let v = VersionInformation::load("nonexistent.json");
        assert_eq!(v.get_name(), "Version information");
        let status = v.get_status();
        assert_eq!(status["Version"]["value"], "dev");
        assert_eq!(status["Version"]["type"], "string");
        assert_eq!(status["Version"]["label"], "status.version.version");
        assert_eq!(status["Branch"]["label"], "status.version.branch");
        assert_eq!(status["CI-Time"]["label"], "status.version.ciTime");
    }
}