Skip to content

Commit

Permalink
Apply string casing to method params while deriving proxies and services
Browse files Browse the repository at this point in the history
  • Loading branch information
Eligioo committed Dec 16, 2024
1 parent 18e779f commit 633794f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use darling::FromMeta;
use heck::{ToKebabCase, ToLowerCamelCase, ToShoutySnakeCase, ToSnakeCase, ToUpperCamelCase};
use proc_macro2::{Literal, TokenStream};
use quote::{format_ident, quote};
use syn::{Attribute, FnArg, Ident, Pat, Signature, Type};
use syn::{spanned::Spanned, Attribute, FnArg, Ident, Pat, Signature, Type};

use proxy::proxy_macro;
use service::service_macro;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl MethodAttributes {

pub(crate) struct RpcMethod<'a> {
signature: &'a Signature,
args: Vec<(&'a Ident, &'a Type)>,
args: Vec<(Ident, &'a Type)>,
method_name: String,
method_name_literal: Literal,
args_struct_ident: Ident,
Expand All @@ -67,6 +67,7 @@ impl<'a> RpcMethod<'a> {
) -> Self {
let mut has_self = false;
let mut args = vec![];
let rename_all = rename_all.as_ref();

for arg in &signature.inputs {
match arg {
Expand All @@ -75,7 +76,12 @@ impl<'a> RpcMethod<'a> {
}
FnArg::Typed(pat_type) => {
let ident = match &*pat_type.pat {
Pat::Ident(ty) => &ty.ident,
Pat::Ident(ty) => {
let fn_arg = rename_all
.map(|r| r.rename(&ty.ident.to_string()))
.unwrap_or(ty.ident.to_string());
Ident::new(&fn_arg, ty.span())
}
_ => panic!("Arguments must not be patterns."),
};
args.push((ident, &*pat_type.ty));
Expand All @@ -92,7 +98,6 @@ impl<'a> RpcMethod<'a> {

let method_name = signature.ident.to_string();
let method_name = rename_all
.as_ref()
.map(|r| r.rename(&method_name))
.unwrap_or(method_name);
let method_name_literal = Literal::string(&method_name);
Expand Down Expand Up @@ -120,6 +125,7 @@ impl<'a> RpcMethod<'a> {
let tokens = quote! {
#[derive(Debug, ::serde::Serialize, ::serde::Deserialize)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
struct #args_struct_ident {
#(#struct_fields)*
}
Expand Down Expand Up @@ -213,6 +219,7 @@ impl<'a> RpcMethod<'a> {
};

quote! {
#[allow(non_snake_case)]
async fn #method_ident(&mut self, #(#method_args),*) #output {
let args = #args_struct_ident {
#(#struct_fields),*
Expand Down

0 comments on commit 633794f

Please sign in to comment.