data_faking/data/defaults/uuids.rs
1use uuid::Uuid;
2use wasm_bindgen::prelude::*;
3
4use crate::{locales::en::person::name::last_name, utils::seeder};
5
6/// as per [new_v1](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v1)
7pub fn uuid_v1() -> Uuid {
8 Uuid::new_v1(
9 new_timestamp(),
10 &[
11 1, 2, 3, 4, 5, 6,
12 ],
13 )
14}
15
16/// as per [new_v3](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v3)
17pub fn uuid_v3() -> Uuid {
18 Uuid::new_v3(
19 &Uuid::NAMESPACE_DNS,
20 last_name().as_bytes(),
21 )
22}
23
24/// as per [new_v4](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v4)
25pub fn uuid_v4() -> Uuid {
26 let built = uuid::Builder::from_random_bytes(seeder::gen::<[u8; 16]>());
27 built.into_uuid()
28}
29
30/// as per [new_v5](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v5)
31pub fn uuid_v5() -> Uuid {
32 Uuid::new_v5(
33 &Uuid::NAMESPACE_DNS,
34 last_name().as_bytes(),
35 )
36}
37
38/// as per [now_v6](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.now_v6)
39pub fn uuid_v6() -> Uuid {
40 let ts = new_timestamp();
41 let node_id = seeder::gen::<[u8; 6]>();
42 Uuid::new_v6(ts, &node_id)
43}
44
45/// as per [now_v7](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.now_v7)
46pub fn uuid_v7() -> Uuid {
47 let (secs, nanos) = crate::data::datetime::unix::unix_ts_gen();
48 let millis = (secs * 1000).saturating_add(nanos as u64 / 1_000_000);
49 let bytes = seeder::gen::<[u8; 10]>();
50 let built = uuid::Builder::from_unix_timestamp_millis(millis, &bytes);
51 built.into_uuid()
52}
53
54/// as per [new_v8](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v8)
55pub fn uuid_v8() -> Uuid {
56 Uuid::new_v8(seeder::gen::<[u8; 16]>())
57}
58
59//
60
61#[wasm_bindgen(js_name = uuid_v1)]
62/// Returns a UUID v1 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
63///
64/// @return
65/// string
66///
67/// @example
68/// ```ignore
69/// faker.uuid_v1();
70/// ```
71pub fn uuid_v1_wasm() -> String {
72 uuid_v1().to_string()
73}
74
75#[wasm_bindgen(js_name = uuid_v3)]
76/// Returns a UUID v3 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
77///
78/// @return
79/// string
80///
81/// @example
82/// ```ignore
83/// faker.uuid_v3();
84/// ```
85pub fn uuid_v3_wasm() -> String {
86 uuid_v3().to_string()
87}
88
89#[wasm_bindgen(js_name = uuid_v4)]
90/// Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
91///
92/// @return
93/// string
94///
95/// @example
96/// ```ignore
97/// faker.uuid_v4();
98/// ```
99pub fn uuid_v4_wasm() -> String {
100 uuid_v4().to_string()
101}
102
103#[wasm_bindgen(js_name = uuid_v5)]
104/// Returns a UUID v5 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
105///
106/// @return
107/// string
108///
109/// @example
110/// ```ignore
111/// faker.uuid_v5();
112/// ```
113pub fn uuid_v5_wasm() -> String {
114 uuid_v5().to_string()
115}
116
117#[wasm_bindgen(js_name = uuid_v6)]
118/// Returns a UUID v6 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
119///
120/// @return
121/// string
122///
123/// @example
124/// ```ignore
125/// faker.uuid_v6();
126/// ```
127pub fn uuid_v6_wasm() -> String {
128 uuid_v6().to_string()
129}
130
131#[wasm_bindgen(js_name = uuid_v7)]
132/// Returns a UUID v7 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
133///
134/// @return
135/// string
136///
137/// @example
138/// ```ignore
139/// faker.uuid_v7();
140/// ```
141pub fn uuid_v7_wasm() -> String {
142 uuid_v7().to_string()
143}
144
145#[wasm_bindgen(js_name = uuid_v8)]
146/// Returns a UUID v8 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
147///
148/// @return
149/// string
150///
151/// @example
152/// ```ignore
153/// faker.uuid_v8();
154/// ```
155pub fn uuid_v8_wasm() -> String {
156 uuid_v8().to_string()
157}
158
159fn new_timestamp() -> uuid::Timestamp {
160 let context = uuid::Context::new(crate::data::defaults::types::u16());
161 let (seconds, nanos) = crate::data::datetime::unix::unix_ts_gen();
162 uuid::Timestamp::from_unix(
163 &context,
164 seconds,
165 nanos
166 )
167}