Allow for remapping type parameters in type substitutions (#735)

* feat!: Allow for remapping type parameters in type substitutions

* chore: cargo fmt

* chore: cargo clippy

* chore: Remove some old code

* a little tidy

* address comment nit

Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
Igor Matuszewski
2023-01-19 10:49:57 +00:00
committed by GitHub
parent b7a29a5efe
commit 977f2a3333
14 changed files with 541 additions and 331 deletions
@@ -7,6 +7,7 @@ use subxt_codegen::{
CratePath,
DerivesRegistry,
RuntimeGenerator,
TypeSubstitutes,
};
fn load_test_metadata() -> frame_metadata::RuntimeMetadataPrefixed {
@@ -56,8 +57,9 @@ fn generate_runtime_interface(crate_path: CratePath) -> String {
pub mod api {}
);
let derives = DerivesRegistry::new(&crate_path);
let type_substitutes = TypeSubstitutes::new(&crate_path);
generator
.generate_runtime(item_mod, derives, crate_path)
.generate_runtime(item_mod, derives, type_substitutes, crate_path)
.to_string()
}
@@ -124,8 +126,9 @@ fn check_root_attrs_preserved() {
// Generate a runtime interface from the provided metadata.
let generator = RuntimeGenerator::new(metadata);
let derives = DerivesRegistry::new(&CratePath::default());
let type_substitutes = TypeSubstitutes::new(&CratePath::default());
let generated_code = generator
.generate_runtime(item_mod, derives, CratePath::default())
.generate_runtime(item_mod, derives, type_substitutes, CratePath::default())
.to_string();
let doc_str_loc = generated_code
+6 -5
View File
@@ -89,12 +89,13 @@ async fn run() {
r#"
#[subxt::subxt(
runtime_metadata_path = "{}",
derive_for_all_types = "Eq, PartialEq"
derive_for_all_types = "Eq, PartialEq",
substitute_type(
type = "sp_arithmetic::per_things::Perbill",
with = "::sp_runtime::Perbill"
)
)]
pub mod node_runtime {{
#[subxt(substitute_type = "sp_arithmetic::per_things::Perbill")]
use ::sp_runtime::Perbill;
}}
pub mod node_runtime {{}}
"#,
metadata_path
.to_str()
@@ -0,0 +1,67 @@
use core::marker::PhantomData;
use codec::{Decode, Encode};
use subxt::utils::AccountId32;
#[derive(Encode, Decode, Debug)]
pub struct CustomAddress(u16);
#[derive(Encode, Decode, Debug)]
pub struct Generic<T>(T);
#[derive(Encode, Decode, Debug)]
pub struct Second<T, U>(U, PhantomData<T>);
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
with = "crate::CustomAddress"
)
)]
pub mod node_runtime {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
with = "crate::Generic<A>"
)
)]
pub mod node_runtime2 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
with = "crate::Generic<B>"
)
)]
pub mod node_runtime3 {}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
substitute_type(
type = "sp_runtime::multiaddress::MultiAddress<A, B>",
with = "crate::Second<B, A>"
)
)]
pub mod node_runtime4 {}
fn main() {
// We assume Polkadot's config of MultiAddress<AccountId32, ()> here
let _ = node_runtime::tx()
.balances()
.transfer(CustomAddress(1337), 123);
let _ = node_runtime2::tx()
.balances()
.transfer(Generic(AccountId32::from([0x01;32])), 123);
let _ = node_runtime3::tx()
.balances()
.transfer(Generic(()), 123);
let _ = node_runtime4::tx()
.balances()
.transfer(Second(AccountId32::from([0x01;32]), PhantomData), 123);
}
@@ -1,7 +1,10 @@
#[subxt::subxt(runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale")]
pub mod node_runtime {
#[subxt::subxt(substitute_type = "sp_arithmetic::per_things::Perbill")]
use sp_runtime::Perbill;
}
#[subxt::subxt(
runtime_metadata_path = "../../../../artifacts/polkadot_metadata.scale",
substitute_type(
type = "sp_arithmetic::per_things::Perbill",
with = "sp_runtime::Perbill"
)
)]
pub mod node_runtime {}
fn main() {}
@@ -1,5 +1,5 @@
error: The substitute path must be a global absolute path; try prefixing with `::` or `crate`
--> src/incorrect/substitute_path_not_absolute.rs:4:9
--> src/incorrect/substitute_path_not_absolute.rs:5:16
|
4 | use sp_runtime::Perbill;
| ^^^^^^^^^^
5 | with = "sp_runtime::Perbill"
| ^^^^^^^^^^^^^^^^^^^^^