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..e235157 --- /dev/null +++ b/fake/src/impls/url/mod.rs @@ -0,0 +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: &str = PATHS.choose(rng).unwrap(); + Url::parse(&format!("{FQDN}{path}")).unwrap() + } +}