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

Only convert struct names to camel case for Call variant structs #412

Merged
merged 3 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions codegen/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -37,8 +40,12 @@ pub fn generate_calls(
call: &PalletCallMetadata<PortableForm>,
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().into(),
"Call",
);
let (call_structs, call_fns): (Vec<_>, Vec<_>) = struct_defs
.iter()
.map(|struct_def| {
Expand Down
8 changes: 6 additions & 2 deletions codegen/src/api/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ pub fn generate_events(
event: &PalletEventMetadata<PortableForm>,
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.into(),
"Event",
);
let event_structs = struct_defs.iter().map(|struct_def| {
let pallet_name = &pallet.name;
let event_struct = &struct_def.name;
Expand Down
11 changes: 8 additions & 3 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompositeDef> {
) -> Vec<CompositeDef>
where
F: Fn(&str) -> std::borrow::Cow<str>,
{
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.as_ref(),
var.fields(),
&[],
type_gen,
Expand Down
3 changes: 1 addition & 2 deletions codegen/src/types/composite_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -85,7 +84,7 @@ impl CompositeDef {
}
}

let name = format_ident!("{}", ident.to_camel_case());
let name = format_ident!("{}", ident);

Self {
name,
Expand Down
33 changes: 33 additions & 0 deletions codegen/src/types/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<AB>());
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()
)
}