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

Retain Rust code items from mod decorated with subxt attribute #721

Merged
merged 7 commits into from
Nov 23, 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
11 changes: 7 additions & 4 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ impl RuntimeGenerator {
})
.collect::<HashMap<_, _>>();

for (path, substitute) in item_mod_ir.type_substitutes().iter() {
type_substitutes.insert(path.to_string(), substitute.clone());
}
type_substitutes.extend(item_mod_ir.type_substitutes().into_iter());

let type_gen = TypeGenerator::new(
&self.metadata.types,
Expand Down Expand Up @@ -302,7 +300,7 @@ impl RuntimeGenerator {
}
};

let mod_ident = item_mod_ir.ident;
let mod_ident = &item_mod_ir.ident;
let pallets_with_constants: Vec<_> = pallets_with_mod_names
.iter()
.filter_map(|(pallet, pallet_mod_name)| {
Expand All @@ -324,9 +322,14 @@ impl RuntimeGenerator {
})
.collect();

let rust_items = item_mod_ir.rust_items();

quote! {
#[allow(dead_code, unused_imports, non_camel_case_types)]
pub mod #mod_ident {
// Preserve any Rust items that were previously defined in the adorned module
#( #rust_items ) *

// Make it easy to access the root via `root_mod` at different levels:
use super::#mod_ident as root_mod;
// Identify the pallets composing the static metadata by name.
Expand Down
20 changes: 15 additions & 5 deletions codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ impl From<syn::ItemMod> for ItemMod {
abort!(module, "out-of-line subxt modules are not supported",)
}
};
let items = items
.into_iter()
.map(<Item as From<syn::Item>>::from)
.collect::<Vec<_>>();

Self {
vis: module.vis,
mod_token: module.mod_token,
ident: module.ident,
brace,
items,
items: items.into_iter().map(From::from).collect(),
}
}
}
Expand All @@ -57,6 +54,10 @@ impl ItemMod {
})
.collect()
}

pub fn rust_items(&self) -> impl Iterator<Item = &syn::Item> {
self.items.iter().filter_map(Item::as_rust)
}
}

#[allow(clippy::large_enum_variant)]
Expand All @@ -66,6 +67,15 @@ pub enum Item {
Subxt(SubxtItem),
}

impl Item {
pub fn as_rust(&self) -> Option<&syn::Item> {
match self {
Item::Rust(item) => Some(item),
_ => None,
}
}
}

impl From<syn::Item> for Item {
fn from(item: syn::Item) -> Self {
if let syn::Item::Use(ref use_) = item {
Expand Down
30 changes: 30 additions & 0 deletions testing/ui-tests/src/correct/rust-items-preserved.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[subxt::subxt(runtime_metadata_path = "../../../artifacts/polkadot_metadata.scale")]
pub mod node_runtime {
pub struct SomeStruct;
pub enum SomeEnum {
A,
B,
}
pub trait SomeTrait {
fn some_func(&self) -> u32;
}
impl SomeTrait for SomeStruct {
fn some_func(&self) -> u32 {
1
}
}
impl SomeTrait for SomeEnum {
fn some_func(&self) -> u32 {
2
}
}
}

fn main() {
use node_runtime::SomeTrait;

let unit = node_runtime::SomeStruct;
assert_eq!(unit.some_func(), 1);
let enumeration = node_runtime::SomeEnum::A;
assert_eq!(enumeration.some_func(), 2);
}
2 changes: 2 additions & 0 deletions testing/ui-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ fn ui_tests() {
let mut m = MetadataTestRunner::default();
let t = trybuild::TestCases::new();

t.pass("src/correct/*.rs");

// Check that storage maps with no keys are handled properly.
t.pass(&m.path_to_ui_test_for_metadata(
"storage_map_no_keys",
Expand Down