1use rand::Rng;
2
3#[derive(Debug, Clone)]
4pub struct Uuid {
5 pub uuid: String,
6}
7
8impl Uuid {
9 pub fn new() -> Self {
10 let mut rng = rand::thread_rng();
11 Self {
12 uuid: format!(
13 "{:04x}{:04x}-{:04x}-{:04x}-{:04x}-{:04x}{:04x}{:04x}",
14 rng.gen::<u16>(),
15 rng.gen::<u16>(),
16 rng.gen::<u16>(),
17 (rng.gen::<u16>() & 0x0fff) | 0x4000,
18 (rng.gen::<u16>() & 0x3fff) | 0x8000,
19 rng.gen::<u16>(),
20 rng.gen::<u16>(),
21 rng.gen::<u16>()
22 ),
23 }
24 }
25}
26
27impl Default for Uuid {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl From<String> for Uuid {
34 fn from(value: String) -> Self {
35 Self { uuid: value }
36 }
37}
38
39impl From<&str> for Uuid {
40 fn from(value: &str) -> Self {
41 Self { uuid: value.into() }
42 }
43}