Skip to content

Commit

Permalink
update either faker
Browse files Browse the repository at this point in the history
  • Loading branch information
cksac committed Mar 13, 2024
1 parent e17ffb9 commit 5353ccc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,19 @@ NegativeBigDecimal();
NoBigDecimalPoints();
```

## Utils
### Either
```rust
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
Expand Down
7 changes: 7 additions & 0 deletions dummy_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct DummyField {
default: bool,
#[darling(default)]
from: Option<String>,
#[darling(default)]
wrapper: Option<String>,
}

#[derive(Debug, FromDeriveInput)]
Expand Down Expand Up @@ -222,6 +224,11 @@ fn expose_field(f: &DummyField) -> proc_macro2::TokenStream {
quote! {
std::convert::Into::<#field_ty>::into(::fake::Fake::fake_with_rng::<#from_ty, _>(&(#faker), rng))
}
} else if let Some(ref wrapper) = f.wrapper {
let wrapper_ty = syn::parse_str::<syn::Type>(wrapper).unwrap();
quote! {
::fake::utils::IntoInner::into_inner(::fake::Fake::fake_with_rng::<#wrapper_ty<#field_ty>, _>(&(#faker), rng))
}
} else {
quote! {
::fake::Fake::fake_with_rng::<#field_ty, _>(&(#faker), rng)
Expand Down
11 changes: 11 additions & 0 deletions fake/examples/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use fake::decimal::*;
use fake::faker::boolean::en::*;
use fake::faker::company::en::*;
use fake::faker::name::en::*;
use fake::utils::{either, WrappedVal};
use fake::uuid::UUIDv4;
use fake::Dummy;
use fake::{Fake, Faker};
Expand Down Expand Up @@ -104,6 +105,13 @@ struct MyStruct<T> {
field: Vec<T>,
}

#[allow(dead_code)]
#[derive(Debug, Dummy)]
struct FakerWrapperStruct {
#[dummy(faker = "either(Buzzword(), CompanyName())", wrapper = "WrappedVal")]
pub val: String,
}

fn main() {
let order: Order = Faker.fake();
println!("{:#?}", order);
Expand Down Expand Up @@ -134,4 +142,7 @@ fn main() {

let v: MyStruct<u32> = Faker.fake();
println!("{:#?}", v);

let v: FakerWrapperStruct = Faker.fake();
println!("{:#?}", v);
}
7 changes: 5 additions & 2 deletions fake/examples/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use fake::faker::phone_number::en::{CellNumber, PhoneNumber};
use fake::{utils::either, Dummy, Fake, Faker};
use fake::{
utils::{either, WrappedVal},
Dummy, Fake, Faker,
};

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

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

View workflow job for this annotation

GitHub Actions / Clippy

field `phone_number` is never read
}

Expand Down
35 changes: 27 additions & 8 deletions fake/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
use crate::{Dummy, Fake, Faker};
use rand::Rng;

pub struct Either<A, B> {
pub trait IntoInner {
type Target;
fn into_inner(self) -> Self::Target;
}

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

impl<A, B> Dummy<Either<A, B>> for String
pub struct WrappedVal<T>(pub T);
impl<T> WrappedVal<T> {
pub fn new(val: T) -> Self {
Self(val)
}
}

impl<T> IntoInner for WrappedVal<T> {
type Target = T;
fn into_inner(self) -> Self::Target {
self.0
}
}

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

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

#[cfg(feature = "always-true-rng")]
Expand Down

0 comments on commit 5353ccc

Please sign in to comment.