From 42432d28a38a5812d6cc4594eaef3af59bcb808e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 27 Jan 2022 11:53:57 +0000 Subject: [PATCH 1/3] Only convert struct names to camel case for `Call` variant structs --- codegen/src/api/calls.rs | 13 +++++++++--- codegen/src/api/events.rs | 8 ++++++-- codegen/src/api/mod.rs | 11 +++++++--- codegen/src/types/composite_def.rs | 3 +-- codegen/src/types/tests.rs | 33 ++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 10 deletions(-) diff --git a/codegen/src/api/calls.rs b/codegen/src/api/calls.rs index 25633ff4dc..37681aac7b 100644 --- a/codegen/src/api/calls.rs +++ b/codegen/src/api/calls.rs @@ -22,7 +22,10 @@ use frame_metadata::{ PalletCallMetadata, PalletMetadata, }; -use heck::SnakeCase as _; +use heck::{ + CamelCase as _, + SnakeCase as _, +}; use proc_macro2::TokenStream as TokenStream2; use proc_macro_error::abort_call_site; use quote::{ @@ -37,8 +40,12 @@ pub fn generate_calls( call: &PalletCallMetadata, types_mod_ident: &syn::Ident, ) -> TokenStream2 { - let struct_defs = - super::generate_structs_from_variants(type_gen, call.ty.id(), "Call"); + let struct_defs = super::generate_structs_from_variants( + type_gen, + call.ty.id(), + |name| name.to_camel_case(), + "Call", + ); let (call_structs, call_fns): (Vec<_>, Vec<_>) = struct_defs .iter() .map(|struct_def| { diff --git a/codegen/src/api/events.rs b/codegen/src/api/events.rs index 0d0f51f6fe..d5a5f51132 100644 --- a/codegen/src/api/events.rs +++ b/codegen/src/api/events.rs @@ -29,8 +29,12 @@ pub fn generate_events( event: &PalletEventMetadata, types_mod_ident: &syn::Ident, ) -> TokenStream2 { - let struct_defs = - super::generate_structs_from_variants(type_gen, event.ty.id(), "Event"); + let struct_defs = super::generate_structs_from_variants( + type_gen, + event.ty.id(), + |name| name.to_string(), + "Event", + ); let event_structs = struct_defs.iter().map(|struct_def| { let pallet_name = &pallet.name; let event_struct = &struct_def.name; diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index e7d317f6d1..1b65764339 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -319,19 +319,24 @@ impl RuntimeGenerator { } } -pub fn generate_structs_from_variants<'a>( +pub fn generate_structs_from_variants<'a, F>( type_gen: &'a TypeGenerator, type_id: u32, + variant_to_struct_name: F, error_message_type_name: &str, -) -> Vec { +) -> Vec +where + F: Fn(&str) -> String, +{ let ty = type_gen.resolve_type(type_id); if let scale_info::TypeDef::Variant(variant) = ty.type_def() { variant .variants() .iter() .map(|var| { + let struct_name = variant_to_struct_name(var.name()); let fields = CompositeDefFields::from_scale_info_fields( - var.name(), + &struct_name, var.fields(), &[], type_gen, diff --git a/codegen/src/types/composite_def.rs b/codegen/src/types/composite_def.rs index b6e5548f06..86d1aa31a4 100644 --- a/codegen/src/types/composite_def.rs +++ b/codegen/src/types/composite_def.rs @@ -22,7 +22,6 @@ use super::{ TypeParameter, TypePath, }; -use heck::CamelCase as _; use proc_macro2::TokenStream; use proc_macro_error::abort_call_site; use quote::{ @@ -85,7 +84,7 @@ impl CompositeDef { } } - let name = format_ident!("{}", ident.to_camel_case()); + let name = format_ident!("{}", ident); Self { name, diff --git a/codegen/src/types/tests.rs b/codegen/src/types/tests.rs index a1f2fe094a..639ce2a949 100644 --- a/codegen/src/types/tests.rs +++ b/codegen/src/types/tests.rs @@ -853,3 +853,36 @@ fn modules() { .to_string() ) } + +#[test] +fn dont_force_struct_names_camel_case() { + #[allow(unused)] + #[derive(TypeInfo)] + struct AB; + + let mut registry = Registry::new(); + registry.register_type(&meta_type::()); + let portable_types: PortableRegistry = registry.into(); + + let type_gen = TypeGenerator::new( + &portable_types, + "root", + Default::default(), + Default::default(), + ); + let types = type_gen.generate_types_mod(); + let tests_mod = get_mod(&types, MOD_PATH).unwrap(); + + assert_eq!( + tests_mod.into_token_stream().to_string(), + quote! { + pub mod tests { + use super::root; + + #[derive(::subxt::codec::Encode, ::subxt::codec::Decode, Debug)] + pub struct AB; + } + } + .to_string() + ) +} From f774cb6b57c5999a2fdc39ec120058459deba4e8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 27 Jan 2022 11:58:00 +0000 Subject: [PATCH 2/3] Use Cow for transform fn --- codegen/src/api/calls.rs | 2 +- codegen/src/api/events.rs | 2 +- codegen/src/api/mod.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/codegen/src/api/calls.rs b/codegen/src/api/calls.rs index 37681aac7b..ea769a3d94 100644 --- a/codegen/src/api/calls.rs +++ b/codegen/src/api/calls.rs @@ -43,7 +43,7 @@ pub fn generate_calls( let struct_defs = super::generate_structs_from_variants( type_gen, call.ty.id(), - |name| name.to_camel_case(), + |name| name.to_camel_case().into(), "Call", ); let (call_structs, call_fns): (Vec<_>, Vec<_>) = struct_defs diff --git a/codegen/src/api/events.rs b/codegen/src/api/events.rs index d5a5f51132..6c2389b52b 100644 --- a/codegen/src/api/events.rs +++ b/codegen/src/api/events.rs @@ -32,7 +32,7 @@ pub fn generate_events( let struct_defs = super::generate_structs_from_variants( type_gen, event.ty.id(), - |name| name.to_string(), + |name| name.into(), "Event", ); let event_structs = struct_defs.iter().map(|struct_def| { diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 1b65764339..b35a9eaf28 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -326,7 +326,7 @@ pub fn generate_structs_from_variants<'a, F>( error_message_type_name: &str, ) -> Vec where - F: Fn(&str) -> String, + F: Fn(&str) -> std::borrow::Cow, { let ty = type_gen.resolve_type(type_id); if let scale_info::TypeDef::Variant(variant) = ty.type_def() { From bda7ec42eb099e222c84e9de8bdaca2ec9d35dd4 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 27 Jan 2022 12:01:44 +0000 Subject: [PATCH 3/3] Use as_ref --- codegen/src/api/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index b35a9eaf28..44af2de52e 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -336,7 +336,7 @@ where .map(|var| { let struct_name = variant_to_struct_name(var.name()); let fields = CompositeDefFields::from_scale_info_fields( - &struct_name, + struct_name.as_ref(), var.fields(), &[], type_gen,