From 15f558eb4d76af4f151771f0cbc3dd96a2f4ff5a Mon Sep 17 00:00:00 2001 From: Kyle McCarthy Date: Mon, 12 Dec 2022 16:12:56 -0600 Subject: [PATCH 1/5] style: run cargo fmt --- dummy_derive/src/lib.rs | 45 ++++++++++-------------- fake/src/faker/impls/administrative.rs | 12 ++----- fake/src/faker/impls/automotive.rs | 7 ++-- fake/src/faker/impls/filesystem.rs | 6 ++-- fake/src/impls/color/mod.rs | 4 +-- fake/src/impls/decimal/mod.rs | 8 +++-- fake/src/impls/semver/mod.rs | 11 +++--- fake/src/impls/zerocopy_byteorder/mod.rs | 1 - fake/tests/derive_macros.rs | 24 +++---------- 9 files changed, 46 insertions(+), 72 deletions(-) diff --git a/dummy_derive/src/lib.rs b/dummy_derive/src/lib.rs index 89e60b7..7f350ca 100644 --- a/dummy_derive/src/lib.rs +++ b/dummy_derive/src/lib.rs @@ -68,12 +68,7 @@ pub fn hello_world(input: TokenStream) -> TokenStream { impl_dummy } ast::Style::Tuple => { - let tuple_fields: Vec<_> = fields - .iter() - .map(|f| { - expose_field(f) - }) - .collect(); + let tuple_fields: Vec<_> = fields.iter().map(|f| expose_field(f)).collect(); let impl_dummy = quote! { impl fake::Dummy for #receiver_name { @@ -128,14 +123,8 @@ pub fn hello_world(input: TokenStream) -> TokenStream { } } ast::Style::Tuple => { - let tuple_fields: Vec<_> = f - .fields - .fields - .iter() - .map(|f| { - expose_field(&f) - }) - .collect(); + let tuple_fields: Vec<_> = + f.fields.fields.iter().map(|f| expose_field(&f)).collect(); quote! { #i => { @@ -151,17 +140,19 @@ pub fn hello_world(input: TokenStream) -> TokenStream { .map(|f| f.ident.as_ref().unwrap()) .collect(); - let let_statements: Vec<_> = f.fields.fields - .iter() - .map(|f| { - let field_name = f.ident.as_ref().unwrap(); - let field_ty = &f.ty; - let stream = expose_field(&f); - quote! { - let #field_name: #field_ty = #stream; - } - }) - .collect(); + let let_statements: Vec<_> = f + .fields + .fields + .iter() + .map(|f| { + let field_name = f.ident.as_ref().unwrap(); + let field_ty = &f.ty; + let stream = expose_field(&f); + quote! { + let #field_name: #field_ty = #stream; + } + }) + .collect(); quote! { #i => { @@ -208,12 +199,12 @@ pub fn hello_world(input: TokenStream) -> TokenStream { fn expose_field(f: &DummyField) -> proc_macro2::TokenStream { if f.default { - quote!{ + quote! { Default::default() } } else if let Some(ref expr) = f.fixed { let fixed = syn::parse_str::(expr).unwrap(); - quote!{ + quote! { #fixed } } else { diff --git a/fake/src/faker/impls/administrative.rs b/fake/src/faker/impls/administrative.rs index 9aa7c37..8f8ebe3 100644 --- a/fake/src/faker/impls/administrative.rs +++ b/fake/src/faker/impls/administrative.rs @@ -26,15 +26,9 @@ impl Dummy> for String { let town_code: u16 = (0..999).fake_with_rng::(rng); let order_code: u16 = (0..999).fake_with_rng::(rng); let department_code: u16 = match department { - "2A" => { - 19 - } - "2B" => { - 18 - } - _ => { - department.parse::().unwrap() - } + "2A" => 19, + "2B" => 18, + _ => department.parse::().unwrap(), }; let number = format!( "{}{:02}{:02}{}{:03}{:03}", diff --git a/fake/src/faker/impls/automotive.rs b/fake/src/faker/impls/automotive.rs index 3beadc5..8416f76 100644 --- a/fake/src/faker/impls/automotive.rs +++ b/fake/src/faker/impls/automotive.rs @@ -9,10 +9,13 @@ use std::char; As with the SIV system, The letters I and O were never used because they could be confused with other characters, like 1 and 0. ref https://en.wikipedia.org/wiki/Vehicle_registration_plates_of_France -The letter U where also not used because it could be confused with V letter. +The letter U where also not used because it could be confused with V letter. ref on french wikipedia article https://fr.wikipedia.org/wiki/Plaque_d%27immatriculation_fran%C3%A7aise */ -const LICENSE_CHARS: [char; 23] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z']; +const LICENSE_CHARS: [char; 23] = [ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', + 'W', 'X', 'Y', 'Z', +]; #[inline] fn numerify_licence_plate(string: &str, rng: &mut R) -> String { diff --git a/fake/src/faker/impls/filesystem.rs b/fake/src/faker/impls/filesystem.rs index 1caa8ab..ab79901 100644 --- a/fake/src/faker/impls/filesystem.rs +++ b/fake/src/faker/impls/filesystem.rs @@ -1925,7 +1925,7 @@ const UNSTABLE_SEMVER: &[&str] = &["alpha", "beta", "rc"]; impl Dummy> for String { fn dummy_with_rng(_: &Semver, rng: &mut R) -> Self { - let patch = &mut(0..20).fake_with_rng::(rng).to_string(); + let patch = &mut (0..20).fake_with_rng::(rng).to_string(); let probability = 10; if Boolean(EN, probability).fake_with_rng(rng) { patch.push_str(&format!( @@ -1945,7 +1945,7 @@ impl Dummy> for String { impl Dummy> for String { fn dummy_with_rng(_: &SemverStable, rng: &mut R) -> Self { - let patch = &mut(0..20).fake_with_rng::(rng).to_string(); + let patch = &mut (0..20).fake_with_rng::(rng).to_string(); format!( "{}.{}.{}", &(0..9).fake_with_rng::(rng).to_string(), @@ -1957,7 +1957,7 @@ impl Dummy> for String { impl Dummy> for String { fn dummy_with_rng(_: &SemverUnstable, rng: &mut R) -> Self { - let patch = &mut(0..20).fake_with_rng::(rng).to_string(); + let patch = &mut (0..20).fake_with_rng::(rng).to_string(); patch.push_str(&format!( "-{}.{}", *UNSTABLE_SEMVER.choose(rng).unwrap(), diff --git a/fake/src/impls/color/mod.rs b/fake/src/impls/color/mod.rs index 4d06ca9..856aa9e 100644 --- a/fake/src/impls/color/mod.rs +++ b/fake/src/impls/color/mod.rs @@ -1,7 +1,7 @@ use crate::{Dummy, Fake, Faker}; use rand::Rng; -use random_color::{RandomColor, Luminosity}; +use random_color::{Luminosity, RandomColor}; impl Dummy for RandomColor { fn dummy_with_rng(_: &Faker, rng: &mut R) -> Self { @@ -9,7 +9,7 @@ impl Dummy for RandomColor { hue: None, luminosity: Some(Luminosity::Random), seed: Some((u64::MIN..u64::MAX).fake_with_rng::(rng)), - alpha: Some((0..10).fake_with_rng::(rng) as f32 / 10.) + alpha: Some((0..10).fake_with_rng::(rng) as f32 / 10.), } } } diff --git a/fake/src/impls/decimal/mod.rs b/fake/src/impls/decimal/mod.rs index 5a74742..70089b0 100644 --- a/fake/src/impls/decimal/mod.rs +++ b/fake/src/impls/decimal/mod.rs @@ -20,7 +20,7 @@ impl Dummy for rust_decimal::Decimal { impl Dummy for rust_decimal::Decimal { fn dummy_with_rng(_: &Decimal, rng: &mut R) -> Self { - Faker.fake_with_rng(rng) + Faker.fake_with_rng(rng) } } @@ -50,6 +50,8 @@ impl Dummy for rust_decimal::Decimal { impl Dummy for rust_decimal::Decimal { fn dummy_with_rng(_: &NoDecimalPoints, rng: &mut R) -> Self { - Faker.fake_with_rng::(rng).round_dp(0) + Faker + .fake_with_rng::(rng) + .round_dp(0) } -} \ No newline at end of file +} diff --git a/fake/src/impls/semver/mod.rs b/fake/src/impls/semver/mod.rs index 827f9a3..42efd18 100644 --- a/fake/src/impls/semver/mod.rs +++ b/fake/src/impls/semver/mod.rs @@ -1,12 +1,10 @@ -use crate::{Dummy, Fake, Faker}; use crate::faker::boolean::raw::Boolean; -use crate::locales::{EN}; +use crate::locales::EN; +use crate::{Dummy, Fake, Faker}; use rand::seq::SliceRandom; use rand::Rng; -const UNSTABLE_SEMVER: &'static [&'static str] = &[ - "alpha", "beta", "rc" -]; +const UNSTABLE_SEMVER: &'static [&'static str] = &["alpha", "beta", "rc"]; impl Dummy for semver::Version { fn dummy_with_rng(_: &Faker, rng: &mut R) -> Self { @@ -16,7 +14,8 @@ impl Dummy for semver::Version { "{}.{}", *UNSTABLE_SEMVER.choose(rng).unwrap(), &(0..9).fake_with_rng::(rng).to_string() - )).unwrap() + )) + .unwrap() } else { semver::Prerelease::EMPTY }; diff --git a/fake/src/impls/zerocopy_byteorder/mod.rs b/fake/src/impls/zerocopy_byteorder/mod.rs index e4487d0..addad9b 100644 --- a/fake/src/impls/zerocopy_byteorder/mod.rs +++ b/fake/src/impls/zerocopy_byteorder/mod.rs @@ -23,4 +23,3 @@ byteorder_faker_impl!(I32); byteorder_faker_impl!(I64); #[cfg(not(target_os = "emscripten"))] byteorder_faker_impl!(I128); - diff --git a/fake/tests/derive_macros.rs b/fake/tests/derive_macros.rs index 67558f9..5041d7f 100644 --- a/fake/tests/derive_macros.rs +++ b/fake/tests/derive_macros.rs @@ -107,10 +107,7 @@ mod field_options { #[test] fn override_range() { #[derive(Dummy)] - struct Obj( - #[dummy(faker = "100..200")] - i32 - ); + struct Obj(#[dummy(faker = "100..200")] i32); let o: Obj = Faker.fake_with_rng(&mut rng()); @@ -125,9 +122,7 @@ mod field_options { Two, } #[derive(Dummy)] - struct Obj( - MyEnum, - ); + struct Obj(MyEnum); let o: Obj = Faker.fake_with_rng(&mut rng()); @@ -137,10 +132,7 @@ mod field_options { #[test] fn with_default() { #[derive(Dummy)] - struct Obj( - #[dummy(default)] - String, - ); + struct Obj(#[dummy(default)] String); let o: Obj = Faker.fake_with_rng(&mut rng()); @@ -150,10 +142,7 @@ mod field_options { #[test] fn with_override_faker() { #[derive(Dummy)] - struct Obj( - #[dummy(faker = "fake::faker::name::en::Name()")] - String, - ); + struct Obj(#[dummy(faker = "fake::faker::name::en::Name()")] String); let o: Obj = Faker.fake_with_rng(&mut rng()); @@ -163,10 +152,7 @@ mod field_options { #[test] fn with_override_fixed_i32() { #[derive(Dummy)] - struct Obj( - #[dummy(fixed = "42")] - i32, - ); + struct Obj(#[dummy(fixed = "42")] i32); let o: Obj = Faker.fake_with_rng(&mut rng()); From 7ecc5f94062e710923729c321f149ea25c1ec65b Mon Sep 17 00:00:00 2001 From: Kyle McCarthy Date: Mon, 12 Dec 2022 16:14:14 -0600 Subject: [PATCH 2/5] style: resolve clippy warnings --- dummy_derive/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dummy_derive/src/lib.rs b/dummy_derive/src/lib.rs index 7f350ca..63b42a3 100644 --- a/dummy_derive/src/lib.rs +++ b/dummy_derive/src/lib.rs @@ -68,7 +68,7 @@ pub fn hello_world(input: TokenStream) -> TokenStream { impl_dummy } ast::Style::Tuple => { - let tuple_fields: Vec<_> = fields.iter().map(|f| expose_field(f)).collect(); + let tuple_fields: Vec<_> = fields.iter().map(expose_field).collect(); let impl_dummy = quote! { impl fake::Dummy for #receiver_name { @@ -88,7 +88,7 @@ pub fn hello_world(input: TokenStream) -> TokenStream { .map(|f| { let field_name = f.ident.as_ref().unwrap(); let field_ty = &f.ty; - let stream = expose_field(&f); + let stream = expose_field(f); quote! { let #field_name: #field_ty = #stream; } @@ -124,7 +124,7 @@ pub fn hello_world(input: TokenStream) -> TokenStream { } ast::Style::Tuple => { let tuple_fields: Vec<_> = - f.fields.fields.iter().map(|f| expose_field(&f)).collect(); + f.fields.fields.iter().map(expose_field).collect(); quote! { #i => { @@ -147,7 +147,7 @@ pub fn hello_world(input: TokenStream) -> TokenStream { .map(|f| { let field_name = f.ident.as_ref().unwrap(); let field_ty = &f.ty; - let stream = expose_field(&f); + let stream = expose_field(f); quote! { let #field_name: #field_ty = #stream; } From 06fa31378294256c9ca7ab19ae24fad516e59491 Mon Sep 17 00:00:00 2001 From: Kyle McCarthy Date: Mon, 12 Dec 2022 16:15:13 -0600 Subject: [PATCH 3/5] test: add failing test --- fake/tests/derive_macros.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fake/tests/derive_macros.rs b/fake/tests/derive_macros.rs index 5041d7f..322bd7a 100644 --- a/fake/tests/derive_macros.rs +++ b/fake/tests/derive_macros.rs @@ -306,3 +306,16 @@ mod field_options { } } } + +mod test_trait_scope { + #[test] + #[allow(dead_code)] + fn it_generates_without_fake_in_scope() { + mod outer { + #[derive(fake::Dummy)] + pub struct Outer { + pub message: String, + } + } + } +} From 0088b8bebdac98568050c1ac0b8b92bd30b6af6f Mon Sep 17 00:00:00 2001 From: Kyle McCarthy Date: Mon, 12 Dec 2022 16:16:36 -0600 Subject: [PATCH 4/5] feat: use fully qualified syntax in trait call --- dummy_derive/src/lib.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/dummy_derive/src/lib.rs b/dummy_derive/src/lib.rs index 63b42a3..4757688 100644 --- a/dummy_derive/src/lib.rs +++ b/dummy_derive/src/lib.rs @@ -208,14 +208,21 @@ fn expose_field(f: &DummyField) -> proc_macro2::TokenStream { #fixed } } else { - let faker = if let Some(ref expr) = f.faker { - syn::parse_str::(expr).unwrap() - } else { - syn::parse_str::("fake::Faker").unwrap() - }; let field_ty = &f.ty; - quote! { - (#faker).fake_with_rng::<#field_ty, _>(rng) + + if let Some(ref expr) = f.faker { + let faker = syn::parse_str::(expr).unwrap(); + + quote! { + (#faker).fake_with_rng::<#field_ty, _>(rng) + } + } else { + let faker = syn::parse_str::("fake::Faker").unwrap(); + let fake = syn::parse_str::("fake::Fake").unwrap(); + + quote! { + <#faker as #fake>::fake_with_rng::<#field_ty, _>(&#faker, rng) + } } } } From 38089be0d7be66163efb2e53cbf7212b8fb485ac Mon Sep 17 00:00:00 2001 From: Kyle McCarthy Date: Mon, 12 Dec 2022 22:04:40 -0600 Subject: [PATCH 5/5] feat: use fully qualified syntax in trait call for field attrs --- dummy_derive/src/lib.rs | 4 ++-- fake/tests/derive_macros.rs | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/dummy_derive/src/lib.rs b/dummy_derive/src/lib.rs index 4757688..616be42 100644 --- a/dummy_derive/src/lib.rs +++ b/dummy_derive/src/lib.rs @@ -209,16 +209,16 @@ fn expose_field(f: &DummyField) -> proc_macro2::TokenStream { } } else { let field_ty = &f.ty; + let fake = syn::parse_str::("fake::Fake").unwrap(); if let Some(ref expr) = f.faker { let faker = syn::parse_str::(expr).unwrap(); quote! { - (#faker).fake_with_rng::<#field_ty, _>(rng) + #fake::fake_with_rng::<#field_ty, _>(&(#faker), rng) } } else { let faker = syn::parse_str::("fake::Faker").unwrap(); - let fake = syn::parse_str::("fake::Fake").unwrap(); quote! { <#faker as #fake>::fake_with_rng::<#field_ty, _>(&#faker, rng) diff --git a/fake/tests/derive_macros.rs b/fake/tests/derive_macros.rs index 322bd7a..6367f03 100644 --- a/fake/tests/derive_macros.rs +++ b/fake/tests/derive_macros.rs @@ -316,6 +316,12 @@ mod test_trait_scope { pub struct Outer { pub message: String, } + + #[derive(fake::Dummy)] + pub struct Outer2 { + #[dummy(faker = "1000..2000")] + pub id: usize, + } } } }