Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vec of Barcodes please #166

Closed
matiu2 opened this issue Jan 22, 2024 · 2 comments
Closed

Vec of Barcodes please #166

matiu2 opened this issue Jan 22, 2024 · 2 comments

Comments

@matiu2
Copy link

matiu2 commented Jan 22, 2024

I'm getting this issue:

error[E0277]: the trait bound `Vec<std::string::String>: Dummy<Vec<Isbn<EN>>>` is not satisfied
   --> src/definitions/order_create.rs:322:25
    |
322 | #[cfg_attr(test, derive(fake::Dummy))]
    |                         ^^^^^^^^^^^ the trait `Dummy<Vec<Isbn<EN>>>` is not implemented for `Vec<std::string::String>`
    |
    = help: the following other types implement trait `Dummy<T>`:
              <Vec<std::string::String> as Dummy<Words<L>>>
              <Vec<std::string::String> as Dummy<Sentences<L>>>
              <Vec<std::string::String> as Dummy<Paragraphs<L>>>
    = note: required for `Vec<Isbn<EN>>` to implement `fake::private::FakeBase<Vec<std::string::String>>`

It'd be nice if we could have a Vec of barcodes please :)

Thank you for the nice library.

@cksac
Copy link
Owner

cksac commented Jan 22, 2024

you can do it like below,

use dummy::Dummy;
use fake::{faker::barcode::raw::*, locales::EN, Fake, Faker};

#[derive(Debug, Dummy)]
pub struct Order {
    #[dummy(faker = "(Isbn(EN), 4..8)")]
    books: Vec<String>,
}

fn main() {
    // 1d vec
    // using tuple for vec (T, L), where U: Dummy<T>, usize: Dummy<L>
    // U = Strig, T = Isbn(EN), L = 4..8
    let isbn_vec: Vec<String> = (Isbn(EN), 4..8).fake();
    println!("Vec {:?}", isbn_vec);

    // 2d vec
    // using tuple for vec ((T, L1), L2), where U: Dummy<T>, usize: Dummy<L1>, usize: Dummy<L2>
    // U = Strig, T = Isbn(EN), L1 = 4..8, L2: 10
    let isbn_vec: Vec<Vec<String>> = ((Isbn(EN), 4..8), 10).fake();
    println!("Vec {:?}", isbn_vec);

    let o = Faker.fake::<Order>();
    println!("Order {:?}", o);
}

@matiu2
Copy link
Author

matiu2 commented Jan 22, 2024

Thank you @cksac; that worked great.

For anyone else that googles this, here's an example of how I did it with derive (and I'm only importing fake when cfg(test) is on:

#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(test, derive(fake::Dummy))]
#[serde(rename_all = "camelCase")]
pub struct Request {
    /// Prescription barcodes
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    #[cfg_attr(test, dummy(faker = "(fake::faker::barcode::en::Isbn13(), 0..4)"))]
    pub prescription_barcodes: Vec<String>,
}

@cksac cksac closed this as completed Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants