Skip to content

Commit b12c13d

Browse files
authored
Adds a new uuid module (#229)
1 parent 787dffe commit b12c13d

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

Cargo.lock

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ fast_poisson = { version = "1.0.2", optional = true, features = [
6666
] }
6767
symphonia = { version = "0.5.4", optional = true, features = ["all-codecs"] }
6868
caith = { version = "4.2.4", optional = true }
69+
uuid = { version = "1.17", optional = true, features = ["v4", "v7", "fast-rng"] }
70+
cuid2 = { version = "0.1.4", optional = true }
6971

7072
[features]
7173
default = [
@@ -112,6 +114,7 @@ all = [
112114
"time",
113115
"toml",
114116
"url",
117+
"uuid",
115118
"pathfinder",
116119
"poissonnoise",
117120
"redis_pubsub",
@@ -161,6 +164,7 @@ sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
161164
time = ["chrono"]
162165
toml = ["serde", "serde_json", "toml-dep"]
163166
url = ["url-dep", "percent-encoding"]
167+
uuid = ["dep:uuid", "cuid2"]
164168

165169
# additional features
166170
dice = ["caith"]

dmsrc/uuid.dm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Generates a version 4 UUID.
2+
/// See https://www.ietf.org/rfc/rfc9562.html#section-5.4 for specifics on version 4 UUIDs.
3+
#define rustg_generate_uuid_v4(...) RUSTG_CALL(RUST_G, "uuid_v4")()
4+
5+
/// Generates a version 7 UUID, with the current time.
6+
/// See https://www.ietf.org/rfc/rfc9562.html#section-5.7 for specifics on version 7 UUIDs.
7+
#define rustg_generate_uuid_v7(...) RUSTG_CALL(RUST_G, "uuid_v7")()
8+
9+
/// Generates a random version 2 CUID.
10+
/// See https://github.com/paralleldrive/cuid2 for specifics on version 2 CUIDs.
11+
#define rustg_generate_cuid2(...) RUSTG_CALL(RUST_G, "cuid2")()
12+
13+
/// Generates a random version 2 CUID with the given length.
14+
/// See https://github.com/paralleldrive/cuid2 for specifics on version 2 CUIDs.
15+
#define rustg_generate_cuid2_length(length) RUSTG_CALL(RUST_G, "cuid2_len")("[length]")

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub mod toml;
6060
pub mod unzip;
6161
#[cfg(feature = "url")]
6262
pub mod url;
63+
#[cfg(feature = "uuid")]
64+
pub mod uuid;
6365
#[cfg(feature = "worleynoise")]
6466
pub mod worleynoise;
6567

src/uuid.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use uuid::Uuid;
2+
3+
byond_fn!(
4+
fn uuid_v4() {
5+
Some(Uuid::new_v4().to_string())
6+
}
7+
);
8+
9+
byond_fn!(
10+
fn uuid_v7() {
11+
Some(Uuid::now_v7().to_string())
12+
}
13+
);
14+
15+
byond_fn!(
16+
fn cuid2() {
17+
Some(cuid2::create_id())
18+
}
19+
);
20+
21+
byond_fn!(
22+
fn cuid2_len(length) {
23+
let length = length.parse::<u16>().ok()?;
24+
Some(
25+
cuid2::CuidConstructor::new()
26+
.with_length(length)
27+
.create_id()
28+
)
29+
}
30+
);

0 commit comments

Comments
 (0)