use regex::Regex;
use uuid::Uuid;
pub type AgentId = String;
const UUID_REGEX_PATTERN: &str = r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
lazy_static::lazy_static! {
static ref UUID_REGEX: Regex = Regex::new(UUID_REGEX_PATTERN).unwrap();
}
pub fn validate_uuid(maybe_uuid: &dyn ToString) -> Option<String> {
let s = maybe_uuid.to_string();
if UUID_REGEX.is_match(&s) {
Some(s)
} else {
None
}
}
pub fn create_agent_id(label: Option<&str>) -> AgentId {
let uuid = Uuid::new_v4();
let bytes = uuid.as_bytes();
let suffix = hex::encode(&bytes[..8]);
match label {
Some(l) => format!("a{}-{}", l, suffix),
None => format!("a{}", suffix),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_uuid_valid() {
let valid_uuid = "550e8400-e29b-41d4-a716-446655440000";
assert_eq!(validate_uuid(&valid_uuid), Some(valid_uuid.to_string()));
}
#[test]
fn test_validate_uuid_invalid() {
assert_eq!(validate_uuid(&"not-a-uuid"), None);
assert_eq!(validate_uuid(&"123"), None);
assert_eq!(validate_uuid(&""), None);
}
#[test]
fn test_create_agent_id_no_label() {
let id = create_agent_id(None);
assert!(id.starts_with('a'));
assert!(!id.contains('-'));
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-"));
assert!(id.len() > 17);
}
}