1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#![cfg(feature = "mac")]
use mac_address as MAC;
use rand;
use crate::{ClockSeq, Domain, Layout, Node, Timestamp, Variant, Version, UUID};
impl UUID {
pub fn new_v1() -> Option<Layout> {
Self::from_mac(Version::TIME, Self::mac())
}
pub fn new_v2(d: Domain) -> Layout {
let utc = Timestamp::new();
Layout {
field_low: (utc & 0xffff_ffff) as u32,
field_mid: ((utc >> 32 & 0xffff) as u16),
field_high_and_version: (utc >> 48 & 0xfff) as u16 | (Version::DCE as u16) << 12,
clock_seq_high_and_reserved: Self::clock_seq_high_and_reserved(Variant::RFC as u8).0,
clock_seq_low: d as u8,
node: Self::mac(),
}
}
pub fn from_utc(v: Version, utc: u64) -> Option<Layout> {
match v {
Version::TIME | Version::DCE => {
let clock_seq = Self::clock_seq_high_and_reserved(Variant::RFC as u8);
Some(Layout {
field_low: ((utc & 0xffff_ffff) as u32),
field_mid: ((utc >> 32 & 0xffff) as u16),
field_high_and_version: (utc >> 48 & 0xfff) as u16 | (v as u16) << 12,
clock_seq_high_and_reserved: clock_seq.0,
clock_seq_low: clock_seq.1,
node: Self::mac(),
})
}
_ => None,
}
}
pub fn from_mac(v: Version, mac: Node) -> Option<Layout> {
match v {
Version::TIME | Version::DCE => {
let utc = Timestamp::new();
let clock_seq = Self::clock_seq_high_and_reserved(Variant::RFC as u8);
Some(Layout {
field_low: ((utc & 0xffff_ffff) as u32),
field_mid: ((utc >> 32 & 0xffff) as u16),
field_high_and_version: (utc >> 48 & 0xfff) as u16 | (v as u16) << 12,
clock_seq_high_and_reserved: clock_seq.0,
clock_seq_low: clock_seq.1,
node: mac,
})
}
_ => None,
}
}
fn clock_seq_high_and_reserved(s: u8) -> (u8, u8) {
let clock_seq = ClockSeq::new(rand::random::<u16>());
(
((clock_seq >> 8) & 0xf) as u8 | s << 4,
(clock_seq & 0xff) as u8,
)
}
fn mac() -> Node {
Node(MAC::get_mac_address().unwrap().unwrap().bytes())
}
}
#[macro_export]
macro_rules! v1 {
() => {
format!("{:x}", $crate::UUID::new_v1().unwrap().as_bytes())
};
}
#[macro_export]
macro_rules! v2 {
($domain:expr) => {
format!("{:x}", $crate::UUID::new_v2($domain).as_bytes())
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_v1() {
let uuid = UUID::new_v1();
assert_eq!(uuid.unwrap().get_version(), Some(Version::TIME));
let uuid = UUID::new_v1();
assert_eq!(uuid.unwrap().get_variant(), Some(Variant::RFC));
}
#[test]
fn new_v2() {
let domains = [Domain::PERSON, Domain::GROUP, Domain::ORG];
for d in domains.iter() {
assert_eq!(UUID::new_v2(*d).get_version(), Some(Version::DCE));
assert_eq!(UUID::new_v2(*d).get_variant(), Some(Variant::RFC));
}
}
#[test]
fn from_mac() {
let uuid = UUID::from_mac(Version::TIME, Node([0x03, 0x2a, 0x35, 0x0d, 0x13, 0x80]));
assert_eq!(uuid.unwrap().get_version(), Some(Version::TIME));
let uuid = UUID::from_mac(Version::TIME, Node([0x03, 0x2a, 0x35, 0x0d, 0x13, 0x80]));
assert_eq!(
uuid.unwrap().get_mac().0,
[0x03, 0x2a, 0x35, 0x0d, 0x13, 0x80]
);
}
#[test]
fn from_utc() {
let uuid = UUID::from_utc(Version::TIME, 0x1234_u64);
assert_eq!(uuid.unwrap().get_version(), Some(Version::TIME));
let uuid = UUID::from_utc(Version::TIME, 0x1234_u64);
assert_eq!(uuid.unwrap().get_time(), 0x1234_u64);
}
#[should_panic]
#[test]
fn from_utc_panic() {
let uuid = UUID::from_utc(Version::RAND, 0x1234_u64);
assert_eq!(uuid.unwrap().get_version(), Some(Version::RAND))
}
}