Refactor type generation, remove code duplication (#352)

* codegen: fix compact unnamed fields

* Fmt

* codegen: move derives and struct_def to types

* codegen: rename struct_def to composite_def.r

* WIP: deduplicate struct def code

* Fmt

* WIP refactoring composite type codegen duplication

* Fmt

* Fix TokenStream import

* Fix field_tokens ty_path parse error

* Fix call struct generation

* Refactor ty_path()

* Optional derives and move CompactAs derive to composite_def

* Fmt

* Introduce CompositeDefFieldType

* Restore default codec derives

* Extract TypeDefParameters and TypeDefGen construction

* Fmt

* Reset codegen to master

* Introduce CompositeDefFields

* Introduce CompositeDefFields

* Fix up errors

* Fix Box field types

* Fmt

* Fix compact attribute

* Handle no fields case

* Handle no fields with trailing semi

* Fix compact field detection

* Fix generic phantom marker

* Fmt

* Fix generic type parm fields

* Clippy

* Fix up boxed call fields

* Fmt

* Add comments to composite_def.rs

* Fix license headers

* Restore Debug derive in tests

* Fix empty struct codegen test

* Use BTreeSet for type params for ordering

* code review: fix comment

* code review: refactor CompositeDefFields as enum

* Fix empty named fields

* Fix generation of call variant enum structs

* Expand field_tokens into separate methods for struct and enum variants

* Add TypeDefParameters docs

* Fix doc link

* Clippy redundant return
This commit is contained in:
Andrew Jones
2022-01-26 09:18:13 +00:00
committed by GitHub
parent 934aebcc2c
commit b0004ea79f
11 changed files with 620 additions and 454 deletions
+27 -12
View File
@@ -14,7 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
use crate::types::TypeGenerator;
use crate::types::{
CompositeDefFields,
TypeGenerator,
};
use frame_metadata::{
PalletCallMetadata,
PalletMetadata,
@@ -39,17 +42,29 @@ pub fn generate_calls(
let (call_structs, call_fns): (Vec<_>, Vec<_>) = struct_defs
.iter()
.map(|struct_def| {
let (call_fn_args, call_args): (Vec<_>, Vec<_>) = struct_def
.named_fields()
.unwrap_or_else(|| {
abort_call_site!(
"Call variant for type {} must have all named fields",
call.ty.id()
)
})
.iter()
.map(|(name, ty)| (quote!( #name: #ty ), name))
.unzip();
let (call_fn_args, call_args): (Vec<_>, Vec<_>) =
match struct_def.fields {
CompositeDefFields::Named(ref named_fields) => {
named_fields
.iter()
.map(|(name, field)| {
let fn_arg_type = &field.type_path;
let call_arg = if field.is_boxed() {
quote! { #name: ::std::boxed::Box::new(#name) }
} else {
quote! { #name }
};
(quote!( #name: #fn_arg_type ), call_arg)
})
.unzip()
}
CompositeDefFields::NoFields => Default::default(),
CompositeDefFields::Unnamed(_) =>
abort_call_site!(
"Call variant for type {} must have all named fields",
call.ty.id()
)
};
let pallet_name = &pallet.name;
let call_struct_name = &struct_def.name;