use chrono::{DateTime, Utc};
#[must_use]
pub fn is_snapshot_version(version: &str) -> bool {
version.ends_with("-SNAPSHOT")
}
#[must_use]
pub fn base_version(version: &str) -> &str {
version.strip_suffix("-SNAPSHOT").unwrap_or(version)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SnapshotTimestamp(pub DateTime<Utc>);
impl SnapshotTimestamp {
#[must_use]
pub fn format(&self) -> String {
self.0.format("%Y%m%d.%H%M%S").to_string()
}
#[must_use]
pub fn compose_version(&self, base_snapshot: &str, build_number: u32) -> String {
let base = base_version(base_snapshot);
format!("{base}-{}-{build_number}", self.format())
}
#[must_use]
pub fn now() -> Self {
Self(Utc::now())
}
}
#[cfg(test)]
mod tests {
use super::{SnapshotTimestamp, base_version, is_snapshot_version};
use chrono::TimeZone;
#[test]
fn snapshot_suffix_detection() {
assert!(is_snapshot_version("1.0-SNAPSHOT"));
assert!(is_snapshot_version("1.2.3-SNAPSHOT"));
assert!(!is_snapshot_version("1.0"));
assert!(!is_snapshot_version("1.0-20260423.000000-1"));
}
#[test]
fn base_version_strips_suffix() {
assert_eq!(base_version("1.2.3-SNAPSHOT"), "1.2.3");
assert_eq!(base_version("1.2.3"), "1.2.3");
}
#[test]
fn timestamp_formats_as_maven_wants() {
let t = SnapshotTimestamp(
chrono::Utc
.with_ymd_and_hms(2026, 4, 23, 12, 30, 45)
.single()
.expect("valid ts"),
);
assert_eq!(t.format(), "20260423.123045");
}
#[test]
fn compose_version_inserts_timestamp_and_build_number() {
let t = SnapshotTimestamp(
chrono::Utc
.with_ymd_and_hms(2026, 4, 23, 12, 30, 45)
.single()
.expect("valid ts"),
);
let v = t.compose_version("1.2.3-SNAPSHOT", 7);
assert_eq!(v, "1.2.3-20260423.123045-7");
}
}