Skip to main content

fake/impls/uuid/
mod.rs

1//! Fake _UUIDs_ generation.
2//!
3//! This module provides implementations for generating fake UUIDs (Universally Unique Identifiers)
4//! using the [`uuid`] crate.
5//!
6//! Please note that:
7//! * All [`Dummy`] implementations for [`String`] use [to_hyphenated](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.to_hyphenated).
8//! * [`Dummy<Faker>`] implementation uses [from_u128](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.from_u128).
9//!
10//! # Examples
11//!
12//! ```rust
13//! use fake::{Fake, Faker};
14//! use fake::uuid::{UUIDv4, UUIDv7};
15//! # use uuid::Uuid;
16//!
17//! // Use the `Dummy<Faker>` implementation
18//! let uuid: Uuid = Faker.fake();
19//!
20//! // Use the `Dummy<UUIDv4>` implementation to generate a UUID instance
21//! let uuid: Uuid = UUIDv4.fake();
22//!
23//! // Use the `Dummy<UUIDv7>` implementation to generate a UUID string
24//! let uuid: String = UUIDv7.fake();
25//! ```
26
27use uuid::{Builder, Uuid, Variant, Version};
28
29use crate::{Dummy, Fake, Faker};
30
31/// As per [new_v1](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v1)
32pub struct UUIDv1;
33/// As per [new_v3](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v3)
34pub struct UUIDv3;
35/// As per [new_v4](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v4)
36pub struct UUIDv4;
37/// As per [new_v5](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v5)
38pub struct UUIDv5;
39/// As per [new_v6](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v6)
40pub struct UUIDv6;
41/// As per [new_v7](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v7)
42pub struct UUIDv7;
43/// As per [new_v8](https://docs.rs/uuid/latest/uuid/struct.Uuid.html#method.new_v8)
44pub struct UUIDv8;
45
46impl Dummy<UUIDv1> for Uuid {
47    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &UUIDv1, rng: &mut R) -> Self {
48        let ticks = rng.random_range(uuid::timestamp::UUID_TICKS_BETWEEN_EPOCHS..u64::MAX);
49        let counter = Faker.fake_with_rng(rng);
50        let ts = uuid::timestamp::Timestamp::from_gregorian(ticks, counter);
51        let node_id: [u8; 6] = Faker.fake_with_rng(rng);
52        Uuid::new_v1(ts, &node_id)
53    }
54}
55
56impl Dummy<UUIDv1> for String {
57    fn dummy_with_rng<R: rand::RngExt + ?Sized>(config: &UUIDv1, rng: &mut R) -> Self {
58        Uuid::dummy_with_rng(config, rng).hyphenated().to_string()
59    }
60}
61
62impl Dummy<UUIDv3> for Uuid {
63    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &UUIDv3, rng: &mut R) -> Self {
64        Builder::from_bytes(rng.random())
65            .with_variant(Variant::RFC4122)
66            .with_version(Version::Md5)
67            .into_uuid()
68    }
69}
70
71impl Dummy<UUIDv3> for String {
72    fn dummy_with_rng<R: rand::RngExt + ?Sized>(config: &UUIDv3, rng: &mut R) -> Self {
73        Uuid::dummy_with_rng(config, rng).hyphenated().to_string()
74    }
75}
76
77impl Dummy<UUIDv4> for Uuid {
78    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &UUIDv4, rng: &mut R) -> Self {
79        Builder::from_bytes(rng.random())
80            .with_variant(Variant::RFC4122)
81            .with_version(Version::Random)
82            .into_uuid()
83    }
84}
85
86impl Dummy<UUIDv4> for String {
87    fn dummy_with_rng<R: rand::RngExt + ?Sized>(config: &UUIDv4, rng: &mut R) -> Self {
88        Uuid::dummy_with_rng(config, rng).hyphenated().to_string()
89    }
90}
91
92impl Dummy<UUIDv5> for Uuid {
93    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &UUIDv5, rng: &mut R) -> Self {
94        Builder::from_bytes(rng.random())
95            .with_variant(Variant::RFC4122)
96            .with_version(Version::Sha1)
97            .into_uuid()
98    }
99}
100
101impl Dummy<UUIDv5> for String {
102    fn dummy_with_rng<R: rand::RngExt + ?Sized>(config: &UUIDv5, rng: &mut R) -> Self {
103        Uuid::dummy_with_rng(config, rng).hyphenated().to_string()
104    }
105}
106
107impl Dummy<UUIDv6> for Uuid {
108    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &UUIDv6, rng: &mut R) -> Self {
109        let ticks = rng.random_range(uuid::timestamp::UUID_TICKS_BETWEEN_EPOCHS..u64::MAX);
110        let counter = Faker.fake_with_rng(rng);
111        let ts = uuid::timestamp::Timestamp::from_gregorian(ticks, counter);
112        let node_id: [u8; 6] = Faker.fake_with_rng(rng);
113        Uuid::new_v6(ts, &node_id)
114    }
115}
116
117impl Dummy<UUIDv6> for String {
118    fn dummy_with_rng<R: rand::RngExt + ?Sized>(config: &UUIDv6, rng: &mut R) -> Self {
119        Uuid::dummy_with_rng(config, rng).hyphenated().to_string()
120    }
121}
122
123impl Dummy<UUIDv7> for Uuid {
124    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &UUIDv7, rng: &mut R) -> Self {
125        let ticks = rng.random_range(uuid::timestamp::UUID_TICKS_BETWEEN_EPOCHS..u64::MAX);
126        let counter = Faker.fake_with_rng(rng);
127        let ts = uuid::timestamp::Timestamp::from_gregorian(ticks, counter);
128        Uuid::new_v7(ts)
129    }
130}
131
132impl Dummy<UUIDv7> for String {
133    fn dummy_with_rng<R: rand::RngExt + ?Sized>(config: &UUIDv7, rng: &mut R) -> Self {
134        Uuid::dummy_with_rng(config, rng).hyphenated().to_string()
135    }
136}
137
138impl Dummy<UUIDv8> for Uuid {
139    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &UUIDv8, rng: &mut R) -> Self {
140        let buf: [u8; 16] = Faker.fake_with_rng(rng);
141        Uuid::new_v8(buf)
142    }
143}
144
145impl Dummy<UUIDv8> for String {
146    fn dummy_with_rng<R: rand::RngExt + ?Sized>(config: &UUIDv8, rng: &mut R) -> Self {
147        Uuid::dummy_with_rng(config, rng).hyphenated().to_string()
148    }
149}
150
151impl Dummy<Faker> for Uuid {
152    fn dummy_with_rng<R: rand::RngExt + ?Sized>(_: &Faker, rng: &mut R) -> Self {
153        Uuid::from_u128(rng.random())
154    }
155}