3139ffa25e
- snowbridge-pezpallet-* → pezsnowbridge-pezpallet-* (201 refs) - pallet/ directories → pezpallet/ (4 locations) - Fixed pezpallet.rs self-include recursion bug - Fixed sc-chain-spec hardcoded crate name in derive macro - Reverted .pezpallet_by_name() to .pallet_by_name() (subxt API) - Added BizinikiwiConfig type alias for zombienet tests - Deleted obsolete session state files Verified: pezsnowbridge-pezpallet-*, pezpallet-staking, pezpallet-staking-async, pezframe-benchmarking-cli all pass cargo check
171 lines
5.7 KiB
Rust
171 lines
5.7 KiB
Rust
// This file is part of Bizinikiwi.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
use crate::pezpallet::{parse::GenericKind, Def};
|
|
use proc_macro2::TokenStream;
|
|
use quote::quote;
|
|
use syn::{parse_quote, Item};
|
|
|
|
///
|
|
/// * Generate default rust doc
|
|
pub fn expand_config(def: &mut Def) -> TokenStream {
|
|
let config = &def.config;
|
|
let config_item = {
|
|
let item = &mut def.item.content.as_mut().expect("Checked by def parser").1[config.index];
|
|
if let Item::Trait(item) = item {
|
|
item
|
|
} else {
|
|
unreachable!("Checked by config parser")
|
|
}
|
|
};
|
|
|
|
config_item.attrs.insert(
|
|
0,
|
|
parse_quote!(
|
|
#[doc = r"
|
|
Configuration trait of this pezpallet.
|
|
|
|
The main purpose of this trait is to act as an interface between this pezpallet and the runtime in
|
|
which it is embedded in. A type, function, or constant in this trait is essentially left to be
|
|
configured by the runtime that includes this pezpallet.
|
|
|
|
Consequently, a runtime that wants to include this pezpallet must implement this trait."
|
|
]
|
|
),
|
|
);
|
|
config_item.attrs.retain(|attr| !attr.path().is_ident("deprecated"));
|
|
|
|
// insert `pezframe_system::Config` supertrait with `RuntimeEvent: From<Event<Self>>` if neither
|
|
// associated type nor type bound is defined.
|
|
if let Some(event) = &def.event {
|
|
if !def.is_pezframe_system {
|
|
let pezframe_system = &def.pezframe_system;
|
|
|
|
// can't use `type_use_gen()` since it returns `T`, not `Self`
|
|
let event_use_gen = match event.gen_kind {
|
|
GenericKind::None => quote!(),
|
|
GenericKind::Config => quote::quote_spanned! {event.attr_span => Self},
|
|
GenericKind::ConfigAndInstance => {
|
|
quote::quote_spanned! {event.attr_span => Self, I}
|
|
},
|
|
};
|
|
|
|
let supertrait_with_event_bound = syn::parse2::<syn::TypeParamBound>(
|
|
quote! { #pezframe_system::Config<RuntimeEvent: From<Event<#event_use_gen>>> },
|
|
)
|
|
.expect("Parsing super trait doesn't fail; qed");
|
|
|
|
config_item.supertraits.push(supertrait_with_event_bound.into());
|
|
}
|
|
}
|
|
|
|
// we only emit `DefaultConfig` if there are trait items, so an empty `DefaultConfig` is
|
|
// impossible consequently.
|
|
match &config.default_sub_trait {
|
|
Some(default_sub_trait) if default_sub_trait.items.len() > 0 => {
|
|
let trait_items = &default_sub_trait
|
|
.items
|
|
.iter()
|
|
.map(|item| {
|
|
if item.1 {
|
|
if let syn::TraitItem::Type(item) = item.0.clone() {
|
|
let mut item = item.clone();
|
|
item.bounds.clear();
|
|
syn::TraitItem::Type(item)
|
|
} else {
|
|
item.0.clone()
|
|
}
|
|
} else {
|
|
item.0.clone()
|
|
}
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
let type_param_bounds = if default_sub_trait.has_system {
|
|
let system = &def.pezframe_system;
|
|
quote::quote!(: #system::DefaultConfig)
|
|
} else {
|
|
quote::quote!()
|
|
};
|
|
|
|
quote!(
|
|
/// Based on [`Config`]. Auto-generated by
|
|
/// [`#[pezpallet::config(with_default)]`](`pezframe_support::pezpallet_macros::config`).
|
|
/// Can be used in tandem with
|
|
/// [`#[register_default_config]`](`pezframe_support::register_default_config`) and
|
|
/// [`#[derive_impl]`](`pezframe_support::derive_impl`) to derive test config traits
|
|
/// based on existing pezpallet config traits in a safe and developer-friendly way.
|
|
///
|
|
/// See [here](`pezframe_support::pezpallet_macros::config`) for more information and caveats about
|
|
/// the auto-generated `DefaultConfig` trait and how it is generated.
|
|
pub trait DefaultConfig #type_param_bounds {
|
|
#(#trait_items)*
|
|
}
|
|
)
|
|
},
|
|
_ => quote!(),
|
|
}
|
|
}
|
|
|
|
/// Generate the metadata for the associated types of the config trait.
|
|
///
|
|
/// Implements the `pezpallet_associated_types_metadata` function for the pezpallet.
|
|
pub fn expand_config_metadata(def: &Def) -> proc_macro2::TokenStream {
|
|
let pezframe_support = &def.pezframe_support;
|
|
let type_impl_gen = &def.type_impl_generics(proc_macro2::Span::call_site());
|
|
let type_use_gen = &def.type_use_generics(proc_macro2::Span::call_site());
|
|
let pezpallet_ident = &def.pezpallet_struct.pezpallet;
|
|
let trait_use_gen = &def.trait_use_generics(proc_macro2::Span::call_site());
|
|
|
|
let mut where_clauses = vec![&def.config.where_clause];
|
|
where_clauses.extend(def.extra_constants.iter().map(|d| &d.where_clause));
|
|
let completed_where_clause = super::merge_where_clauses(&where_clauses);
|
|
|
|
let types = def.config.associated_types_metadata.iter().map(|metadata| {
|
|
let ident = &metadata.ident;
|
|
let span = ident.span();
|
|
let ident_str = ident.to_string();
|
|
let cfgs = &metadata.cfg;
|
|
|
|
let no_docs = vec![];
|
|
let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { &metadata.doc };
|
|
|
|
quote::quote_spanned!(span => {
|
|
#( #cfgs ) *
|
|
#pezframe_support::__private::metadata_ir::PalletAssociatedTypeMetadataIR {
|
|
name: #ident_str,
|
|
ty: #pezframe_support::__private::scale_info::meta_type::<
|
|
<T as Config #trait_use_gen>::#ident
|
|
>(),
|
|
docs: #pezframe_support::__private::vec![ #( #doc ),* ],
|
|
}
|
|
})
|
|
});
|
|
|
|
quote::quote!(
|
|
impl<#type_impl_gen> #pezpallet_ident<#type_use_gen> #completed_where_clause {
|
|
|
|
#[doc(hidden)]
|
|
pub fn pezpallet_associated_types_metadata()
|
|
-> #pezframe_support::__private::vec::Vec<#pezframe_support::__private::metadata_ir::PalletAssociatedTypeMetadataIR>
|
|
{
|
|
#pezframe_support::__private::vec![ #( #types ),* ]
|
|
}
|
|
}
|
|
)
|
|
}
|