From 2009f3becebf533dec444e2a8238709b74ea70dd Mon Sep 17 00:00:00 2001 From: Kai Fukushima <38278379+foolishell@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:21:31 +0900 Subject: [PATCH 1/2] feat: add url feature --- README.md | 1 + fake/Cargo.toml | 1 + fake/README.md | 1 + fake/src/impls/mod.rs | 2 ++ fake/src/impls/url/mod.rs | 12 ++++++++++++ 5 files changed, 17 insertions(+) create mode 100644 fake/src/impls/url/mod.rs diff --git a/README.md b/README.md index b649628..801572c 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Available features: - `time` - `zerocopy` - `glam` + - `url` - `always-true-rng`: expose AlwaysTrueRng - `maybe-non-empty-collections`: allow to use AlwaysTrueRng to generate non-empty collections diff --git a/fake/Cargo.toml b/fake/Cargo.toml index 7a88a1f..fabb77e 100644 --- a/fake/Cargo.toml +++ b/fake/Cargo.toml @@ -38,6 +38,7 @@ rand_core = { version = "0.6", optional = true } glam = { version = "0.28.0", optional = true } url-escape = { version = "0.1", optional = true } bson = { version = "2.10", optional = true } +url = { version = "2.5.2", optional = true } [dev-dependencies] chrono = { version = "0.4", features = ["clock"], default-features = false } diff --git a/fake/README.md b/fake/README.md index c09e2bd..57ea794 100644 --- a/fake/README.md +++ b/fake/README.md @@ -31,6 +31,7 @@ Available features: - `time` - `zerocopy` - `glam` + - `url` - `always-true-rng`: expose AlwaysTrueRng - `maybe-non-empty-collections`: allow to use AlwaysTrueRng to generate non-empty collections diff --git a/fake/src/impls/mod.rs b/fake/src/impls/mod.rs index bcf61de..c1041d0 100644 --- a/fake/src/impls/mod.rs +++ b/fake/src/impls/mod.rs @@ -27,6 +27,8 @@ pub mod std; pub mod time; #[cfg(feature = "ulid")] pub mod ulid; +#[cfg(feature = "url")] +pub mod url; #[cfg(feature = "uuid")] pub mod uuid; #[cfg(feature = "zerocopy")] diff --git a/fake/src/impls/url/mod.rs b/fake/src/impls/url/mod.rs new file mode 100644 index 0000000..7d41172 --- /dev/null +++ b/fake/src/impls/url/mod.rs @@ -0,0 +1,12 @@ +use crate::{Dummy, Fake, Faker}; +use rand::Rng; +use url::Url; + +const FQDN: &str = "https://example.com"; + +impl Dummy for Url { + fn dummy_with_rng(_: &Faker, rng: &mut R) -> Self { + let path: String = Faker.fake_with_rng(rng); + Url::parse(&format!("{FQDN}/{path}")).unwrap() + } +} From c736536a9b80d557bd52662aab13e1cea2086ea3 Mon Sep 17 00:00:00 2001 From: Kai Fukushima <38278379+foolishell@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:15:00 +0900 Subject: [PATCH 2/2] fix: add PATHS --- fake/src/impls/url/mod.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/fake/src/impls/url/mod.rs b/fake/src/impls/url/mod.rs index 7d41172..e235157 100644 --- a/fake/src/impls/url/mod.rs +++ b/fake/src/impls/url/mod.rs @@ -1,12 +1,45 @@ use crate::{Dummy, Fake, Faker}; +use rand::seq::SliceRandom; use rand::Rng; use url::Url; const FQDN: &str = "https://example.com"; +const PATHS: [&str; 30] = [ + "/apple", + "/orange", + "/banana", + "/grape", + "/pear", + "/peach/sweet", + "/melon/fresh", + "/kiwi/season", + "/lemon/acidic", + "/lime/citrus", + "/cherry?sweet=true", + "/berry#mixed", + "/mango?seasonal=true", + "/apricot#dried", + "/avocado?ripe=true", + "/papaya#exotic", + "/fig?type=black", + "/date#sweet", + "/olive?type=black", + "/tomato#fresh", + "/onion?type=red", + "/carrot#organic", + "/potato?type=russet", + "/spinach#fresh", + "/lettuce?crispy=true", + "/cabbage#green", + "/cucumber?seedless=true", + "/pepper#bell", + "/garlic?organic=true", + "/ginger#spicy", +]; impl Dummy for Url { fn dummy_with_rng(_: &Faker, rng: &mut R) -> Self { - let path: String = Faker.fake_with_rng(rng); - Url::parse(&format!("{FQDN}/{path}")).unwrap() + let path: &str = PATHS.choose(rng).unwrap(); + Url::parse(&format!("{FQDN}{path}")).unwrap() } }