Skip to content

Commit

Permalink
- add either util for String target
Browse files Browse the repository at this point in the history
- add local mods
  • Loading branch information
cksac committed Mar 1, 2024
1 parent b2cb752 commit e17ffb9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fake/examples/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use fake::faker::phone_number::en::{CellNumber, PhoneNumber};
use fake::{utils::either, Dummy, Fake, Faker};

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

Check failure on line 7 in fake/examples/utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

field `phone_number` is never read
}

fn main() {
for _ in 0..10 {
let foo: Foo = Faker.fake();

Check failure on line 12 in fake/examples/utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

use of a disallowed/placeholder name `foo`
println!("{:?}", foo);
}
}
3 changes: 3 additions & 0 deletions fake/src/faker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ macro_rules! def_fakers {
def_fakers!(@m fr_fr=>FR_FR {$($name$(< $($lts),* >)?($($arg:$typ),*);)+});
def_fakers!(@m zh_tw=>ZH_TW {$($name$(< $($lts),* >)?($($arg:$typ),*);)+});
def_fakers!(@m zh_cn=>ZH_CN {$($name$(< $($lts),* >)?($($arg:$typ),*);)+});
def_fakers!(@m ar_sa=>AR_SA {$($name$(< $($lts),* >)?($($arg:$typ),*);)+});
def_fakers!(@m ja_jp=>JA_JP {$($name$(< $($lts),* >)?($($arg:$typ),*);)+});
def_fakers!(@m pt_br=>PT_BR {$($name$(< $($lts),* >)?($($arg:$typ),*);)+});
};
}

Expand Down
25 changes: 25 additions & 0 deletions fake/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
use crate::{Dummy, Fake, Faker};
use rand::Rng;

pub struct Either<A, B> {
pub a: A,
pub b: B,
}

impl<A, B> Dummy<Either<A, B>> for String
where
String: Dummy<A> + Dummy<B>,
{
fn dummy_with_rng<R: Rng + ?Sized>(config: &Either<A, B>, rng: &mut R) -> Self {
if Faker.fake_with_rng(rng) {
config.a.fake_with_rng(rng)
} else {
config.b.fake_with_rng(rng)
}
}
}

pub fn either<A, B>(a: A, b: B) -> Either<A, B> {
Either { a, b }
}

#[cfg(feature = "always-true-rng")]
mod always_true_rng {
use rand::{rngs::mock::StepRng, Error, RngCore};
Expand Down

0 comments on commit e17ffb9

Please sign in to comment.