GithubHelp home page GithubHelp logo

fake-rs's Introduction

Fake

Rust Docs Status Latest Version

A Rust library for generating fake data.

Installation

Default:

[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
    • ulid
    • 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

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<T> {
    field: Vec<T>,
}

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

    let b: Bar<Foo> = 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::<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<'a>(fmt: &'a str);

Boolean

Boolean(ratio: u8);

Internet

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

HTTP

RfcStatusCode();
ValidStatusCode();

Color

HexColor();
RgbColor();
RgbaColor();
HslColor();
HslaColor();
Color();

Company

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

Currency

CurrencyCode();
CurrencyName();
CurrencySymbol();

Creditcard

CreditCardNumber();

Address

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

Administrative

HealthInsuranceCode();

Automotive

LicencePlate();

Barcode

Isbn();
Isbn13();
Isbn10();

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();

Finance

Bic();
Isin();

UUID

UUIDv1();
UUIDv3();
UUIDv4();
UUIDv5();

Decimal

Decimal();
PositiveDecimal();
NegativeDecimal();
NoDecimalPoints();

Bigdecimal

BigDecimal();
PositiveBigDecimal();
NegativeBigDecimal();
NoBigDecimalPoints();

Utils

Either

use fake::faker::phone_number::en::{CellNumber, PhoneNumber};
use fake::{utils::{either, WrappedVal}, Dummy, Fake, Faker};

#[derive(Debug, Dummy)]
struct Foo {
    #[dummy(faker = "either(PhoneNumber(), CellNumber())", wrapper = "WrappedVal")]
    phone_number: String,
}

LICENSE

This project is licensed under either of

at your option.

fake-rs's People

Contributors

cksac avatar roms1383 avatar mothsart avatar jayvdb avatar saiintbrisson avatar xd009642 avatar llogiq avatar birkenfeld avatar kyle-mccarthy avatar urkle avatar ajf-sa avatar andrewtoth avatar dejiah avatar frederikhaaning avatar pickfire avatar yagince avatar theanht1 avatar antono avatar simpsoneric avatar joelmon avatar kyrias avatar kornelski avatar mekagoza avatar killercup avatar svenstaro avatar catflyflyfly avatar daamien avatar robatipoor avatar whiteccinn avatar bryanmorgan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.