Skip to content

Commit

Permalink
fix: syntax for linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy-lmao committed Nov 7, 2024
1 parent 6d46bac commit 5fcc018
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 256 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT OR Apache-2.0"
readme = "README.md"

[dependencies]
convert_case = "0.6.0"
pretty_assertions = "1.4.1"
prettyplease = "0.2.25"
proc-macro2 = "1.0.89"
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use convert_case::{Case, Casing};
use modules::{delete_query_builder, update_query_builder};
use modules::{insert_query_builder, one_query_builder};
use proc_macro::TokenStream;
Expand Down Expand Up @@ -29,7 +30,13 @@ pub fn dbset_derive(input: TokenStream) -> TokenStream {
let from_row_impl = from_row::get_from_row_impl(&input);
let dbset_impl = dbset::get_dbset_impl(&input);

let module_name = quote::format_ident!("{}_module", dbset_name);
let module_name = quote::format_ident!(
"{}_module",
dbset_name
.to_string()
.from_case(Case::Pascal)
.to_case(Case::Snake)
);

let expanded = quote! {

Expand Down
32 changes: 27 additions & 5 deletions src/modules/delete_query_builder/delete_query_builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use convert_case::{Case, Casing};
use proc_macro2::Ident;
use quote::quote;
use syn::DeriveInput;
Expand Down Expand Up @@ -30,8 +31,10 @@ pub fn get_query_builder(input: &DeriveInput) -> proc_macro2::TokenStream {
let builder_struct_generics = all_required_insert_fields
.clone()
.map(|(field_name, _)| {

let gen_name_pascal = quote::format_ident!("{}", field_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
quote! {
#field_name = NotSet,
#gen_name_pascal = NotSet,
}
})
.chain(vec![quote! {
Expand All @@ -55,8 +58,9 @@ pub fn get_query_builder(input: &DeriveInput) -> proc_macro2::TokenStream {
}]);

let phantom_struct_fields = all_required_insert_fields.clone().map(|(name, _)| {
let gen_name_pascal = quote::format_ident!("{}", name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
let ph_name = quote::format_ident!("_{}", name);
quote! { #ph_name: std::marker::PhantomData::<#name>, }
quote! { #ph_name: std::marker::PhantomData::<#gen_name_pascal>, }
});

// Create Builder Struct
Expand Down Expand Up @@ -140,21 +144,39 @@ pub fn get_query_builder(input: &DeriveInput) -> proc_macro2::TokenStream {

let pre_impl_generics_in = all_required_insert_fields.clone().map(|(gen_name, _)|{
if gen_name != field_name {
return quote!{ #gen_name, }
if !is_unique_field {
let gen_name_pascal = quote::format_ident!("{}", gen_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
return quote!{ #gen_name_pascal, }
} else {
return quote!{ }

}
}
quote!{ }

});

let generics_in = all_required_insert_fields.clone().map(|(gen_name, _)|{
if gen_name != field_name {
return quote!{ #gen_name, }
if !is_unique_field {
let gen_name_pascal = quote::format_ident!("{}", gen_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
return quote!{ #gen_name_pascal, }
} else {
return quote!{ NotSet, }
}
}

quote!{ NotSet, }
}).chain(vec![quote!{NotSet}]);

let generics_out = all_required_insert_fields.clone().map(|(gen_name, _)|{
if gen_name != field_name {
return quote!{ #gen_name, }
if !is_unique_field {
let gen_name_pascal = quote::format_ident!("{}", gen_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
return quote!{ #gen_name_pascal, }
} else {
return quote!{ NotSet, }
}
}
quote!{ Set, }
}).chain( if is_unique_field {
Expand Down
24 changes: 12 additions & 12 deletions src/modules/delete_query_builder/test_delete_query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ pub struct User {
"#;

let output = r#"
pub struct UserDbSetDeleteQueryBuilder<id = NotSet, UniqueFields = NotSet> {
pub struct UserDbSetDeleteQueryBuilder<Id = NotSet, UniqueFields = NotSet> {
id: Option<String>,
email: Option<String>,
_unique_fields: std::marker::PhantomData<UniqueFields>,
_id: std::marker::PhantomData<id>,
_id: std::marker::PhantomData<Id>,
}
impl UserDbSetDeleteQueryBuilder {
pub fn new() -> UserDbSetDeleteQueryBuilder<NotSet, NotSet> {
Expand All @@ -56,8 +56,8 @@ impl UserDbSetDeleteQueryBuilder<NotSet, NotSet> {
}
}
}
impl<id> UserDbSetDeleteQueryBuilder<id, NotSet> {
pub fn email_eq(self, email: String) -> UserDbSetDeleteQueryBuilder<id, Set> {
impl UserDbSetDeleteQueryBuilder<NotSet, NotSet> {
pub fn email_eq(self, email: String) -> UserDbSetDeleteQueryBuilder<NotSet, Set> {
UserDbSetDeleteQueryBuilder {
email: Some(email),
id: self.id,
Expand Down Expand Up @@ -183,15 +183,15 @@ pub struct FavouritedProduct {

let output = r#"
pub struct FavouritedProductDbSetDeleteQueryBuilder<
product_id = NotSet,
user_id = NotSet,
ProductId = NotSet,
UserId = NotSet,
UniqueFields = NotSet,
> {
product_id: Option<uuid::Uuid>,
user_id: Option<uuid::Uuid>,
_unique_fields: std::marker::PhantomData<UniqueFields>,
_product_id: std::marker::PhantomData<product_id>,
_user_id: std::marker::PhantomData<user_id>,
_product_id: std::marker::PhantomData<ProductId>,
_user_id: std::marker::PhantomData<UserId>,
}
impl FavouritedProductDbSetDeleteQueryBuilder {
Expand All @@ -205,11 +205,11 @@ impl FavouritedProductDbSetDeleteQueryBuilder {
}
}
}
impl<user_id> FavouritedProductDbSetDeleteQueryBuilder<NotSet, user_id, NotSet> {
impl<UserId> FavouritedProductDbSetDeleteQueryBuilder<NotSet, UserId, NotSet> {
pub fn product_id_eq(
self,
product_id: uuid::Uuid,
) -> FavouritedProductDbSetDeleteQueryBuilder<Set, user_id, NotSet> {
) -> FavouritedProductDbSetDeleteQueryBuilder<Set, UserId, NotSet> {
FavouritedProductDbSetDeleteQueryBuilder {
product_id: Some(product_id),
user_id: self.user_id,
Expand All @@ -219,11 +219,11 @@ impl<user_id> FavouritedProductDbSetDeleteQueryBuilder<NotSet, user_id, NotSet>
}
}
}
impl<product_id> FavouritedProductDbSetDeleteQueryBuilder<product_id, NotSet, NotSet> {
impl<ProductId> FavouritedProductDbSetDeleteQueryBuilder<ProductId, NotSet, NotSet> {
pub fn user_id_eq(
self,
user_id: uuid::Uuid,
) -> FavouritedProductDbSetDeleteQueryBuilder<product_id, Set, NotSet> {
) -> FavouritedProductDbSetDeleteQueryBuilder<ProductId, Set, NotSet> {
FavouritedProductDbSetDeleteQueryBuilder {
user_id: Some(user_id),
product_id: self.product_id,
Expand Down
17 changes: 12 additions & 5 deletions src/modules/insert_query_builder/insert_query_builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use convert_case::{Casing,Case};
use proc_macro2::Ident;
use quote::quote;
use syn::{DeriveInput, Type};
Expand Down Expand Up @@ -34,8 +35,9 @@ pub fn get_insert_query_builder(input: &DeriveInput) -> proc_macro2::TokenStream

// Get builder struct generics
let builder_struct_generics = all_required_insert_fields.clone().map(|(field_name, _)| {
let gen_name_pascal = quote::format_ident!("{}", field_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
quote! {
#field_name = NotSet,
#gen_name_pascal = NotSet,
}
});

Expand All @@ -54,8 +56,9 @@ pub fn get_insert_query_builder(input: &DeriveInput) -> proc_macro2::TokenStream
});

let phantom_struct_fields = all_required_insert_fields.clone().map(|(name, _)| {
let gen_name_pascal = quote::format_ident!("{}", name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
let ph_name = quote::format_ident!("_{}", name);
quote! { #ph_name: std::marker::PhantomData::<#name>, }
quote! { #ph_name: std::marker::PhantomData::<#gen_name_pascal>, }
});

// Create Builder Struct
Expand Down Expand Up @@ -122,22 +125,26 @@ pub fn get_insert_query_builder(input: &DeriveInput) -> proc_macro2::TokenStream


let pre_impl_generics_in = all_required_insert_fields.clone().map(|(gen_name, _)|{

if gen_name != field_name {
return quote!{ #gen_name, }
let gen_name_pascal = quote::format_ident!("{}", gen_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
return quote!{ #gen_name_pascal, }
}
quote!{ }
});

let generics_in = all_required_insert_fields.clone().map(|(gen_name, _)|{
if gen_name != field_name {
return quote!{ #gen_name, }
let gen_name_pascal = quote::format_ident!("{}", gen_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
return quote!{ #gen_name_pascal, }
}
quote!{ NotSet, }
});

let generics_out = all_required_insert_fields.clone().map(|(gen_name, _)|{
if gen_name != field_name {
return quote!{ #gen_name, }
let gen_name_pascal = quote::format_ident!("{}", gen_name.to_string().from_case(Case::Snake).to_case(Case::Pascal));
return quote!{ #gen_name_pascal, }
}
quote!{ Set, }
});
Expand Down
28 changes: 14 additions & 14 deletions src/modules/insert_query_builder/test_insert_query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub struct Account {
"#;

let output = r#"
pub struct AccountDbSetInsertBuilder<email = NotSet> {
pub struct AccountDbSetInsertBuilder<Email = NotSet> {
email: Option<String>,
_email: std::marker::PhantomData<email>,
_email: std::marker::PhantomData<Email>,
}
impl AccountDbSetInsertBuilder {
pub fn new() -> AccountDbSetInsertBuilder<NotSet> {
Expand Down Expand Up @@ -82,14 +82,14 @@ pub struct User {
"#;

let output = r#"
pub struct UserDbSetInsertBuilder<id = NotSet, name = NotSet, email = NotSet> {
pub struct UserDbSetInsertBuilder<Id = NotSet, Name = NotSet, Email = NotSet> {
id: Option<String>,
name: Option<String>,
details: Option<String>,
email: Option<String>,
_id: std::marker::PhantomData<id>,
_name: std::marker::PhantomData<name>,
_email: std::marker::PhantomData<email>,
_id: std::marker::PhantomData<Id>,
_name: std::marker::PhantomData<Name>,
_email: std::marker::PhantomData<Email>,
}
impl UserDbSetInsertBuilder {
pub fn new() -> UserDbSetInsertBuilder<NotSet, NotSet, NotSet> {
Expand All @@ -104,8 +104,8 @@ impl UserDbSetInsertBuilder {
}
}
}
impl<name, email> UserDbSetInsertBuilder<NotSet, name, email> {
pub fn id(self, id: String) -> UserDbSetInsertBuilder<Set, name, email> {
impl<Name, Email> UserDbSetInsertBuilder<NotSet, Name, Email> {
pub fn id(self, id: String) -> UserDbSetInsertBuilder<Set, Name, Email> {
UserDbSetInsertBuilder {
id: Some(id),
name: self.name,
Expand All @@ -117,8 +117,8 @@ impl<name, email> UserDbSetInsertBuilder<NotSet, name, email> {
}
}
}
impl<id, email> UserDbSetInsertBuilder<id, NotSet, email> {
pub fn name(self, name: String) -> UserDbSetInsertBuilder<id, Set, email> {
impl<Id, Email> UserDbSetInsertBuilder<Id, NotSet, Email> {
pub fn name(self, name: String) -> UserDbSetInsertBuilder<Id, Set, Email> {
UserDbSetInsertBuilder {
name: Some(name),
id: self.id,
Expand All @@ -130,8 +130,8 @@ impl<id, email> UserDbSetInsertBuilder<id, NotSet, email> {
}
}
}
impl<id,name, email> UserDbSetInsertBuilder<id, name, email> {
pub fn details(self, details: String) -> UserDbSetInsertBuilder<id, name, email> {
impl<Id,Name, Email> UserDbSetInsertBuilder<Id, Name, Email> {
pub fn details(self, details: String) -> UserDbSetInsertBuilder<Id, Name, Email> {
UserDbSetInsertBuilder {
details: Some(details),
id: self.id,
Expand All @@ -143,8 +143,8 @@ impl<id,name, email> UserDbSetInsertBuilder<id, name, email> {
}
}
}
impl<id, name> UserDbSetInsertBuilder<id, name, NotSet> {
pub fn email(self, email: String) -> UserDbSetInsertBuilder<id, name, Set> {
impl<Id, Name> UserDbSetInsertBuilder<Id, Name, NotSet> {
pub fn email(self, email: String) -> UserDbSetInsertBuilder<Id, Name, Set> {
UserDbSetInsertBuilder {
email: Some(email),
id: self.id,
Expand Down
Loading

0 comments on commit 5fcc018

Please sign in to comment.