Skip to main content

jt_consoleutils/
version.rs

1/// Returns the version string in the format "yyyy-mm-dd (hash)".
2///
3/// Both values are typically injected at build time via `build.rs` using
4/// `env!("BUILD_DATE")` and `env!("GIT_HASH")`, then passed in here.
5///
6/// # Example
7///
8/// ```rust,ignore
9/// // This example is marked `ignore` because `BUILD_DATE` and `GIT_HASH` are
10/// // environment variables injected at compile time by `build.rs`. They are
11/// // not available in the doctest environment, so the example cannot be run
12/// // as a test. It is provided for illustration purposes only.
13/// const BUILD_DATE: &str = env!("BUILD_DATE");
14/// const GIT_HASH: &str = env!("GIT_HASH");
15///
16/// let v = jt_consoleutils::version::version_string(BUILD_DATE, GIT_HASH);
17/// ```
18pub fn version_string(build_date: &str, git_hash: &str) -> String {
19   format!("{} ({})", build_date, git_hash)
20}
21
22#[cfg(test)]
23mod tests {
24   use super::*;
25
26   #[test]
27   fn version_string_formats_correctly() {
28      assert_eq!(version_string("2025-01-15", "abc1234"), "2025-01-15 (abc1234)");
29   }
30
31   #[test]
32   fn version_string_with_unknown_hash() {
33      assert_eq!(version_string("2025-01-15", "unknown"), "2025-01-15 (unknown)");
34   }
35}