sh_layer1/utils.rs
1//! Utility functions for ID generation
2//!
3//! Provides standardized short ID generation across all layers.
4
5use uuid::Uuid;
6
7/// Generate a short 8-character ID from UUID v4
8///
9/// This is the standard ID format used across the continuum project.
10/// Format: 8 lowercase hex characters (e.g., "a1b2c3d4")
11///
12/// # Example
13/// ```
14/// use sh_layer1::utils::generate_short_id;
15///
16/// let id = generate_short_id();
17/// assert_eq!(id.len(), 8);
18/// assert!(id.chars().all(|c| c.is_ascii_hexdigit()));
19/// ```
20pub fn generate_short_id() -> String {
21 Uuid::new_v4().to_string()[..8].to_string()
22}
23
24/// Generate a prefixed short ID
25///
26/// # Example
27/// ```
28/// use sh_layer1::utils::generate_prefixed_id;
29///
30/// let task_id = generate_prefixed_id("task");
31/// assert!(task_id.starts_with("task_"));
32/// assert_eq!(task_id.len(), 13); // "task_" + 8 chars
33/// ```
34pub fn generate_prefixed_id(prefix: &str) -> String {
35 format!("{}_{}", prefix, generate_short_id())
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn test_generate_short_id() {
44 let id = generate_short_id();
45 assert_eq!(id.len(), 8);
46 assert!(id.chars().all(|c| c.is_ascii_hexdigit()));
47 }
48
49 #[test]
50 fn test_generate_short_id_uniqueness() {
51 let id1 = generate_short_id();
52 let id2 = generate_short_id();
53 // Very unlikely to be the same
54 assert_ne!(id1, id2);
55 }
56
57 #[test]
58 fn test_generate_prefixed_id() {
59 let id = generate_prefixed_id("task");
60 assert!(id.starts_with("task_"));
61 assert_eq!(id.len(), 13);
62 }
63
64 #[test]
65 fn test_generate_prefixed_id_various() {
66 let tc_id = generate_prefixed_id("tc");
67 assert!(tc_id.starts_with("tc_"));
68
69 let call_id = generate_prefixed_id("call");
70 assert!(call_id.starts_with("call_"));
71 }
72}