Skip to content

Commit

Permalink
Merge pull request #6 from paritytech/aj-metadata-vnext
Browse files Browse the repository at this point in the history
Aj metadata vnext
  • Loading branch information
paulormart committed Oct 29, 2021
2 parents ab132fd + c19d2e6 commit 4aaf21d
Show file tree
Hide file tree
Showing 13 changed files with 11,227 additions and 9,868 deletions.
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn codegen<I: Input>(encoded: &mut I) -> color_eyre::Result<()> {
let item_mod = syn::parse_quote!(
pub mod api {}
);
let runtime_api = generator.generate_runtime(item_mod);
let runtime_api = generator.generate_runtime(item_mod, Default::default());
println!("{}", runtime_api);
Ok(())
}
35 changes: 28 additions & 7 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod calls;
mod events;
mod storage;

use super::GeneratedTypeDerives;
use crate::{
ir,
struct_def::StructDef,
Expand All @@ -43,9 +44,16 @@ use std::{
path,
string::ToString,
};
use syn::parse_quote;
use syn::{
parse_quote,
punctuated::Punctuated,
};

pub fn generate_runtime_api<P>(item_mod: syn::ItemMod, path: P) -> TokenStream2
pub fn generate_runtime_api<P>(
item_mod: syn::ItemMod,
path: P,
generated_type_derives: Option<Punctuated<syn::Path, syn::Token![,]>>,
) -> TokenStream2
where
P: AsRef<path::Path>,
{
Expand All @@ -60,8 +68,13 @@ where
let metadata = frame_metadata::RuntimeMetadataPrefixed::decode(&mut &bytes[..])
.unwrap_or_else(|e| abort_call_site!("Failed to decode metadata: {}", e));

let mut derives = GeneratedTypeDerives::default();
if let Some(user_derives) = generated_type_derives {
derives.append(user_derives.iter().cloned())
}

let generator = RuntimeGenerator::new(metadata);
generator.generate_runtime(item_mod)
generator.generate_runtime(item_mod, derives)
}

pub struct RuntimeGenerator {
Expand All @@ -76,7 +89,11 @@ impl RuntimeGenerator {
}
}

pub fn generate_runtime(&self, item_mod: syn::ItemMod) -> TokenStream2 {
pub fn generate_runtime(
&self,
item_mod: syn::ItemMod,
derives: GeneratedTypeDerives,
) -> TokenStream2 {
let item_mod_ir = ir::ItemMod::from(item_mod);

// some hardcoded default type substitutes, can be overridden by user
Expand Down Expand Up @@ -121,8 +138,12 @@ impl RuntimeGenerator {
type_substitutes.insert(path.to_string(), substitute.clone());
}

let type_gen =
TypeGenerator::new(&self.metadata.types, "runtime_types", type_substitutes);
let type_gen = TypeGenerator::new(
&self.metadata.types,
"runtime_types",
type_substitutes,
derives.clone(),
);
let types_mod = type_gen.generate_types_mod();
let types_mod_ident = types_mod.ident();
let pallets_with_mod_names = self
Expand Down Expand Up @@ -179,7 +200,7 @@ impl RuntimeGenerator {
});

let outer_event = quote! {
#[derive(Debug, Eq, PartialEq, ::codec::Encode, ::codec::Decode)]
#derives
pub enum Event {
#( #outer_event_variants )*
}
Expand Down
52 changes: 52 additions & 0 deletions codegen/src/derives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

use syn::punctuated::Punctuated;

#[derive(Debug, Clone)]
pub struct GeneratedTypeDerives {
derives: Punctuated<syn::Path, syn::Token![,]>,
}

impl GeneratedTypeDerives {
pub fn new(derives: Punctuated<syn::Path, syn::Token!(,)>) -> Self {
Self { derives }
}

pub fn append(&mut self, derives: impl Iterator<Item = syn::Path>) {
for derive in derives {
self.derives.push(derive)
}
}
}

impl Default for GeneratedTypeDerives {
fn default() -> Self {
let mut derives = Punctuated::new();
derives.push(syn::parse_quote!(::subxt::codec::Encode));
derives.push(syn::parse_quote!(::subxt::codec::Decode));
Self::new(derives)
}
}

impl quote::ToTokens for GeneratedTypeDerives {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let derives = &self.derives;
tokens.extend(quote::quote! {
#[derive(#derives)]
})
}
}
10 changes: 7 additions & 3 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
//! Library to generate an API for a Substrate runtime from its metadata.

mod api;
mod derives;
mod ir;
mod struct_def;
mod types;

pub use self::api::{
generate_runtime_api,
RuntimeGenerator,
pub use self::{
api::{
generate_runtime_api,
RuntimeGenerator,
},
derives::GeneratedTypeDerives,
};
10 changes: 8 additions & 2 deletions codegen/src/struct_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

use super::GeneratedTypeDerives;
use crate::types::{
TypeGenerator,
TypePath,
Expand All @@ -32,6 +33,7 @@ pub struct StructDef {
pub name: syn::Ident,
pub fields: StructDefFields,
pub field_visibility: Option<syn::Visibility>,
pub derives: GeneratedTypeDerives,
}

#[derive(Debug)]
Expand Down Expand Up @@ -83,10 +85,13 @@ impl StructDef {
)
};

let derives = type_gen.derives().clone();

Self {
name,
fields,
field_visibility,
derives,
}
}

Expand All @@ -102,6 +107,7 @@ impl StructDef {
impl quote::ToTokens for StructDef {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let visibility = &self.field_visibility;
let derives = &self.derives;
tokens.extend(match self.fields {
StructDefFields::Named(ref named_fields) => {
let fields = named_fields.iter().map(|(name, ty)| {
Expand All @@ -111,7 +117,7 @@ impl quote::ToTokens for StructDef {
});
let name = &self.name;
quote! {
#[derive(Debug, Eq, PartialEq, ::codec::Encode, ::codec::Decode)]
#derives
pub struct #name {
#( #fields ),*
}
Expand All @@ -125,7 +131,7 @@ impl quote::ToTokens for StructDef {
});
let name = &self.name;
quote! {
#[derive(Debug, Eq, PartialEq, ::codec::Encode, ::codec::Decode)]
#derives
pub struct #name (
#( #fields ),*
);
Expand Down
Loading

0 comments on commit 4aaf21d

Please sign in to comment.