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
use std::sync::Mutex;
pub(crate) fn uuid_v4(input: u128) -> String {
let mut out = String::with_capacity(36);
let mut rnd_idx: u8 = 0;
const HEX_CHARS: &[u8; 16] = b"0123456789abcdef";
for str_idx in 0..36 {
if str_idx == 8 || str_idx == 13 || str_idx == 18 || str_idx == 23 {
out.push('-');
} else if str_idx == 14 {
out.push('4');
} else {
let mut dat: u8 = ((input >> (rnd_idx * 4)) & 0x0F) as u8;
if str_idx == 19 {
dat |= 0b00001000;
}
rnd_idx += 1;
out.push(HEX_CHARS[dat as usize] as char);
}
}
out
}
pub struct IdempotencyTokenProvider {
inner: Inner,
}
enum Inner {
Static(&'static str),
Random(Mutex<fastrand::Rng>),
}
pub fn default_provider() -> IdempotencyTokenProvider {
IdempotencyTokenProvider::random()
}
impl From<&'static str> for IdempotencyTokenProvider {
fn from(token: &'static str) -> Self {
Self::fixed(token)
}
}
impl IdempotencyTokenProvider {
pub fn make_idempotency_token(&self) -> String {
match &self.inner {
Inner::Static(token) => token.to_string(),
Inner::Random(rng) => {
let input: u128 = rng.lock().unwrap().u128(..);
uuid_v4(input)
}
}
}
pub fn with_seed(seed: u64) -> Self {
Self {
inner: Inner::Random(Mutex::new(fastrand::Rng::with_seed(seed))),
}
}
pub fn random() -> Self {
Self {
inner: Inner::Random(Mutex::new(fastrand::Rng::new())),
}
}
pub fn fixed(token: &'static str) -> Self {
Self {
inner: Inner::Static(token),
}
}
}
impl Clone for IdempotencyTokenProvider {
fn clone(&self) -> Self {
match &self.inner {
Inner::Static(token) => IdempotencyTokenProvider::fixed(token),
Inner::Random(_) => IdempotencyTokenProvider::random(),
}
}
}