mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-22 14:58:04 +00:00
Remove codec::Encode and codec::Decode derives from generated APIs by default (#2008)
* Remove codec::Encode and codec::Decode from generated APIs by default * clippy fixes * clippy * More fixes, and CompactAs only if Encode/Decode * revert println in example * fix lightclient test * fix docs * Fix another rust doc comment * Fix failing storage test * Remove now-unnecessary test * clippy * clippy * Remove pointless clone
This commit is contained in:
@@ -6,7 +6,7 @@ use heck::{ToSnakeCase as _, ToUpperCamelCase};
|
||||
use proc_macro2::{Ident, TokenStream as TokenStream2, TokenStream};
|
||||
use quote::{format_ident, quote};
|
||||
use scale_info::TypeDef;
|
||||
use scale_typegen::{TypeGenerator, typegen::type_path::TypePath};
|
||||
use scale_typegen::TypeGenerator;
|
||||
use subxt_metadata::{
|
||||
PalletMetadata, StorageEntryMetadata, StorageEntryModifier, StorageEntryType, StorageHasher,
|
||||
};
|
||||
@@ -92,7 +92,7 @@ fn generate_storage_entry_fns(
|
||||
.expect("type is in metadata; qed");
|
||||
|
||||
let alias_name = format_ident!("Param{}", idx);
|
||||
let alias_type = primitive_type_alias(&ty_path, type_gen.settings());
|
||||
let alias_type = ty_path.to_token_stream(type_gen.settings());
|
||||
|
||||
let alias_type_def = quote!( pub type #alias_name = #alias_type; );
|
||||
let alias_type_path = quote!( types::#alias_module_name::#alias_name );
|
||||
@@ -206,7 +206,7 @@ fn generate_storage_entry_fns(
|
||||
let key = &keys_slice[0];
|
||||
if key.hasher.ends_with_key() {
|
||||
let arg = &key.arg_name;
|
||||
let keys = quote!(#static_storage_key::new(#arg.borrow()));
|
||||
let keys = quote!(#static_storage_key::new(#arg));
|
||||
let path = &key.alias_type_path;
|
||||
let path = quote!(#static_storage_key<#path>);
|
||||
(keys, path)
|
||||
@@ -220,7 +220,7 @@ fn generate_storage_entry_fns(
|
||||
arg_name, hasher, ..
|
||||
}| {
|
||||
if hasher.ends_with_key() {
|
||||
quote!( #static_storage_key::new(#arg_name.borrow()) )
|
||||
quote!( #static_storage_key::new(#arg_name) )
|
||||
} else {
|
||||
quote!(())
|
||||
}
|
||||
@@ -250,7 +250,7 @@ fn generate_storage_entry_fns(
|
||||
arg_name,
|
||||
alias_type_path,
|
||||
..
|
||||
}| quote!( #arg_name: impl ::core::borrow::Borrow<#alias_type_path> ),
|
||||
}| quote!( #arg_name: #alias_type_path ),
|
||||
);
|
||||
|
||||
quote!(
|
||||
@@ -300,34 +300,14 @@ fn generate_storage_entry_fns(
|
||||
))
|
||||
}
|
||||
|
||||
fn primitive_type_alias(
|
||||
type_path: &TypePath,
|
||||
settings: &scale_typegen::TypeGeneratorSettings,
|
||||
) -> TokenStream {
|
||||
// Vec<T> is cast to [T]
|
||||
if let Some(ty) = type_path.vec_type_param() {
|
||||
let ty = ty.to_token_stream(settings);
|
||||
return quote!([#ty]);
|
||||
}
|
||||
// String is cast to str
|
||||
if type_path.is_string() {
|
||||
return quote!(::core::primitive::str);
|
||||
}
|
||||
type_path.to_token_stream(settings)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::RuntimeGenerator;
|
||||
use frame_metadata::v15;
|
||||
use heck::ToUpperCamelCase;
|
||||
use quote::{format_ident, quote};
|
||||
use scale_info::{MetaType, meta_type};
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use subxt_metadata::Metadata;
|
||||
|
||||
// TODO: Think about adding tests for storage codegen which can use this sort of function.
|
||||
#[allow(dead_code)]
|
||||
fn metadata_with_storage_entries(
|
||||
storage_entries: impl IntoIterator<Item = (&'static str, MetaType)>,
|
||||
) -> Metadata {
|
||||
@@ -387,68 +367,4 @@ mod tests {
|
||||
.expect("can build valid metadata");
|
||||
metadata
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn borrow_type_replacements() {
|
||||
let storage_entries = [
|
||||
("vector", meta_type::<Vec<u8>>()),
|
||||
("boxed", meta_type::<Box<u16>>()),
|
||||
("string", meta_type::<String>()),
|
||||
("static_string", meta_type::<&'static str>()),
|
||||
("cow_string", meta_type::<Cow<'_, str>>()),
|
||||
];
|
||||
|
||||
let expected_borrowed_types = [
|
||||
quote!([::core::primitive::u8]),
|
||||
quote!(::core::primitive::u16),
|
||||
quote!(::core::primitive::str),
|
||||
quote!(::core::primitive::str),
|
||||
quote!(::core::primitive::str),
|
||||
];
|
||||
|
||||
let metadata = metadata_with_storage_entries(storage_entries);
|
||||
|
||||
let item_mod = syn::parse_quote!(
|
||||
pub mod api {}
|
||||
);
|
||||
let generator = RuntimeGenerator::new(metadata);
|
||||
let generated = generator
|
||||
.generate_runtime(
|
||||
item_mod,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
syn::parse_str("::subxt_path").unwrap(),
|
||||
false,
|
||||
)
|
||||
.expect("should be able to generate runtime");
|
||||
let generated_str = generated.to_string();
|
||||
|
||||
for ((name, _), expected_type) in storage_entries
|
||||
.into_iter()
|
||||
.zip(expected_borrowed_types.into_iter())
|
||||
{
|
||||
let name_ident = format_ident!("{}", name);
|
||||
let expected_storage_constructor = quote!(
|
||||
fn #name_ident(
|
||||
&self,
|
||||
_0: impl ::core::borrow::Borrow<types::#name_ident::Param0>,
|
||||
)
|
||||
);
|
||||
dbg!(&generated_str);
|
||||
dbg!(&expected_storage_constructor.to_string());
|
||||
assert!(generated_str.contains(&expected_storage_constructor.to_string()));
|
||||
|
||||
let alias_name = format_ident!("{}", name.to_upper_camel_case());
|
||||
let expected_alias_module = quote!(
|
||||
pub mod #name_ident {
|
||||
use super::runtime_types;
|
||||
|
||||
pub type #alias_name = ::core::primitive::bool;
|
||||
pub type Param0 = #expected_type;
|
||||
}
|
||||
);
|
||||
|
||||
assert!(generated_str.contains(&expected_alias_module.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user