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:
James Wilson
2025-05-29 14:43:42 +01:00
committed by GitHub
parent 0473cfd292
commit ed25a3ac26
22 changed files with 627 additions and 8974 deletions
+24 -8
View File
@@ -312,15 +312,35 @@ fn subxt_type_gen_settings(
crate_path: &syn::Path,
should_gen_docs: bool,
) -> TypeGeneratorSettings {
// If we're using codec::Encode or codec::Decode derives, then we want to
// output #[codec(index = N)] and #[codec(compact)] attrs, else we don't.
let insert_codec_attributes = derives.default_derives().derives().iter().any(|path| {
let mut segments_backwards = path.segments.iter().rev();
let ident = segments_backwards.next();
let module = segments_backwards.next();
let is_ident_match = ident.is_some_and(|s| s.ident == "Encode" || s.ident == "Decode");
let is_module_match = module.is_some_and(|s| s.ident == "codec");
is_ident_match && is_module_match
});
// If we're inserting the codec attributes, we also should use `CompactAs` where necessary.
let compact_as_type_path = if insert_codec_attributes {
Some(parse_quote!(#crate_path::ext::codec::CompactAs))
} else {
None
};
TypeGeneratorSettings {
types_mod_ident: parse_quote!(runtime_types),
should_gen_docs,
derives,
substitutes,
decoded_bits_type_path: Some(parse_quote!(#crate_path::utils::bits::DecodedBits)),
compact_as_type_path: Some(parse_quote!(#crate_path::ext::codec::CompactAs)),
compact_as_type_path,
compact_type_path: Some(parse_quote!(#crate_path::ext::codec::Compact)),
insert_codec_attributes: true,
insert_codec_attributes,
alloc_crate_path: AllocCratePath::Custom(parse_quote!(#crate_path::alloc)),
}
}
@@ -329,19 +349,15 @@ fn default_derives(crate_path: &syn::Path) -> DerivesRegistry {
let encode_crate_path = quote::quote! { #crate_path::ext::scale_encode }.to_string();
let decode_crate_path = quote::quote! { #crate_path::ext::scale_decode }.to_string();
let derives: [syn::Path; 5] = [
let derives: [syn::Path; 3] = [
parse_quote!(#crate_path::ext::scale_encode::EncodeAsType),
parse_quote!(#crate_path::ext::scale_decode::DecodeAsType),
parse_quote!(#crate_path::ext::codec::Encode),
parse_quote!(#crate_path::ext::codec::Decode),
parse_quote!(Debug),
];
let attributes: [syn::Attribute; 4] = [
let attributes: [syn::Attribute; 2] = [
parse_quote!(#[encode_as_type(crate_path = #encode_crate_path)]),
parse_quote!(#[decode_as_type(crate_path = #decode_crate_path)]),
parse_quote!(#[codec(crate = #crate_path::ext::codec)]),
parse_quote!(#[codec(dumb_trait_bound)]),
];
let mut derives_registry = DerivesRegistry::new();