diff --git a/README.md b/README.md index a75feb4..31078d3 100644 --- a/README.md +++ b/README.md @@ -328,3 +328,327 @@ This project is licensed under either of http://opensource.org/licenses/MIT) at your option. +======= +# Fake + +![Rust](https://github.com/cksac/fake-rs/workflows/Rust/badge.svg) +[![Docs Status](https://docs.rs/fake/badge.svg)](https://docs.rs/fake) +[![Latest Version](https://img.shields.io/crates/v/fake.svg)](https://crates.io/crates/fake) + +A Rust library for generating fake data. + +## Installation + +Default: + +```toml +[dependencies] +fake = { version = "2.9.2", features = ["derive"] } +``` + +Available features: + +- `derive`: if you want to use `#[derive(Dummy)]` +- supported crates feature flags: + - `chrono` + - `chrono-tz` + - `http` + - `uuid` + - `bigdecimal` (via `bigdecimal-rs`) + - `rust_decimal` + - `random_color` + - `geo` + - `semver` + - `serde_json` + - `time` + - `zerocopy` + - `glam` +- `always-true-rng`: expose AlwaysTrueRng +- `maybe-non-empty-collections`: allow to use AlwaysTrueRng to generate non-empty collections + +## Usage + +```rust +use fake::{Dummy, Fake, Faker}; +use rand::rngs::StdRng; +use rand::SeedableRng; + +#[derive(Debug, Dummy)] +pub struct Foo { + #[dummy(faker = "1000..2000")] + order_id: usize, + customer: String, + paid: bool, +} + +#[derive(Debug, Dummy)] +struct Bar { + field: Vec, +} + +fn main() { + // type derived Dummy + let f: Foo = Faker.fake(); + println!("{:?}", f); + + let b: Bar = Faker.fake(); + println!("{:?}", b); + + // using `Faker` to generate default fake value of given type + let tuple = Faker.fake::<(u8, u32, f32)>(); + println!("tuple {:?}", tuple); + println!("String {:?}", Faker.fake::()); + + // types U can used to generate fake value T, if `T: Dummy` + println!("String {:?}", (8..20).fake::()); + println!("u32 {:?}", (8..20).fake::()); + + // using `faker` module with locales + use fake::faker::name::raw::*; + use fake::locales::*; + + let name: String = Name(EN).fake(); + println!("name {:?}", name); + + let name: String = Name(ZH_TW).fake(); + println!("name {:?}", name); + + // using convenient function without providing locale + use fake::faker::lorem::en::*; + let words: Vec = Words(3..5).fake(); + println!("words {:?}", words); + + // using macro to generate nested collection + let name_vec = fake::vec![String as Name(EN); 4, 3..5, 2]; + println!("random nested vec {:?}", name_vec); + + // fixed seed rng + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, + ]; + let ref mut r = StdRng::from_seed(seed); + for _ in 0..5 { + let v: usize = Faker.fake_with_rng(r); + println!("value from fixed seed {}", v); + } +} +``` + +# Fakers with locale + +## Lorem + +```rust +Word(); +Words(count: Range); +Sentence(count: Range); +Sentences(count: Range); +Paragraph(count: Range); +Paragraphs(count: Range); +``` + +## Name + +```rust +FirstName(); +LastName(); +Title(); +Suffix(); +Name(); +NameWithTitle(); +``` + +## Number + +```rust +Digit(); +NumberWithFormat<'a>(fmt: &'a str); +``` + +## Boolean + +```rust +Boolean(ratio: u8); +``` + +## Internet + +```rust +FreeEmailProvider(); +DomainSuffix(); +FreeEmail(); +SafeEmail(); +Username(); +Password(len_range: Range); +IPv4(); +IPv6(); +IP(); +MACAddress(); +UserAgent(); +``` + +## HTTP + +```rust +RfcStatusCode(); +ValidStatusCode(); +``` + +## Color + +```rust +HexColor(); +RgbColor(); +RgbaColor(); +HslColor(); +HslaColor(); +Color(); +``` + +## Company + +```rust +CompanySuffix(); +CompanyName(); +Buzzword(); +BuzzwordMiddle(); +BuzzwordTail(); +CatchPhase(); +BsVerb(); +BsAdj(); +BsNoun(); +Bs(); +Profession(); +Industry(); +``` + +## Currency + +```rust +CurrencyCode(); +CurrencyName(); +CurrencySymbol(); +``` + +## Creditcard + +```rust +CreditCardNumber(); +``` + +## Address + +```rust +CityPrefix(); +CitySuffix(); +CityName(); +CountryName(); +CountryCode(); +StreetSuffix(); +StreetName(); +TimeZone(); +StateName(); +StateAbbr(); +SecondaryAddressType(); +SecondaryAddress(); +ZipCode(); +PostCode(); +BuildingNumber(); +Latitude(); +Longitude(); +Geohash(precision: u8); +``` + +## Administrative + +```rust +HealthInsuranceCode(); +``` + +## Automotive + +```rust +LicencePlate(); +``` + +## Barcode + +```rust +Isbn(); +Isbn13(); +Isbn10(); +``` + +## Phone Number + +```rust +PhoneNumber(); +CellNumber(); +``` + +## Date/Time + +```rust +Time(); +Date(); +DateTime(); +Duration(); +DateTimeBefore(dt: DateTime); +DateTimeAfter(dt: DateTime); +DateTimeBetween(start: DateTime, end: DateTime); +``` + +## Filesystem + +```rust +FilePath(); +FileName(); +FileExtension(); +DirPath(); +``` + +### Finance + +```rust +Bic(); +Isin(); +``` + +### UUID + +```rust +UUIDv1(); +UUIDv3(); +UUIDv4(); +UUIDv5(); +``` + +### Decimal + +```rust +Decimal(); +PositiveDecimal(); +NegativeDecimal(); +NoDecimalPoints(); +``` + +### Bigdecimal + +```rust +BigDecimal(); +PositiveBigDecimal(); +NegativeBigDecimal(); +NoBigDecimalPoints(); +``` + +# LICENSE + +This project is licensed under either of + +- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or + http://www.apache.org/licenses/LICENSE-2.0) +- MIT license ([LICENSE-MIT](LICENSE-MIT) or + http://opensource.org/licenses/MIT) + +at your option. diff --git a/fake/src/impls/chrono/mod.rs b/fake/src/impls/chrono/mod.rs index 9c6dbdd..056d82f 100644 --- a/fake/src/impls/chrono/mod.rs +++ b/fake/src/impls/chrono/mod.rs @@ -33,11 +33,11 @@ impl Dummy for Utc { impl Dummy for FixedOffset { fn dummy_with_rng(_: &Faker, rng: &mut R) -> Self { if rng.gen_bool(0.5) { - let halfs: i32 = (0..=28).fake_with_rng(rng); - FixedOffset::east_opt(halfs * 30 * 60).expect("failed to create FixedOffset") + let halves: i32 = (0..=28).fake_with_rng(rng); + FixedOffset::east_opt(halves * 30 * 60).expect("failed to create FixedOffset") } else { - let halfs: i32 = (0..=24).fake_with_rng(rng); - FixedOffset::west_opt(halfs * 30 * 60).expect("failed to create FixedOffset") + let halves: i32 = (0..=24).fake_with_rng(rng); + FixedOffset::west_opt(halves * 30 * 60).expect("failed to create FixedOffset") } } } diff --git a/fake/src/utils.rs b/fake/src/utils.rs index 3735dd0..6035e6f 100644 --- a/fake/src/utils.rs +++ b/fake/src/utils.rs @@ -1,5 +1,5 @@ #[cfg(feature = "always-true-rng")] -mod alway_true_rng { +mod always_true_rng { use rand::{rngs::mock::StepRng, Error, RngCore}; use rand_core::impls; @@ -58,4 +58,4 @@ mod alway_true_rng { } #[cfg(feature = "always-true-rng")] -pub use alway_true_rng::AlwaysTrueRng; +pub use always_true_rng::AlwaysTrueRng;