From 3ef8f4fdc3547fc358111b6a43f08422211081cb Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Sun, 21 Jan 2024 16:16:31 +0800 Subject: [PATCH] Add ulid support --- README.md | 1 + fake/Cargo.toml | 1 + fake/src/impls/mod.rs | 2 ++ fake/src/impls/ulid/mod.rs | 13 +++++++++++++ fake/src/lib.rs | 4 ++++ fake/tests/determinism.rs | 8 ++++++++ 6 files changed, 29 insertions(+) create mode 100644 fake/src/impls/ulid/mod.rs diff --git a/README.md b/README.md index fe8da67..39ef0e9 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Available features: - `chrono` - `chrono-tz` - `http` + - `ulid` - `uuid` - `bigdecimal` (via `bigdecimal-rs`) - `rust_decimal` diff --git a/fake/Cargo.toml b/fake/Cargo.toml index 2c04205..eba41c8 100644 --- a/fake/Cargo.toml +++ b/fake/Cargo.toml @@ -27,6 +27,7 @@ geo-types = { version = "0.7", default-features = false, optional = true } http = { version = "1", optional = true } semver = { version = "1", optional = true } serde_json = { version = "1.0", optional = true } +ulid = { version = "1.1", optional = true } uuid = { version = "1.5", features = ["v1", "v3", "v4", "v5"], optional = true } time = { version = "0.3", features = ["formatting"], optional = true } num-traits = { version = "0.2", optional = true } diff --git a/fake/src/impls/mod.rs b/fake/src/impls/mod.rs index 77e2ea0..fb4a5bc 100644 --- a/fake/src/impls/mod.rs +++ b/fake/src/impls/mod.rs @@ -23,6 +23,8 @@ pub mod serde_json; pub mod std; #[cfg(feature = "time")] pub mod time; +#[cfg(feature = "ulid")] +pub mod ulid; #[cfg(feature = "uuid")] pub mod uuid; #[cfg(feature = "zerocopy")] diff --git a/fake/src/impls/ulid/mod.rs b/fake/src/impls/ulid/mod.rs new file mode 100644 index 0000000..a5d2e75 --- /dev/null +++ b/fake/src/impls/ulid/mod.rs @@ -0,0 +1,13 @@ +//! ulid + +use ulid::Ulid; + +use crate::{Dummy, Faker}; + +impl Dummy for Ulid { + fn dummy_with_rng(_: &Faker, rng: &mut R) -> Self { + let time_part: u64 = rng.gen_range(0..(1 << Ulid::TIME_BITS)); + let rand_part: u128 = rng.gen_range(0..(1 << Ulid::RAND_BITS)); + Ulid::from_parts(time_part, rand_part) + } +} diff --git a/fake/src/lib.rs b/fake/src/lib.rs index b3afd10..33a05ea 100644 --- a/fake/src/lib.rs +++ b/fake/src/lib.rs @@ -5,6 +5,7 @@ //! - `derive` provides `#[derive(Dummy)]` //! - `chrono` [chrono](https://docs.rs/chrono) integration //! - `http` [http](https://docs.rs/http) integration +//! - `ulid` [ulid](https://docs.rs/ulid) integration //! - `uuid` [uuid](https://docs.rs/uuid) integration //! //! # Usage @@ -251,6 +252,9 @@ pub use impls::std::string::StringFaker; #[cfg(feature = "geo")] pub use impls::geo; +#[cfg(feature = "ulid")] +pub use impls::ulid; + #[cfg(feature = "uuid")] pub use impls::uuid; diff --git a/fake/tests/determinism.rs b/fake/tests/determinism.rs index 14be5f1..9e7d78f 100644 --- a/fake/tests/determinism.rs +++ b/fake/tests/determinism.rs @@ -216,6 +216,14 @@ check_determinism! { l10d SafeEmail; String, fake_safeemail_en, fake_safeemail_f check_determinism! { l10d UserAgent; String, fake_useragent_en, fake_useragent_fr, fake_useragent_cn, fake_useragent_tw, fake_useragent_jp, fake_useragent_br } check_determinism! { l10d Username; String, fake_username_en, fake_username_fr, fake_username_cn, fake_username_tw, fake_username_jp, fake_username_br } +// it's sufficient to check one language, because it doesn't change anything +#[cfg(feature = "ulid")] +mod ulid { + use fake::{Fake, Faker}; + use rand::SeedableRng as _; + check_determinism! { one fake_ulid, ulid::Ulid, Faker } +} + // it's sufficient to check one language, because it doesn't change anything #[cfg(feature = "uuid")] mod uuid {