use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use sha2::{Digest, Sha256};
use time::macros::format_description;
use time::OffsetDateTime;
static COUNTER: AtomicU64 = AtomicU64::new(0);
pub fn unique() -> String {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or_default();
let count = COUNTER.fetch_add(1, Ordering::Relaxed);
format!("{:x}-{nanos:x}-{count:x}", std::process::id())
}
pub fn session_token() -> String {
format!("s-{}", short_digest(&unique()))
}
pub fn artifact_id() -> String {
format!("a-{}", short_digest(&unique()))
}
pub fn is_safe_name(value: &str) -> bool {
!value.is_empty()
&& value != "."
&& value != ".."
&& value
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))
}
pub fn short_digest(value: &str) -> String {
digest(value)[..12].to_owned()
}
pub fn digest(value: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(value.as_bytes());
hasher
.finalize()
.iter()
.map(|byte| format!("{byte:02x}"))
.collect()
}
pub fn timestamp() -> String {
let description =
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z");
OffsetDateTime::now_utc()
.format(description)
.unwrap_or_else(|_| "1970-01-01T00:00:00.000Z".to_owned())
}
pub fn is_timestamp(value: &str) -> bool {
let shape = "0000-00-00T00:00:00.000Z";
value.len() == shape.len()
&& value
.chars()
.zip(shape.chars())
.all(|(had, want)| match want {
'0' => had.is_ascii_digit(),
other => had == other,
})
}
pub fn instant_of(value: &str) -> Option<SystemTime> {
if !is_timestamp(value) {
return None;
}
let field = |from: usize, to: usize| value.get(from..to)?.parse::<u32>().ok();
let year = i32::try_from(field(0, 4)?).ok()?;
let month = time::Month::try_from(u8::try_from(field(5, 7)?).ok()?).ok()?;
let day = u8::try_from(field(8, 10)?).ok()?;
let date = time::Date::from_calendar_date(year, month, day).ok()?;
let clock = time::Time::from_hms_milli(
u8::try_from(field(11, 13)?).ok()?,
u8::try_from(field(14, 16)?).ok()?,
u8::try_from(field(17, 19)?).ok()?,
u16::try_from(field(20, 23)?).ok()?,
)
.ok()?;
Some(SystemTime::from(
time::PrimitiveDateTime::new(date, clock).assume_utc(),
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_written_timestamp_reads_back_as_the_instant_it_was_written_at() {
let before = SystemTime::now();
let written = timestamp();
let after = SystemTime::now();
let read = instant_of(&written).expect("what this build writes, it reads");
let floor = |instant: SystemTime| {
let since = instant.duration_since(UNIX_EPOCH).expect("after the epoch");
UNIX_EPOCH + std::time::Duration::from_millis(since.as_millis() as u64)
};
assert!(floor(before) <= read && read <= after);
}
#[test]
fn a_stamp_of_the_right_shape_and_no_calendar_date_names_no_instant() {
assert_eq!(instant_of("2026-13-01T00:00:00.000Z"), None);
assert_eq!(instant_of("2026-02-30T00:00:00.000Z"), None);
assert_eq!(instant_of("2026-09-19T24:00:00.000Z"), None);
assert_eq!(instant_of("yesterday"), None);
assert_eq!(
instant_of("1970-01-01T00:00:01.500Z"),
Some(UNIX_EPOCH + std::time::Duration::from_millis(1_500))
);
}
}