[][src]Macro faker_rand::faker_impl_from_templates

macro_rules! faker_impl_from_templates {
    ($name: ident; $($fmt: expr, $($arg:ty),+);+;) => { ... };
}

Create a generator implementation from a set of format strings and sub-generators.

The first argument to the macro must be the name of type to create an implementation for. Said type must be a newtype whose first member must be a String. The first argument must be followed by a semicolon.

After the first argument, the macro accepts a semicolon-separated sequence of template patterns. A template pattern is a comma-separated sequence starting with a std::format-compatible string literal, followed by a sequence of generator type names. The {}s in the string literal will be replaced by sampled data from each of the given generators.

The macro will generate a Distribution and Display implementation for the type.

If multiple template patterns are given, the created implementation will, on each invocation, choose from one of them with equal likelihood. To bias generation in favor of one template pattern over another, you can provide the same template pattern multiple times.

use faker_rand::faker_impl_from_templates;

// First, declare your newtype wrapper around String.
struct Demo(String);

// Then, invoke the macro.
//
// Note well: all commas and semicolons in this example, even trailing
// semicolons, are strictly required.
faker_impl_from_templates! {
    // The type we're creating a generator implementation for.
    Demo;

    // The template patterns.
    "{}.{}", faker_rand::util::AsciiDigit, faker_rand::lorem::Word;
    "{} ~~~ {}", faker_rand::lorem::Word, faker_rand::util::AsciiDigit;
}

use rand::{Rng, SeedableRng};
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0);

assert_eq!("qui ~~~ 5", rng.gen::<Demo>().to_string());
assert_eq!("debitis ~~~ 5", rng.gen::<Demo>().to_string());
assert_eq!("5.aliquid", rng.gen::<Demo>().to_string());
assert_eq!("0.doloribus", rng.gen::<Demo>().to_string());