mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 19:17:58 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -17,16 +17,19 @@
|
||||
|
||||
//! Util function used by this crate.
|
||||
|
||||
use proc_macro2::{TokenStream, Span};
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
|
||||
use syn::{
|
||||
Ident, Error, Signature, Pat, PatType, FnArg, Type, token, TraitItemMethod, ItemTrait,
|
||||
TraitItem, parse_quote, spanned::Spanned, Result, Meta, NestedMeta, Lit, Attribute,
|
||||
parse_quote, spanned::Spanned, token, Attribute, Error, FnArg, Ident, ItemTrait, Lit, Meta,
|
||||
NestedMeta, Pat, PatType, Result, Signature, TraitItem, TraitItemMethod, Type,
|
||||
};
|
||||
|
||||
use proc_macro_crate::{crate_name, FoundCrate};
|
||||
|
||||
use std::{env, collections::{BTreeMap, btree_map::Entry}};
|
||||
use std::{
|
||||
collections::{btree_map::Entry, BTreeMap},
|
||||
env,
|
||||
};
|
||||
|
||||
use quote::quote;
|
||||
|
||||
@@ -53,8 +56,9 @@ impl<'a> RuntimeInterfaceFunction<'a> {
|
||||
pub fn latest_version(&self) -> (u32, &TraitItemMethod) {
|
||||
(
|
||||
self.latest_version,
|
||||
self.versions.get(&self.latest_version)
|
||||
.expect("If latest_version has a value, the key with this value is in the versions; qed")
|
||||
self.versions.get(&self.latest_version).expect(
|
||||
"If latest_version has a value, the key with this value is in the versions; qed",
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -70,9 +74,12 @@ impl<'a> RuntimeInterface<'a> {
|
||||
}
|
||||
|
||||
pub fn all_versions(&self) -> impl Iterator<Item = (u32, &TraitItemMethod)> {
|
||||
self.items.iter().flat_map(|(_, item)| item.versions.iter()).map(|(v, i)| (*v, *i))
|
||||
self.items
|
||||
.iter()
|
||||
.flat_map(|(_, item)| item.versions.iter())
|
||||
.map(|(v, i)| (*v, *i))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the include for the runtime-interface crate.
|
||||
pub fn generate_runtime_interface_include() -> TokenStream {
|
||||
@@ -88,16 +95,16 @@ pub fn generate_runtime_interface_include() -> TokenStream {
|
||||
Err(e) => {
|
||||
let err = Error::new(Span::call_site(), e).to_compile_error();
|
||||
quote!( #err )
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the access to the `sp-runtime-interface` crate.
|
||||
pub fn generate_crate_access() -> TokenStream {
|
||||
if env::var("CARGO_PKG_NAME").unwrap() == "sp-runtime-interface" {
|
||||
quote!( sp_runtime_interface )
|
||||
quote!(sp_runtime_interface)
|
||||
} else {
|
||||
quote!( proc_macro_runtime_interface )
|
||||
quote!(proc_macro_runtime_interface)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,26 +116,14 @@ pub fn create_exchangeable_host_function_ident(name: &Ident) -> Ident {
|
||||
/// Create the host function identifier for the given function name.
|
||||
pub fn create_host_function_ident(name: &Ident, version: u32, trait_name: &Ident) -> Ident {
|
||||
Ident::new(
|
||||
&format!(
|
||||
"ext_{}_{}_version_{}",
|
||||
trait_name.to_string().to_snake_case(),
|
||||
name,
|
||||
version,
|
||||
),
|
||||
&format!("ext_{}_{}_version_{}", trait_name.to_string().to_snake_case(), name, version,),
|
||||
Span::call_site(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Create the host function identifier for the given function name.
|
||||
pub fn create_function_ident_with_version(name: &Ident, version: u32) -> Ident {
|
||||
Ident::new(
|
||||
&format!(
|
||||
"{}_version_{}",
|
||||
name,
|
||||
version,
|
||||
),
|
||||
Span::call_site(),
|
||||
)
|
||||
Ident::new(&format!("{}_version_{}", name, version,), Span::call_site())
|
||||
}
|
||||
|
||||
/// Returns the function arguments of the given `Signature`, minus any `self` arguments.
|
||||
@@ -143,10 +138,8 @@ pub fn get_function_arguments<'a>(sig: &'a Signature) -> impl Iterator<Item = Pa
|
||||
.map(|(i, arg)| {
|
||||
let mut res = arg.clone();
|
||||
if let Pat::Wild(wild) = &*arg.pat {
|
||||
let ident = Ident::new(
|
||||
&format!("__runtime_interface_generated_{}_", i),
|
||||
wild.span(),
|
||||
);
|
||||
let ident =
|
||||
Ident::new(&format!("__runtime_interface_generated_{}_", i), wild.span());
|
||||
|
||||
res.pat = Box::new(parse_quote!( #ident ))
|
||||
}
|
||||
@@ -170,12 +163,10 @@ pub fn get_function_argument_types<'a>(sig: &'a Signature) -> impl Iterator<Item
|
||||
pub fn get_function_argument_types_without_ref<'a>(
|
||||
sig: &'a Signature,
|
||||
) -> impl Iterator<Item = Box<Type>> + 'a {
|
||||
get_function_arguments(sig)
|
||||
.map(|pt| pt.ty)
|
||||
.map(|ty| match *ty {
|
||||
Type::Reference(type_ref) => type_ref.elem,
|
||||
_ => ty,
|
||||
})
|
||||
get_function_arguments(sig).map(|pt| pt.ty).map(|ty| match *ty {
|
||||
Type::Reference(type_ref) => type_ref.elem,
|
||||
_ => ty,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the function argument names and types, minus any `self`. If any of the arguments
|
||||
@@ -183,11 +174,10 @@ pub fn get_function_argument_types_without_ref<'a>(
|
||||
pub fn get_function_argument_names_and_types_without_ref<'a>(
|
||||
sig: &'a Signature,
|
||||
) -> impl Iterator<Item = (Box<Pat>, Box<Type>)> + 'a {
|
||||
get_function_arguments(sig)
|
||||
.map(|pt| match *pt.ty {
|
||||
Type::Reference(type_ref) => (pt.pat, type_ref.elem),
|
||||
_ => (pt.pat, pt.ty),
|
||||
})
|
||||
get_function_arguments(sig).map(|pt| match *pt.ty {
|
||||
Type::Reference(type_ref) => (pt.pat, type_ref.elem),
|
||||
_ => (pt.pat, pt.ty),
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the `&`/`&mut` for all function argument types, minus the `self` arg. If a function
|
||||
@@ -195,23 +185,18 @@ pub fn get_function_argument_names_and_types_without_ref<'a>(
|
||||
pub fn get_function_argument_types_ref_and_mut<'a>(
|
||||
sig: &'a Signature,
|
||||
) -> impl Iterator<Item = Option<(token::And, Option<token::Mut>)>> + 'a {
|
||||
get_function_arguments(sig)
|
||||
.map(|pt| pt.ty)
|
||||
.map(|ty| match *ty {
|
||||
Type::Reference(type_ref) => Some((type_ref.and_token, type_ref.mutability)),
|
||||
_ => None,
|
||||
})
|
||||
get_function_arguments(sig).map(|pt| pt.ty).map(|ty| match *ty {
|
||||
Type::Reference(type_ref) => Some((type_ref.and_token, type_ref.mutability)),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an iterator over all trait methods for the given trait definition.
|
||||
fn get_trait_methods<'a>(trait_def: &'a ItemTrait) -> impl Iterator<Item = &'a TraitItemMethod> {
|
||||
trait_def
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|i| match i {
|
||||
TraitItem::Method(ref method) => Some(method),
|
||||
_ => None,
|
||||
})
|
||||
trait_def.items.iter().filter_map(|i| match i {
|
||||
TraitItem::Method(ref method) => Some(method),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse version attribute.
|
||||
@@ -221,36 +206,34 @@ fn parse_version_attribute(version: &Attribute) -> Result<u32> {
|
||||
let meta = version.parse_meta()?;
|
||||
|
||||
let err = Err(Error::new(
|
||||
meta.span(),
|
||||
"Unexpected `version` attribute. The supported format is `#[version(1)]`",
|
||||
)
|
||||
);
|
||||
meta.span(),
|
||||
"Unexpected `version` attribute. The supported format is `#[version(1)]`",
|
||||
));
|
||||
|
||||
match meta {
|
||||
Meta::List(list) => {
|
||||
Meta::List(list) =>
|
||||
if list.nested.len() != 1 {
|
||||
err
|
||||
} else if let Some(NestedMeta::Lit(Lit::Int(i))) = list.nested.first() {
|
||||
i.base10_parse()
|
||||
} else {
|
||||
err
|
||||
}
|
||||
},
|
||||
},
|
||||
_ => err,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return item version (`#[version(X)]`) attribute, if present.
|
||||
fn get_item_version(item: &TraitItemMethod) -> Result<Option<u32>> {
|
||||
item.attrs.iter().find(|attr| attr.path.is_ident("version"))
|
||||
item.attrs
|
||||
.iter()
|
||||
.find(|attr| attr.path.is_ident("version"))
|
||||
.map(|attr| parse_version_attribute(attr))
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Returns all runtime interface members, with versions.
|
||||
pub fn get_runtime_interface<'a>(trait_def: &'a ItemTrait)
|
||||
-> Result<RuntimeInterface<'a>>
|
||||
{
|
||||
pub fn get_runtime_interface<'a>(trait_def: &'a ItemTrait) -> Result<RuntimeInterface<'a>> {
|
||||
let mut functions: BTreeMap<syn::Ident, RuntimeInterfaceFunction<'a>> = BTreeMap::new();
|
||||
|
||||
for item in get_trait_methods(trait_def) {
|
||||
@@ -258,25 +241,26 @@ pub fn get_runtime_interface<'a>(trait_def: &'a ItemTrait)
|
||||
let version = get_item_version(item)?.unwrap_or(1);
|
||||
|
||||
match functions.entry(name.clone()) {
|
||||
Entry::Vacant(entry) => { entry.insert(RuntimeInterfaceFunction::new(version, item)); },
|
||||
Entry::Vacant(entry) => {
|
||||
entry.insert(RuntimeInterfaceFunction::new(version, item));
|
||||
},
|
||||
Entry::Occupied(mut entry) => {
|
||||
if let Some(existing_item) = entry.get().versions.get(&version) {
|
||||
let mut err = Error::new(
|
||||
item.span(),
|
||||
"Duplicated version attribute",
|
||||
);
|
||||
let mut err = Error::new(item.span(), "Duplicated version attribute");
|
||||
err.combine(Error::new(
|
||||
existing_item.span(),
|
||||
"Previous version with the same number defined here",
|
||||
));
|
||||
|
||||
return Err(err);
|
||||
return Err(err)
|
||||
}
|
||||
|
||||
let interface_item = entry.get_mut();
|
||||
if interface_item.latest_version < version { interface_item.latest_version = version; }
|
||||
if interface_item.latest_version < version {
|
||||
interface_item.latest_version = version;
|
||||
}
|
||||
interface_item.versions.insert(version, item);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,8 +270,11 @@ pub fn get_runtime_interface<'a>(trait_def: &'a ItemTrait)
|
||||
if next_expected != *version {
|
||||
return Err(Error::new(
|
||||
item.span(),
|
||||
format!("Unexpected version attribute: missing version '{}' for this function", next_expected),
|
||||
));
|
||||
format!(
|
||||
"Unexpected version attribute: missing version '{}' for this function",
|
||||
next_expected
|
||||
),
|
||||
))
|
||||
}
|
||||
next_expected += 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user