Rust-Fake rs: fake-rs — A library for generating fake data

Fake

Rust Docs Status Latest Version

A Rust library for generating fake data.

Installation

Default:

[dependencies]
fake = "2.2"

If you want to use #[derive(Dummy)]:

fake = { version = "2.2", features=['derive']}

If you want the date and time features from chrono:

fake = { version = "2.2", features=['chrono']}

If you want http faker features:

fake = { version = "2.2", features=['http']}

Usage

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,
}

fn main() {
    // type derived Dummy
    let f: Foo = Faker.fake();
    println!("{:?}", f);

    // using `Faker` to generate default fake value of given type
    let tuple = Faker.fake::<(u8, u32, f32)>();
    println!("tuple {:?}", tuple);
    println!("String {:?}", Faker.fake::<String>());

    // types U can used to generate fake value T, if `T: Dummy<U>`
    println!("String {:?}", (8..20).fake::<String>());
    println!("u32 {:?}", (8..20).fake::<u32>());

    // 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<String> = 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

Word();
Words(count: Range<usize>);
Sentence(count: Range<usize>);
Sentences(count: Range<usize>);
Paragraph(count: Range<usize>);
Paragraphs(count: Range<usize>);

Name

FirstName();
LastName();
Title();
Suffix();
Name();
NameWithTitle();

Number

Digit();
NumberWithFormat(fmt: &'static str);

Boolean

Boolean(ratio: u8);

Internet

FreeEmailProvider();
DomainSuffix();
FreeEmail();
SafeEmail();
Username();
Password(len_range: Range<usize>);
IPv4();
IPv6();
IP();
MACAddress();
Color();
UserAgent();

HTTP

RfcStatusCode();
ValidStatusCode();

Company

CompanySuffix();
CompanyName();
Buzzword();
BuzzwordMiddle();
BuzzwordTail();
CatchPhase();
BsVerb();
BsAdj();
BsNoun();
Bs();
Profession();
Industry();

Address

CityPrefix();
CitySuffix();
CityName();
CountryName();
CountryCode();
StreetSuffix();
StreetName();
TimeZone();
StateName();
StateAbbr();
SecondaryAddressType();
SecondaryAddress();
ZipCode();
PostCode();
BuildingNumber();
Latitude();
Longitude();

Phone Number

PhoneNumber();
CellNumber();

Date/Time

Time();
Date();
DateTime();
Duration();
DateTimeBefore(dt: DateTime<Utc>);
DateTimeAfter(dt: DateTime<Utc>);
DateTimeBetween(start: DateTime<Utc>, end: DateTime<Utc>);

Filesystem

FilePath();
FileName();
FileExtension();
DirPath();

LICENSE

This project is licensed under either of

at your option.

Comments

  • Feature : Add an image faker
    Feature : Add an image faker

    Sep 12, 2019

    Hello, What about adding an image faker? We could use this kind of API : http://lorempixel.com/

    Let me know if your interested and i will contribute.

    Reply
  • Image URL
    Image URL

    Oct 4, 2019

    Feature: Add Image faker (it's not https://www.lorempixel.com but related to #25).

    Reply
  • Ideas around address consistency, email validity, etc.
    Ideas around address consistency, email validity, etc.

    Oct 5, 2019

    Also, you might notice a few more ideas about data one can generate at https://www.fakeaddressgenerator.com/ and https://www.fakepersongenerator.com/ i.e. stuff like IMEI.

    You might consider an interface that makes generating "valid" address information possible. Afaik there are no existing fake address generators online that actually do this. It'd normally be undesirable, but who knows.

    There are a couple free temporary email address providers like https://10minutemail.com/ In the past, I think they allowed offline generation of the valid email address, assuming you knew one of their currently active domains, but this appears gone now, and maybe with good reason.

    Reply
  • Crates.io link to source code is borked
    Crates.io link to source code is borked

    Nov 13, 2019

    Refers to https://github.com/cksac/fake-rs/fake but should instead be https://github.com/cksac/fake-rs

    Reply
  • Add recipe related fake data
    Add recipe related fake data

    Feb 22, 2020

    I'm currently needing fake data for recipe and ingredients, including separated lists for vegetables, fruits, nuts etc.

    I'd imagine something like this:

    enum Category {
      Fruit,
      Vegetable,
      Nut,
      // etc...
    }
    
    Ingredient(category: Option<Category>);
    Ingredients(count: usize, category: Option<Category>);
    Recipe();
    Recipes(count: usize);
    

    As of now, I do not have recipe steps implemented but might add them later on. One thing that might actually be better would be moving the Ingredient call to a separate call like Fruit(), Vegetable(), Nut() and have Recipe re-use them. That way, they are not coupled to recipes and should make it clear that they can be used by their own.

    If there is interest I can open a PR so you can merge my additions back into master.

    Reply
  • Added Internet.{ip,ipv4,ipv6}
    Added Internet.{ip,ipv4,ipv6}

    Mar 22, 2018

    Very simple implementation for ipv6

    Reply
  • take_one - generic over `T: ?Sized`
    take_one - generic over `T: ?Sized`

    May 28, 2019

    Maybe it's also a good idea to Re-export rand, if somebody wants to use thread_rng

    Reply
  • fake data generation seed
    fake data generation seed

    Mar 19, 2020

    is there a way to provide the generator with a seed, so that, given the same seed, I will always get the same fake data back?

    I'm trying to mock an external web server, and I would like to return fake data based on a provided id.

    Many thanks for the great lib!

    Reply
  • Return original Chrono values instead of `String`s
    Return original Chrono values instead of `String`s

    Jul 6, 2019

    A project of mine stores DateTime<Utc> in some struct and would like to generate dummy timestamps for testing purposes. However this crate only returns Strings, and I had to convert them back to DateTime<Utc>, which would be a bit slow and even unreliable.

    Reply
  • Implement (optional) datetime feature using chrono
    Implement (optional) datetime feature using chrono

    Jun 18, 2018

    This should implement issue #11 . This is implemented using an optional Cargo feature, so it's disabled by default to minimize unnecessary dependencies and compile time. That can be changed.

    @svenstaro , can you take a look over this and let me know if this is what you had in mind? The tests in src/lib.rs are instructive, and the implementation is all in src/faker/time.rs

    Reply
  • Custom derive #[derive(Dummy)]
    Custom derive #[derive(Dummy)]

    May 13, 2017

    implement procedural macro for #[derive(Dummy)]

    enhancement help wanted 
    Reply