Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Update to latest frame-metadata #3

Merged
merged 7 commits into from
May 6, 2021
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
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() -> Result<(), Error> {
// todo: accept these as arguments from command line and/or stdin
let mod_name = "node_runtime";
let path = {
let root = std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".into());
let root = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into());
let root_path = std::path::Path::new(&root);
root_path.join("../core/node-runtime.scale")
};
Expand Down
5 changes: 3 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ authors = ["Andrew Jones <[email protected]>"]
edition = "2018"

[dependencies]
frame-metadata = { package = "frame-metadata", git = "https://github.com/paritytech/frame-metadata", branch = "main", default-features = false, features = ["v13"] }
frame-metadata = { package = "frame-metadata", git = "https://github.com/paritytech/frame-metadata", branch = "aj-substrate", default-features = false, features = ["v13"] }
heck = "0.3.1"
proc-macro2 = "1.0"
quote = "1"
syn = { version = "1.0", features = ["parsing", "full"] }
scale = { package = "parity-scale-codec", version = "2.0", default-features = false}
scale-info = { version = "0.6.0", default-features = false, optional = true, features = ["derive", "decode"] }
scale-info = { git = "https://github.com/paritytech/scale-info", branch = "aj-substrate", default-features = false, features = ["derive", "decode"] }

thiserror = "1.0.22"

[features]
Expand Down
Binary file modified core/node-runtime.scale
Binary file not shown.
52 changes: 18 additions & 34 deletions core/src/generate_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{TokenStream2, TypeGenerator};
use frame_metadata::{v13::RuntimeMetadataV13, RuntimeMetadata, RuntimeMetadataPrefixed};
use heck::SnakeCase as _;
use quote::{format_ident, quote};
use scale_info::prelude::string::ToString;
use heck::SnakeCase as _;

pub struct RuntimeGenerator {
metadata: RuntimeMetadataV13,
Expand All @@ -20,12 +20,12 @@ impl RuntimeGenerator {
let type_gen = TypeGenerator::new(&self.metadata.types, "__runtime_types");
let types_mod = type_gen.generate_types_mod();
let types_mod_ident = types_mod.ident();
let modules = self.metadata.modules.iter().map(|module| {
let mod_name = format_ident!("{}", module.name.to_string().to_snake_case());
let calls = module
let modules = self.metadata.pallets.iter().map(|pallet| {
let mod_name = format_ident!("{}", pallet.name.to_string().to_snake_case());
let calls = pallet
.calls
.as_ref()
.unwrap_or(&Vec::new())
.map_or(&Vec::new(), |call_metadata| &call_metadata.calls)
.iter()
.map(|call| {
use heck::CamelCase as _;
Expand All @@ -45,30 +45,14 @@ impl RuntimeGenerator {
}
})
.collect::<Vec<_>>();
let event =
if let Some(ref events) = module.event {
let event_variants = events
.iter()
.map(|event| {
let name = format_ident!("{}", event.name);
let args = event.arguments.iter().map(|arg| {
type_gen.resolve_type_path(arg.ty.id(), &[])
// todo: add docs and #[compact] attr
});
quote! {
#name (#( #args ),*),
}
})
.collect::<Vec<_>>();
quote! {
#[derive(Debug, ::codec::Encode, ::codec::Decode)]
pub enum Event {
#( #event_variants )*
}
}
} else {
quote! {}
};
let event = if let Some(ref event) = pallet.event {
let event_type = type_gen.resolve_type_path(event.ty.id(), &[]);
quote! {
pub type Event = #event_type;
}
} else {
quote! {}
};

let calls = if !calls.is_empty() {
quote! {
Expand All @@ -90,12 +74,12 @@ impl RuntimeGenerator {
}
});

let outer_event_variants = self.metadata.modules.iter().filter_map(|m| {
let variant_name = format_ident!("{}", m.name);
let mod_name = format_ident!("{}", m.name.to_string().to_snake_case());
let index = proc_macro2::Literal::u8_unsuffixed(m.index);
let outer_event_variants = self.metadata.pallets.iter().filter_map(|p| {
let variant_name = format_ident!("{}", p.name);
let mod_name = format_ident!("{}", p.name.to_string().to_snake_case());
let index = proc_macro2::Literal::u8_unsuffixed(p.index);

m.event.as_ref().map(|_| {
p.event.as_ref().map(|_| {
quote! {
#[codec(index = #index)]
#variant_name(#mod_name::Event),
Expand Down
Loading