Skip to content

Commit

Permalink
cargo fmt --all
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Sep 19, 2024
1 parent b4b7175 commit f0d85ac
Show file tree
Hide file tree
Showing 83 changed files with 14,810 additions and 13,920 deletions.
2,117 changes: 1,097 additions & 1,020 deletions support/procedural-fork/src/benchmark.rs

Large diffs are not rendered by default.

393 changes: 198 additions & 195 deletions support/procedural-fork/src/construct_runtime/expand/call.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,82 +20,82 @@ use proc_macro2::{Ident, TokenStream};
use quote::quote;

pub(crate) fn expand_conversion_fn(
composite_name: &str,
path: &PalletPath,
instance: Option<&Ident>,
variant_name: &Ident,
composite_name: &str,
path: &PalletPath,
instance: Option<&Ident>,
variant_name: &Ident,
) -> TokenStream {
let composite_name = quote::format_ident!("{}", composite_name);
let runtime_composite_name = quote::format_ident!("Runtime{}", composite_name);
let composite_name = quote::format_ident!("{}", composite_name);
let runtime_composite_name = quote::format_ident!("Runtime{}", composite_name);

if let Some(inst) = instance {
quote! {
impl From<#path::#composite_name<#path::#inst>> for #runtime_composite_name {
fn from(hr: #path::#composite_name<#path::#inst>) -> Self {
#runtime_composite_name::#variant_name(hr)
}
}
}
} else {
quote! {
impl From<#path::#composite_name> for #runtime_composite_name {
fn from(hr: #path::#composite_name) -> Self {
#runtime_composite_name::#variant_name(hr)
}
}
}
}
if let Some(inst) = instance {
quote! {
impl From<#path::#composite_name<#path::#inst>> for #runtime_composite_name {
fn from(hr: #path::#composite_name<#path::#inst>) -> Self {
#runtime_composite_name::#variant_name(hr)
}
}
}
} else {
quote! {
impl From<#path::#composite_name> for #runtime_composite_name {
fn from(hr: #path::#composite_name) -> Self {
#runtime_composite_name::#variant_name(hr)
}
}
}
}
}

pub(crate) fn expand_variant(
composite_name: &str,
index: u8,
path: &PalletPath,
instance: Option<&Ident>,
variant_name: &Ident,
composite_name: &str,
index: u8,
path: &PalletPath,
instance: Option<&Ident>,
variant_name: &Ident,
) -> TokenStream {
let composite_name = quote::format_ident!("{}", composite_name);
let composite_name = quote::format_ident!("{}", composite_name);

if let Some(inst) = instance {
quote! {
#[codec(index = #index)]
#variant_name(#path::#composite_name<#path::#inst>),
}
} else {
quote! {
#[codec(index = #index)]
#variant_name(#path::#composite_name),
}
}
if let Some(inst) = instance {
quote! {
#[codec(index = #index)]
#variant_name(#path::#composite_name<#path::#inst>),
}
} else {
quote! {
#[codec(index = #index)]
#variant_name(#path::#composite_name),
}
}
}

pub(crate) fn expand_variant_count(
composite_name: &str,
path: &PalletPath,
instance: Option<&Ident>,
composite_name: &str,
path: &PalletPath,
instance: Option<&Ident>,
) -> TokenStream {
let composite_name = quote::format_ident!("{}", composite_name);
let composite_name = quote::format_ident!("{}", composite_name);

if let Some(inst) = instance {
quote! {
#path::#composite_name::<#path::#inst>::VARIANT_COUNT
}
} else {
// Wrapped `<`..`>` means: use default type parameter for enum.
//
// This is used for pallets without instance support or pallets with instance support when
// we don't specify instance:
//
// ```
// pub struct Pallet<T, I = ()>{..}
//
// #[pallet::composite_enum]
// pub enum HoldReason<I: 'static = ()> {..}
//
// Pallet1: pallet_x, // <- default type parameter
// ```
quote! {
<#path::#composite_name>::VARIANT_COUNT
}
}
if let Some(inst) = instance {
quote! {
#path::#composite_name::<#path::#inst>::VARIANT_COUNT
}
} else {
// Wrapped `<`..`>` means: use default type parameter for enum.
//
// This is used for pallets without instance support or pallets with instance support when
// we don't specify instance:
//
// ```
// pub struct Pallet<T, I = ()>{..}
//
// #[pallet::composite_enum]
// pub enum HoldReason<I: 'static = ()> {..}
//
// Pallet1: pallet_x, // <- default type parameter
// ```
quote! {
<#path::#composite_name>::VARIANT_COUNT
}
}
}
208 changes: 109 additions & 99 deletions support/procedural-fork/src/construct_runtime/expand/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,125 +23,135 @@ use std::str::FromStr;
use syn::Ident;

pub fn expand_outer_config(
runtime: &Ident,
pallet_decls: &[Pallet],
scrate: &TokenStream,
runtime: &Ident,
pallet_decls: &[Pallet],
scrate: &TokenStream,
) -> TokenStream {
let mut types = TokenStream::new();
let mut fields = TokenStream::new();
let mut genesis_build_calls = TokenStream::new();
let mut query_genesis_config_part_macros = Vec::new();
let mut types = TokenStream::new();
let mut fields = TokenStream::new();
let mut genesis_build_calls = TokenStream::new();
let mut query_genesis_config_part_macros = Vec::new();

for decl in pallet_decls {
if let Some(pallet_entry) = decl.find_part("Config") {
let path = &decl.path;
let pallet_name = &decl.name;
let path_str = path.into_token_stream().to_string();
let config = format_ident!("{}Config", pallet_name);
let field_name =
&Ident::new(&pallet_name.to_string().to_snake_case(), decl.name.span());
let part_is_generic = !pallet_entry.generics.params.is_empty();
let attr = &decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
quote! {
#acc
#attr
}
});
for decl in pallet_decls {
if let Some(pallet_entry) = decl.find_part("Config") {
let path = &decl.path;
let pallet_name = &decl.name;
let path_str = path.into_token_stream().to_string();
let config = format_ident!("{}Config", pallet_name);
let field_name =
&Ident::new(&pallet_name.to_string().to_snake_case(), decl.name.span());
let part_is_generic = !pallet_entry.generics.params.is_empty();
let attr = &decl
.cfg_pattern
.iter()
.fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
quote! {
#acc
#attr
}
});

types.extend(expand_config_types(attr, runtime, decl, &config, part_is_generic));
fields.extend(quote!(#attr pub #field_name: #config,));
genesis_build_calls
.extend(expand_config_build_storage_call(scrate, &config, attr, field_name));
query_genesis_config_part_macros.push(quote! {
types.extend(expand_config_types(
attr,
runtime,
decl,
&config,
part_is_generic,
));
fields.extend(quote!(#attr pub #field_name: #config,));
genesis_build_calls.extend(expand_config_build_storage_call(
scrate, &config, attr, field_name,
));
query_genesis_config_part_macros.push(quote! {
#path::__substrate_genesis_config_check::is_genesis_config_defined!(#pallet_name);
#[cfg(feature = "std")]
#path::__substrate_genesis_config_check::is_std_enabled_for_genesis!(#pallet_name, #path_str);
});
}
}
}
}

quote! {
#( #query_genesis_config_part_macros )*
quote! {
#( #query_genesis_config_part_macros )*

#types
#types

use #scrate::__private::serde as __genesis_config_serde_import__;
#[derive(#scrate::__private::serde::Serialize, #scrate::__private::serde::Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[serde(crate = "__genesis_config_serde_import__")]
pub struct RuntimeGenesisConfig {
#fields
}
use #scrate::__private::serde as __genesis_config_serde_import__;
#[derive(#scrate::__private::serde::Serialize, #scrate::__private::serde::Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[serde(crate = "__genesis_config_serde_import__")]
pub struct RuntimeGenesisConfig {
#fields
}

#[cfg(any(feature = "std", test))]
impl #scrate::sp_runtime::BuildStorage for RuntimeGenesisConfig {
fn assimilate_storage(
&self,
storage: &mut #scrate::sp_runtime::Storage,
) -> std::result::Result<(), String> {
#scrate::__private::BasicExternalities::execute_with_storage(storage, || {
<Self as #scrate::traits::BuildGenesisConfig>::build(&self);
Ok(())
})
}
}
#[cfg(any(feature = "std", test))]
impl #scrate::sp_runtime::BuildStorage for RuntimeGenesisConfig {
fn assimilate_storage(
&self,
storage: &mut #scrate::sp_runtime::Storage,
) -> std::result::Result<(), String> {
#scrate::__private::BasicExternalities::execute_with_storage(storage, || {
<Self as #scrate::traits::BuildGenesisConfig>::build(&self);
Ok(())
})
}
}

impl #scrate::traits::BuildGenesisConfig for RuntimeGenesisConfig {
fn build(&self) {
#genesis_build_calls
<AllPalletsWithSystem as #scrate::traits::OnGenesis>::on_genesis();
}
}
impl #scrate::traits::BuildGenesisConfig for RuntimeGenesisConfig {
fn build(&self) {
#genesis_build_calls
<AllPalletsWithSystem as #scrate::traits::OnGenesis>::on_genesis();
}
}

/// Test the `Default` derive impl of the `RuntimeGenesisConfig`.
#[cfg(test)]
#[test]
fn test_genesis_config_builds() {
#scrate::__private::sp_io::TestExternalities::default().execute_with(|| {
<RuntimeGenesisConfig as #scrate::traits::BuildGenesisConfig>::build(
&RuntimeGenesisConfig::default()
);
});
}
}
/// Test the `Default` derive impl of the `RuntimeGenesisConfig`.
#[cfg(test)]
#[test]
fn test_genesis_config_builds() {
#scrate::__private::sp_io::TestExternalities::default().execute_with(|| {
<RuntimeGenesisConfig as #scrate::traits::BuildGenesisConfig>::build(
&RuntimeGenesisConfig::default()
);
});
}
}
}

fn expand_config_types(
attr: &TokenStream,
runtime: &Ident,
decl: &Pallet,
config: &Ident,
part_is_generic: bool,
attr: &TokenStream,
runtime: &Ident,
decl: &Pallet,
config: &Ident,
part_is_generic: bool,
) -> TokenStream {
let path = &decl.path;
let path = &decl.path;

match (decl.instance.as_ref(), part_is_generic) {
(Some(inst), true) => quote! {
#attr
pub type #config = #path::GenesisConfig<#runtime, #path::#inst>;
},
(None, true) => quote! {
#attr
pub type #config = #path::GenesisConfig<#runtime>;
},
(_, false) => quote! {
#attr
pub type #config = #path::GenesisConfig;
},
}
match (decl.instance.as_ref(), part_is_generic) {
(Some(inst), true) => quote! {
#attr
pub type #config = #path::GenesisConfig<#runtime, #path::#inst>;
},
(None, true) => quote! {
#attr
pub type #config = #path::GenesisConfig<#runtime>;
},
(_, false) => quote! {
#attr
pub type #config = #path::GenesisConfig;
},
}
}

fn expand_config_build_storage_call(
scrate: &TokenStream,
pallet_genesis_config: &Ident,
attr: &TokenStream,
field_name: &Ident,
scrate: &TokenStream,
pallet_genesis_config: &Ident,
attr: &TokenStream,
field_name: &Ident,
) -> TokenStream {
quote! {
#attr
<#pallet_genesis_config as #scrate::traits::BuildGenesisConfig>::build(&self.#field_name);
}
quote! {
#attr
<#pallet_genesis_config as #scrate::traits::BuildGenesisConfig>::build(&self.#field_name);
}
}
Loading

0 comments on commit f0d85ac

Please sign in to comment.