use std::sync::OnceLock; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); struct Identity { commit: &'static str, date: &'static str, } static IDENTITY: OnceLock = OnceLock::new(); pub fn set_identity(commit: &'static str, date: &'static str) { let _ = IDENTITY.set(Identity { commit, date }); } pub fn commit() -> &'static str { IDENTITY.get().map_or("unknown", |i| i.commit) } pub fn date() -> &'static str { IDENTITY.get().map_or("unknown", |i| i.date) } pub fn display() -> String { format_build(VERSION, commit(), date()) } pub fn format_build(version: &str, commit: &str, date: &str) -> String { format!("{version} ({commit}, {date})") } pub fn commit_of(build: &str) -> Option<&str> { let inner = build.split_once('(')?.1; let commit = inner.split([',', ')']).next()?.trim(); (!commit.is_empty() && commit != "unknown").then_some(commit) } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Freshness { UpToDate, Outdated, Unknown, } pub fn compare(runner: Option<&str>, server: &str) -> Freshness { match (runner.and_then(commit_of), commit_of(server)) { (Some(r), Some(s)) if r == s => Freshness::UpToDate, (Some(_), Some(_)) => Freshness::Outdated, _ => Freshness::Unknown, } } #[cfg(test)] mod tests { use super::*; #[test] fn up_to_date_compares_the_commit_only() { let server = format_build("0.1.0", "cf8e648", "2026-10-20"); assert_eq!(server, "0.1.0 (cf8e648, 2026-10-20)"); let older = format_build("0.1.0", "4d21658", "2026-10-19"); assert_eq!(compare(Some(&older), &server), Freshness::Outdated); assert_eq!(compare(None, &server), Freshness::Unknown); assert_eq!(compare(Some("0.1.0"), &server), Freshness::Unknown); let unknown = format_build("0.1.0", "unknown", "unknown"); assert_eq!(compare(Some(&server), &unknown), Freshness::Unknown); } }