mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 05:47:58 +00:00
Use pallet names to name enum variants (#8990)
* Remove unused event_metadata variable * Eliminate mod_name and use pallet names to name enum variants * Rename field name `pallet` to `path` * Use only the pallet name to generate variant names * Use new naming scheme for Event enum in sudo pallet tests * Use new naming scheme for Event enum in offences pallet tests * Use new naming scheme for Event enum in contracts pallet tests * Use new naming scheme for Event enum in collective pallet tests * Use new naming scheme for Event enum in bounties pallet tests * Use new naming scheme for Event enum in balances pallet tests * Use new naming scheme for Event enum in assets pallet tests * Use new naming scheme for Event enum in frame support tests * Use new naming scheme for Event enum in tips pallet tests * Use new naming scheme for Event enum in transaction payment pallet tests * Use new naming scheme for GenesisConfig fields in example pallet tests * Use new naming scheme for GenesisConfig fields in elections pallet tests * Use new naming scheme for Event enum in election provider multi-phase pallet tests * Use new naming scheme for Event enum in elections phragmen pallet tests * Use new naming scheme for GenesisConfig fields in chain spec * Use new naming scheme for Event enum in staking pallet mock * Use new naming scheme for GenesisConfig fields in node-template chain spec * Use new naming scheme for GenesisConfig fields in node-testing chain spec * Use new naming scheme for Event enum in node executor tests * Use new naming scheme for GenesisConfig fields in transaction storage pallet mock * Refactor match conditions
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
// limitations under the License
|
||||
|
||||
use crate::construct_runtime::Pallet;
|
||||
use inflector::Inflector;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::Ident;
|
||||
@@ -32,12 +33,8 @@ pub fn expand_outer_config(
|
||||
for decl in pallet_decls {
|
||||
if let Some(pallet_entry) = decl.find_part("Config") {
|
||||
let config = format_ident!("{}Config", decl.name);
|
||||
let mod_name = decl.pallet.mod_name();
|
||||
let field_name = if let Some(inst) = decl.instance.as_ref() {
|
||||
format_ident!("{}_{}", mod_name, inst)
|
||||
} else {
|
||||
mod_name
|
||||
};
|
||||
let pallet_name = &decl.name.to_string().to_snake_case();
|
||||
let field_name = &Ident::new(pallet_name, decl.name.span());
|
||||
let part_is_generic = !pallet_entry.generics.params.is_empty();
|
||||
|
||||
types.extend(expand_config_types(runtime, decl, &config, part_is_generic));
|
||||
@@ -56,7 +53,6 @@ pub fn expand_outer_config(
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
#[serde(crate = "__genesis_config_serde_import__")]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct GenesisConfig {
|
||||
#fields
|
||||
}
|
||||
@@ -85,7 +81,7 @@ fn expand_config_types(
|
||||
config: &Ident,
|
||||
part_is_generic: bool,
|
||||
) -> TokenStream {
|
||||
let path = &decl.pallet;
|
||||
let path = &decl.path;
|
||||
|
||||
match (decl.instance.as_ref(), part_is_generic) {
|
||||
(Some(inst), true) => quote!{
|
||||
@@ -109,7 +105,7 @@ fn expand_config_build_storage_call(
|
||||
decl: &Pallet,
|
||||
field_name: &Ident,
|
||||
) -> TokenStream {
|
||||
let path = &decl.pallet;
|
||||
let path = &decl.path;
|
||||
let instance = if let Some(inst) = decl.instance.as_ref() {
|
||||
quote!(#path::#inst)
|
||||
} else {
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License
|
||||
|
||||
use crate::construct_runtime::{Pallet, parse::PalletPath};
|
||||
use crate::construct_runtime::Pallet;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use quote::quote;
|
||||
use syn::{Generics, Ident};
|
||||
|
||||
pub fn expand_outer_event(
|
||||
@@ -27,11 +27,10 @@ pub fn expand_outer_event(
|
||||
) -> syn::Result<TokenStream> {
|
||||
let mut event_variants = TokenStream::new();
|
||||
let mut event_conversions = TokenStream::new();
|
||||
let mut events_metadata = TokenStream::new();
|
||||
|
||||
for pallet_decl in pallet_decls {
|
||||
if let Some(pallet_entry) = pallet_decl.find_part("Event") {
|
||||
let path = &pallet_decl.pallet;
|
||||
let path = &pallet_decl.path;
|
||||
let index = pallet_decl.index;
|
||||
let instance = pallet_decl.instance.as_ref();
|
||||
let generics = &pallet_entry.generics;
|
||||
@@ -53,9 +52,8 @@ pub fn expand_outer_event(
|
||||
(None, false) => quote!(#path::Event),
|
||||
};
|
||||
|
||||
event_variants.extend(expand_event_variant(runtime, path, index, instance, generics));
|
||||
event_conversions.extend(expand_event_conversion(scrate, path, instance, &pallet_event));
|
||||
events_metadata.extend(expand_event_metadata(scrate, path, &pallet_event));
|
||||
event_variants.extend(expand_event_variant(runtime, pallet_decl, index, instance, generics));
|
||||
event_conversions.extend(expand_event_conversion(scrate, pallet_decl, &pallet_event));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,49 +75,42 @@ pub fn expand_outer_event(
|
||||
|
||||
fn expand_event_variant(
|
||||
runtime: &Ident,
|
||||
path: &PalletPath,
|
||||
pallet: &Pallet,
|
||||
index: u8,
|
||||
instance: Option<&Ident>,
|
||||
generics: &Generics,
|
||||
) -> TokenStream {
|
||||
let path = &pallet.path;
|
||||
let variant_name = &pallet.name;
|
||||
let part_is_generic = !generics.params.is_empty();
|
||||
let mod_name = &path.mod_name();
|
||||
|
||||
match (instance, part_is_generic) {
|
||||
(Some(inst), true) => {
|
||||
let variant = format_ident!("{}_{}", mod_name, inst);
|
||||
quote!(#[codec(index = #index)] #variant(#path::Event<#runtime, #path::#inst>),)
|
||||
match instance {
|
||||
Some(inst) if part_is_generic => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Event<#runtime, #path::#inst>),)
|
||||
}
|
||||
(Some(inst), false) => {
|
||||
let variant = format_ident!("{}_{}", mod_name, inst);
|
||||
quote!(#[codec(index = #index)] #variant(#path::Event<#path::#inst>),)
|
||||
Some(inst) => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Event<#path::#inst>),)
|
||||
}
|
||||
(None, true) => {
|
||||
quote!(#[codec(index = #index)] #mod_name(#path::Event<#runtime>),)
|
||||
None if part_is_generic => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Event<#runtime>),)
|
||||
}
|
||||
(None, false) => {
|
||||
quote!(#[codec(index = #index)] #mod_name(#path::Event),)
|
||||
None => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Event),)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_event_conversion(
|
||||
scrate: &TokenStream,
|
||||
path: &PalletPath,
|
||||
instance: Option<&Ident>,
|
||||
pallet: &Pallet,
|
||||
pallet_event: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let mod_name = path.mod_name();
|
||||
let variant = if let Some(inst) = instance {
|
||||
format_ident!("{}_{}", mod_name, inst)
|
||||
} else {
|
||||
mod_name
|
||||
};
|
||||
let variant_name = &pallet.name;
|
||||
|
||||
quote!{
|
||||
impl From<#pallet_event> for Event {
|
||||
fn from(x: #pallet_event) -> Self {
|
||||
Event::#variant(x)
|
||||
Event::#variant_name(x)
|
||||
}
|
||||
}
|
||||
impl #scrate::sp_std::convert::TryInto<#pallet_event> for Event {
|
||||
@@ -127,20 +118,10 @@ fn expand_event_conversion(
|
||||
|
||||
fn try_into(self) -> #scrate::sp_std::result::Result<#pallet_event, Self::Error> {
|
||||
match self {
|
||||
Self::#variant(evt) => Ok(evt),
|
||||
Self::#variant_name(evt) => Ok(evt),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_event_metadata(
|
||||
scrate: &TokenStream,
|
||||
path: &PalletPath,
|
||||
pallet_event: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let mod_name = path.mod_name();
|
||||
|
||||
quote!{(stringify!(#mod_name), #scrate::event::FnEncode(#pallet_event::metadata)),}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ fn expand_pallet_metadata_storage(
|
||||
) -> TokenStream {
|
||||
if filtered_names.contains(&"Storage") {
|
||||
let instance = decl.instance.as_ref().into_iter();
|
||||
let path = &decl.pallet;
|
||||
let path = &decl.path;
|
||||
|
||||
quote!{
|
||||
Some(#scrate::metadata::DecodeDifferent::Encode(
|
||||
@@ -114,7 +114,7 @@ fn expand_pallet_metadata_calls(
|
||||
) -> TokenStream {
|
||||
if filtered_names.contains(&"Call") {
|
||||
let instance = decl.instance.as_ref().into_iter();
|
||||
let path = &decl.pallet;
|
||||
let path = &decl.path;
|
||||
|
||||
quote!{
|
||||
Some(#scrate::metadata::DecodeDifferent::Encode(
|
||||
@@ -135,7 +135,7 @@ fn expand_pallet_metadata_events(
|
||||
decl: &Pallet,
|
||||
) -> TokenStream {
|
||||
if filtered_names.contains(&"Event") {
|
||||
let path = &decl.pallet;
|
||||
let path = &decl.path;
|
||||
let part_is_generic =
|
||||
!decl.find_part("Event").expect("Event part exists; qed").generics.params.is_empty();
|
||||
let pallet_event = match (decl.instance.as_ref(), part_is_generic) {
|
||||
@@ -160,7 +160,7 @@ fn expand_pallet_metadata_constants(
|
||||
scrate: &TokenStream,
|
||||
decl: &Pallet,
|
||||
) -> TokenStream {
|
||||
let path = &decl.pallet;
|
||||
let path = &decl.path;
|
||||
let instance = decl.instance.as_ref().into_iter();
|
||||
|
||||
quote!{
|
||||
@@ -177,7 +177,7 @@ fn expand_pallet_metadata_errors(
|
||||
scrate: &TokenStream,
|
||||
decl: &Pallet,
|
||||
) -> TokenStream {
|
||||
let path = &decl.pallet;
|
||||
let path = &decl.path;
|
||||
let instance = decl.instance.as_ref().into_iter();
|
||||
|
||||
quote!{
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License
|
||||
|
||||
use crate::construct_runtime::{parse::PalletPath, Pallet, SYSTEM_PALLET_NAME};
|
||||
use crate::construct_runtime::{Pallet, SYSTEM_PALLET_NAME};
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use quote::quote;
|
||||
use syn::{token, Ident, Generics};
|
||||
|
||||
pub fn expand_outer_origin(
|
||||
@@ -39,7 +39,6 @@ pub fn expand_outer_origin(
|
||||
|
||||
for pallet_decl in pallets.iter().filter(|pallet| pallet.name != SYSTEM_PALLET_NAME) {
|
||||
if let Some(pallet_entry) = pallet_decl.find_part("Origin") {
|
||||
let path = &pallet_decl.pallet;
|
||||
let instance = pallet_decl.instance.as_ref();
|
||||
let index = pallet_decl.index;
|
||||
let generics = &pallet_entry.generics;
|
||||
@@ -54,15 +53,15 @@ pub fn expand_outer_origin(
|
||||
}
|
||||
|
||||
caller_variants.extend(
|
||||
expand_origin_caller_variant(runtime, path, index, instance, generics),
|
||||
expand_origin_caller_variant(runtime, pallet_decl, index, instance, generics),
|
||||
);
|
||||
pallet_conversions.extend(
|
||||
expand_origin_pallet_conversions(scrate, runtime, path, instance, generics),
|
||||
expand_origin_pallet_conversions(scrate, runtime, pallet_decl, instance, generics),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let system_path = &system_pallet.pallet;
|
||||
let system_path = &system_pallet.path;
|
||||
let system_index = system_pallet.index;
|
||||
|
||||
Ok(quote!{
|
||||
@@ -251,28 +250,27 @@ pub fn expand_outer_origin(
|
||||
|
||||
fn expand_origin_caller_variant(
|
||||
runtime: &Ident,
|
||||
path: &PalletPath,
|
||||
pallet: &Pallet,
|
||||
index: u8,
|
||||
instance: Option<&Ident>,
|
||||
generics: &Generics,
|
||||
) -> TokenStream {
|
||||
let part_is_generic = !generics.params.is_empty();
|
||||
let mod_name = &path.mod_name();
|
||||
let variant_name = &pallet.name;
|
||||
let path = &pallet.path;
|
||||
|
||||
match (instance, part_is_generic) {
|
||||
(Some(inst), true) => {
|
||||
let variant = format_ident!("{}_{}", mod_name, inst);
|
||||
quote!(#[codec(index = #index)] #variant(#path::Origin<#runtime, #path::#inst>),)
|
||||
match instance {
|
||||
Some(inst) if part_is_generic => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Origin<#runtime, #path::#inst>),)
|
||||
}
|
||||
(Some(inst), false) => {
|
||||
let variant = format_ident!("{}_{}", mod_name, inst);
|
||||
quote!(#[codec(index = #index)] #variant(#path::Origin<#path::#inst>),)
|
||||
Some(inst) => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Origin<#path::#inst>),)
|
||||
}
|
||||
(None, true) => {
|
||||
quote!(#[codec(index = #index)] #mod_name(#path::Origin<#runtime>),)
|
||||
None if part_is_generic => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Origin<#runtime>),)
|
||||
}
|
||||
(None, false) => {
|
||||
quote!(#[codec(index = #index)] #mod_name(#path::Origin),)
|
||||
None => {
|
||||
quote!(#[codec(index = #index)] #variant_name(#path::Origin),)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -280,29 +278,25 @@ fn expand_origin_caller_variant(
|
||||
fn expand_origin_pallet_conversions(
|
||||
scrate: &TokenStream,
|
||||
runtime: &Ident,
|
||||
path: &PalletPath,
|
||||
pallet: &Pallet,
|
||||
instance: Option<&Ident>,
|
||||
generics: &Generics,
|
||||
) -> TokenStream {
|
||||
let mod_name = path.mod_name();
|
||||
let variant = if let Some(inst) = instance {
|
||||
format_ident!("{}_{}", mod_name, inst)
|
||||
} else {
|
||||
mod_name
|
||||
};
|
||||
let path = &pallet.path;
|
||||
let variant_name = &pallet.name;
|
||||
|
||||
let part_is_generic = !generics.params.is_empty();
|
||||
let pallet_origin = match (instance, part_is_generic) {
|
||||
(Some(inst), true) => quote!(#path::Origin<#runtime, #path::#inst>),
|
||||
(Some(inst), false) => quote!(#path::Origin<#path::#inst>),
|
||||
(None, true) => quote!(#path::Origin<#runtime>),
|
||||
(None, false) => quote!(#path::Origin),
|
||||
let pallet_origin = match instance {
|
||||
Some(inst) if part_is_generic => quote!(#path::Origin<#runtime, #path::#inst>),
|
||||
Some(inst) => quote!(#path::Origin<#path::#inst>),
|
||||
None if part_is_generic => quote!(#path::Origin<#runtime>),
|
||||
None => quote!(#path::Origin),
|
||||
};
|
||||
|
||||
quote!{
|
||||
impl From<#pallet_origin> for OriginCaller {
|
||||
fn from(x: #pallet_origin) -> Self {
|
||||
OriginCaller::#variant(x)
|
||||
OriginCaller::#variant_name(x)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,7 +311,7 @@ fn expand_origin_pallet_conversions(
|
||||
impl From<Origin> for #scrate::sp_std::result::Result<#pallet_origin, Origin> {
|
||||
/// NOTE: converting to pallet origin loses the origin filter information.
|
||||
fn from(val: Origin) -> Self {
|
||||
if let OriginCaller::#variant(l) = val.caller {
|
||||
if let OriginCaller::#variant_name(l) = val.caller {
|
||||
Ok(l)
|
||||
} else {
|
||||
Err(val)
|
||||
@@ -330,7 +324,7 @@ fn expand_origin_pallet_conversions(
|
||||
fn try_from(
|
||||
x: OriginCaller,
|
||||
) -> #scrate::sp_std::result::Result<#pallet_origin, OriginCaller> {
|
||||
if let OriginCaller::#variant(l) = x {
|
||||
if let OriginCaller::#variant_name(l) = x {
|
||||
Ok(l)
|
||||
} else {
|
||||
Err(x)
|
||||
|
||||
@@ -35,7 +35,7 @@ const SYSTEM_PALLET_NAME: &str = "System";
|
||||
pub struct Pallet {
|
||||
pub name: Ident,
|
||||
pub index: u8,
|
||||
pub pallet: PalletPath,
|
||||
pub path: PalletPath,
|
||||
pub instance: Option<Ident>,
|
||||
pub pallet_parts: Vec<PalletPart>,
|
||||
}
|
||||
@@ -101,7 +101,7 @@ fn complete_pallets(decl: impl Iterator<Item = PalletDeclaration>) -> syn::Resul
|
||||
Ok(Pallet {
|
||||
name: pallet.name,
|
||||
index: final_index,
|
||||
pallet: pallet.pallet,
|
||||
path: pallet.path,
|
||||
instance: pallet.instance,
|
||||
pallet_parts: pallet.pallet_parts,
|
||||
})
|
||||
@@ -252,7 +252,7 @@ fn decl_outer_dispatch<'a>(
|
||||
let pallets_tokens = pallet_declarations
|
||||
.filter(|pallet_declaration| pallet_declaration.exists_part("Call"))
|
||||
.map(|pallet_declaration| {
|
||||
let pallet = &pallet_declaration.pallet.inner.segments.last().unwrap();
|
||||
let pallet = &pallet_declaration.path.inner.segments.last().unwrap();
|
||||
let name = &pallet_declaration.name;
|
||||
let index = pallet_declaration.index;
|
||||
quote!(#[codec(index = #index)] #pallet::#name)
|
||||
@@ -275,7 +275,7 @@ fn decl_all_pallets<'a>(
|
||||
let mut names = Vec::new();
|
||||
for pallet_declaration in pallet_declarations {
|
||||
let type_name = &pallet_declaration.name;
|
||||
let pallet = &pallet_declaration.pallet;
|
||||
let pallet = &pallet_declaration.path;
|
||||
let mut generics = vec![quote!(#runtime)];
|
||||
generics.extend(
|
||||
pallet_declaration
|
||||
|
||||
@@ -156,7 +156,7 @@ pub struct PalletDeclaration {
|
||||
pub name: Ident,
|
||||
/// Optional fixed index (e.g. `MyPallet ... = 3,`)
|
||||
pub index: Option<u8>,
|
||||
pub pallet: PalletPath,
|
||||
pub path: PalletPath,
|
||||
pub instance: Option<Ident>,
|
||||
pub pallet_parts: Vec<PalletPart>,
|
||||
}
|
||||
@@ -165,7 +165,7 @@ impl Parse for PalletDeclaration {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let name = input.parse()?;
|
||||
let _: Token![:] = input.parse()?;
|
||||
let pallet = input.parse()?;
|
||||
let path = input.parse()?;
|
||||
let instance = if input.peek(Token![<]) {
|
||||
let _: Token![<] = input.parse()?;
|
||||
let res = Some(input.parse()?);
|
||||
@@ -189,7 +189,7 @@ impl Parse for PalletDeclaration {
|
||||
|
||||
let parsed = Self {
|
||||
name,
|
||||
pallet,
|
||||
path,
|
||||
instance,
|
||||
pallet_parts,
|
||||
index,
|
||||
@@ -247,30 +247,6 @@ impl Parse for PalletPath {
|
||||
}
|
||||
}
|
||||
|
||||
impl PalletPath {
|
||||
/// Return the snake-cased module name for this path.
|
||||
pub fn mod_name(&self) -> Ident {
|
||||
let mut iter = self.inner.segments.iter();
|
||||
let mut mod_name = match &iter.next().expect("Path should always have 1 segment; qed").ident {
|
||||
ident if ident == "self" || ident == "super" || ident == "crate" => {
|
||||
// Skip `crate`, `self` and `super` quasi-keywords when creating the module name
|
||||
iter.next()
|
||||
.expect("There must be a path segment pointing to a pallet following \
|
||||
`crate`, `self` or `super`; qed")
|
||||
.ident
|
||||
.clone()
|
||||
}
|
||||
ident => ident.clone(),
|
||||
};
|
||||
|
||||
for segment in iter {
|
||||
mod_name = quote::format_ident!("{}_{}", mod_name, segment.ident);
|
||||
}
|
||||
|
||||
mod_name
|
||||
}
|
||||
}
|
||||
|
||||
impl quote::ToTokens for PalletPath {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
self.inner.to_tokens(tokens);
|
||||
|
||||
Reference in New Issue
Block a user