forked from paritytech/subxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from paritytech/aj-metadata-vnext
Aj metadata vnext
- Loading branch information
Showing
8 changed files
with
198 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "subxt-codegen" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
async-trait = "0.1.49" | ||
codec = { package = "parity-scale-codec", version = "2", default-features = false, features = ["derive", "full"] } | ||
darling = "0.13.0" | ||
frame-metadata = "14.0" | ||
heck = "0.3.2" | ||
proc-macro2 = "1.0.24" | ||
proc-macro-crate = "0.1.5" | ||
proc-macro-error = "1.0.4" | ||
quote = "1.0.8" | ||
syn = "1.0.58" | ||
scale-info = "1.0.0" | ||
|
||
[dev-dependencies] | ||
codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } | ||
pretty_assertions = "0.6.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2019-2021 Parity Technologies (UK) Ltd. | ||
// This file is part of substrate-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 substrate-subxt. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Library to generate an API for a Substrate runtime from its metadata. | ||
|
||
mod api; | ||
mod struct_def; | ||
mod types; | ||
|
||
pub use self::api::generate_runtime_types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2019-2021 Parity Technologies (UK) Ltd. | ||
// This file is part of substrate-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 substrate-subxt. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use crate::types::{ | ||
TypeGenerator, | ||
TypePath, | ||
}; | ||
use heck::CamelCase as _; | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use proc_macro_error::abort_call_site; | ||
use quote::{ | ||
format_ident, | ||
quote, | ||
}; | ||
use scale_info::form::PortableForm; | ||
|
||
#[derive(Debug)] | ||
pub struct StructDef { | ||
pub name: syn::Ident, | ||
pub fields: StructDefFields, | ||
pub field_visibility: Option<syn::Visibility>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum StructDefFields { | ||
Named(Vec<(syn::Ident, TypePath)>), | ||
Unnamed(Vec<TypePath>), | ||
} | ||
|
||
impl StructDef { | ||
pub fn new( | ||
ident: &str, | ||
fields: &[scale_info::Field<PortableForm>], | ||
field_visibility: Option<syn::Visibility>, | ||
type_gen: &TypeGenerator, | ||
) -> Self { | ||
let name = format_ident!("{}", ident.to_camel_case()); | ||
let fields = fields | ||
.iter() | ||
.map(|field| { | ||
let name = field.name().map(|f| format_ident!("{}", f)); | ||
let ty = type_gen.resolve_type_path(field.ty().id(), &[]); | ||
(name, ty) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let named = fields.iter().all(|(name, _)| name.is_some()); | ||
let unnamed = fields.iter().all(|(name, _)| name.is_none()); | ||
|
||
let fields = if named { | ||
StructDefFields::Named( | ||
fields | ||
.iter() | ||
.map(|(name, field)| { | ||
let name = name.as_ref().unwrap_or_else(|| { | ||
abort_call_site!("All fields should have a name") | ||
}); | ||
(name.clone(), field.clone()) | ||
}) | ||
.collect(), | ||
) | ||
} else if unnamed { | ||
StructDefFields::Unnamed( | ||
fields.iter().map(|(_, field)| field.clone()).collect(), | ||
) | ||
} else { | ||
abort_call_site!( | ||
"Struct '{}': Fields should either be all named or all unnamed.", | ||
name, | ||
) | ||
}; | ||
|
||
Self { | ||
name, | ||
fields, | ||
field_visibility, | ||
} | ||
} | ||
|
||
pub fn named_fields(&self) -> Option<&[(syn::Ident, TypePath)]> { | ||
if let StructDefFields::Named(ref fields) = self.fields { | ||
Some(fields) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
impl quote::ToTokens for StructDef { | ||
fn to_tokens(&self, tokens: &mut TokenStream2) { | ||
let visibility = &self.field_visibility; | ||
tokens.extend(match self.fields { | ||
StructDefFields::Named(ref named_fields) => { | ||
let fields = named_fields.iter().map(|(name, ty)| { | ||
let compact_attr = | ||
ty.is_compact().then(|| quote!( #[codec(compact)] )); | ||
quote! { #compact_attr #visibility #name: #ty } | ||
}); | ||
let name = &self.name; | ||
quote! { | ||
#[derive(Debug, Eq, PartialEq, ::codec::Encode, ::codec::Decode)] | ||
pub struct #name { | ||
#( #fields ),* | ||
} | ||
} | ||
} | ||
StructDefFields::Unnamed(ref unnamed_fields) => { | ||
let fields = unnamed_fields.iter().map(|ty| { | ||
let compact_attr = | ||
ty.is_compact().then(|| quote!( #[codec(compact)] )); | ||
quote! { #compact_attr #visibility #ty } | ||
}); | ||
let name = &self.name; | ||
quote! { | ||
#[derive(Debug, Eq, PartialEq, ::codec::Encode, ::codec::Decode)] | ||
pub struct #name ( | ||
#( #fields ),* | ||
); | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters