#[derive(Dummy)]
{
// Attributes available to this derive:
#[dummy]
}
Available on crate feature
derive
only.Expand description
Derive macro generating an impl of the trait Dummy
. This works for both structs and enums.
§Attributes
For any fields in the type there are a number of keys that can be used to control the code generation. All of these go within the dummy attribute.
faker
key can be used to provide a specific faker for a field provided it implementsFake
.expr
key can be used to provide a rust expression as a fixed value.default
key sets the value to the typesDefault
implementation.
§Examples
A simple example for deriving Dummy
on a struct:
use fake::{Dummy, Fake, Faker};
use fake::faker::name::en::Name;
#[derive(Dummy)]
pub struct Foo {
#[dummy(faker = "1000..2000")]
order_id: usize,
#[dummy(faker = "Name()")]
customer: String,
paid: bool,
#[dummy(expr = "\"Fixed\".into()")]
fixed_value: String,
#[dummy(default)]
other: String,
}
let f: Foo = Faker.fake();
This would generate code roughly equivalent to:
use fake::{Dummy, Fake, Faker, Rng};
use fake::faker::name::en::Name;
pub struct Foo {
order_id: usize,
customer: String,
paid: bool,
fixed_value: String,
other: String,
}
impl Dummy<Faker> for Foo {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
let order_id = Fake::fake_with_rng::<usize, _>(&(1000..2000), rng);
let customer = Fake::fake_with_rng::<String, _>(&(Name()), rng);
let paid = Fake::fake_with_rng::<bool, _>(&Faker, rng);
let fixed_value = "Fixed".into();
let other = Default::default();
Self {
order_id,
customer,
paid,
fixed_value,
other,
}
}
}
let f: Foo = Faker.fake();
A simple example for deriving Dummy
on an enum. For enum tuple variants the faker attribute
is applied directly to the types in the tuple, for struct variants it is applied on each struct
field.
use fake::{Dummy, Fake, Faker};
use fake::faker::name::en::Name;
#[derive(Dummy)]
pub enum Bar {
Simple,
Tuple(#[dummy(faker="0..5")] i32),
Structure {
#[dummy(faker = "1000..2000")]
i: usize,
j: String,
}
}
let b: Bar = Faker.fake();
This will generate code roughly equivalent to:
use fake::{Dummy, Fake, Faker, Rng};
use fake::faker::name::en::Name;
pub enum Bar {
Simple,
Tuple(i32),
Structure {
i: usize,
j: String,
}
}
impl Dummy<Faker> for Bar {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
match rng.random_range(0..3usize) {
0 => Self::Simple,
1 => Self::Tuple(Fake::fake_with_rng::<i32, _>(&(0..5), rng)),
2 => {
let i = Fake::fake_with_rng::<usize, _>(&(1000..2000), rng);
let j = Fake::fake_with_rng::<String, _>(&Faker, rng);
Self::Structure { i, j }
},
_ => unreachable!(),
}
}
}
let b: Bar = Faker.fake();