pub use uuid::Uuid;
#[must_use]
pub fn new_v7() -> Uuid {
Uuid::now_v7()
}
pub const PROVENANCE_NAMESPACE: Uuid = Uuid::from_bytes([
0x9f, 0x6c, 0x2a, 0x1e, 0x7b, 0x42, 0x4d, 0x88, 0xa3, 0x10, 0x5e, 0x0c, 0x91, 0x33, 0x77, 0xd2,
]);
#[must_use]
pub fn new_v5(namespace: &Uuid, name: &[u8]) -> Uuid {
Uuid::new_v5(namespace, name)
}
#[must_use]
pub fn to_bytes(uuid: &Uuid) -> [u8; 16] {
*uuid.as_bytes()
}
#[must_use]
pub fn from_bytes(bytes: &[u8; 16]) -> Uuid {
Uuid::from_bytes(*bytes)
}
#[must_use]
pub fn to_string(uuid: &Uuid) -> String {
uuid.hyphenated().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn uuids_are_unique() {
let mut seen = std::collections::HashSet::new();
for _ in 0..1000 {
assert!(seen.insert(new_v7()), "generated a duplicate UUID");
}
assert_eq!(seen.len(), 1000);
}
#[test]
fn uuids_are_time_monotone() {
let mut prev = new_v7();
for _ in 0..1000 {
let next = new_v7();
assert!(next > prev, "UUIDv7 ordering violated: {next} !> {prev}");
prev = next;
}
}
#[test]
fn byte_round_trip_preserves_value() {
let original = new_v7();
let bytes = to_bytes(&original);
let restored = from_bytes(&bytes);
assert_eq!(original, restored);
}
#[test]
fn string_matches_rfc_format() {
let s = to_string(&new_v7());
assert_eq!(s.len(), 36);
let groups: Vec<&str> = s.split('-').collect();
assert_eq!(groups.len(), 5);
assert_eq!(
groups.iter().map(|g| g.len()).collect::<Vec<_>>(),
vec![8, 4, 4, 4, 12]
);
assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
assert_eq!(groups[2].chars().next(), Some('7'));
}
}