dsntk_common/idents.rs
1//! # Random UUID generator
2
3use uuid::Uuid;
4
5/// Returns a string representation of a random UUID v4.
6///
7/// # Example
8///
9/// The string should be 36 characters long.
10///
11/// ```
12/// use dsntk_common::gen_id;
13///
14/// assert_eq!(36, gen_id().len());
15/// ```
16/// # References
17///
18/// * [Version 4 UUIDs in RFC4122](https://www.rfc-editor.org/rfc/rfc4122#section-4.4)
19///
20pub fn gen_id() -> String {
21 Uuid::new_v4().to_string()
22}
23
24#[cfg(test)]
25mod tests {
26 use super::*;
27
28 #[test]
29 fn test_valid_references() {
30 let id = gen_id();
31 for (i, ch) in id.chars().enumerate() {
32 if matches!(i, 8 | 13 | 18 | 23) {
33 assert_eq!(ch, '-');
34 } else {
35 assert!(matches!(ch, 'a'..='f' | '0'..='9'))
36 }
37 }
38 }
39}