use regex::Regex;
use std::sync::LazyLock;
use uuid::Uuid;
static UUID_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$").unwrap()
});
pub fn validate_uuid(maybe_uuid: &str) -> Option<Uuid> {
if UUID_REGEX.is_match(maybe_uuid) {
Uuid::parse_str(maybe_uuid).ok()
} else {
None
}
}
pub fn create_agent_id(label: Option<&str>) -> String {
let suffix = Uuid::new_v4().simple().to_string()[..16].to_string();
match label {
Some(l) => format!("a{l}-{suffix}"),
None => format!("a{suffix}"),
}
}
pub fn generate_uuid() -> String {
Uuid::new_v4().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_uuid_valid() {
let valid = "550e8400-e29b-41d4-a716-446655440000";
assert!(validate_uuid(valid).is_some());
}
#[test]
fn test_validate_uuid_invalid() {
assert!(validate_uuid("not-a-uuid").is_none());
assert!(validate_uuid("").is_none());
assert!(validate_uuid("550e8400-e29b-41d4-a716").is_none());
}
#[test]
fn test_create_agent_id_no_label() {
let id = create_agent_id(None);
assert!(id.starts_with('a'));
assert_eq!(id.len(), 17); }
#[test]
fn test_create_agent_id_with_label() {
let id = create_agent_id(Some("compact"));
assert!(id.starts_with("acompact-"));
}
#[test]
fn test_generate_uuid() {
let uuid1 = generate_uuid();
let uuid2 = generate_uuid();
assert_ne!(uuid1, uuid2);
assert!(validate_uuid(&uuid1).is_some());
}
}