1use uuid::Uuid;
4use std::time::{SystemTime, UNIX_EPOCH};
5
6pub struct Sid;
10
11impl Sid {
12 pub fn uuid() -> String { Uuid::new_v4().simple().to_string() }
14
15 pub fn short() -> String {
17 let u = Uuid::new_v4();
18 u.simple().to_string()[..16].to_string()
19 }
20
21 pub fn tiny() -> String {
23 let u = Uuid::new_v4();
24 u.simple().to_string()[..8].to_string()
25 }
26
27 pub fn tsid() -> String {
29 let ts = SystemTime::now()
30 .duration_since(UNIX_EPOCH)
31 .unwrap()
32 .as_millis();
33 let rand: u32 = rand::random();
34 format!("{:013x}{:07x}", ts, rand & 0x0FFFFFFF)
35 }
36
37 pub fn uuid7() -> String {
39 Uuid::now_v7().simple().to_string()
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46 #[test]
47 fn test_short() { assert_eq!(Sid::short().len(), 16); }
48 #[test]
49 fn test_tiny() { assert_eq!(Sid::tiny().len(), 8); }
50 #[test]
51 fn test_uuid() { assert_eq!(Sid::uuid().len(), 32); }
52}