mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-20 23:15:42 +00:00
Make pallet macro generate accessor to PalletInfo information on pallet placeholder (#8630)
* generate accessor to PalletInfo information on pallet placeholder * remove unused * use trait, and add tests * less verbose doc * add PalletInfoAccess to prelude for ease usage
This commit is contained in:
committed by
GitHub
parent
d64f79924a
commit
ea10494ca9
@@ -23,6 +23,7 @@ use crate::pallet::{Def, parse::helper::get_doc_literals};
|
||||
/// * Implement ModuleErrorMetadata on Pallet
|
||||
/// * declare Module type alias for construct_runtime
|
||||
/// * replace the first field type of `struct Pallet` with `PhantomData` if it is `_`
|
||||
/// * implementation of `PalletInfoAccess` information
|
||||
pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
let frame_support = &def.frame_support;
|
||||
let frame_system = &def.frame_system;
|
||||
@@ -134,5 +135,27 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
.put_into_storage::<<T as #frame_system::Config>::PalletInfo, Self>();
|
||||
}
|
||||
}
|
||||
|
||||
// Implement `PalletInfoAccess` for `Pallet`
|
||||
impl<#type_impl_gen> #frame_support::traits::PalletInfoAccess
|
||||
for #pallet_ident<#type_use_gen>
|
||||
#config_where_clause
|
||||
{
|
||||
fn index() -> usize {
|
||||
<
|
||||
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
|
||||
>::index::<Self>()
|
||||
.expect("Pallet is part of the runtime because pallet `Config` trait is \
|
||||
implemented by the runtime")
|
||||
}
|
||||
|
||||
fn name() -> &'static str {
|
||||
<
|
||||
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
|
||||
>::name::<Self>()
|
||||
.expect("Pallet is part of the runtime because pallet `Config` trait is \
|
||||
implemented by the runtime")
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1234,7 +1234,7 @@ pub mod pallet_prelude {
|
||||
EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, DebugNoBound, CloneNoBound, Twox256,
|
||||
Twox128, Blake2_256, Blake2_128, Identity, Twox64Concat, Blake2_128Concat, ensure,
|
||||
RuntimeDebug, storage,
|
||||
traits::{Get, Hooks, IsType, GetPalletVersion, EnsureOrigin},
|
||||
traits::{Get, Hooks, IsType, GetPalletVersion, EnsureOrigin, PalletInfoAccess},
|
||||
dispatch::{DispatchResultWithPostInfo, Parameter, DispatchError, DispatchResult},
|
||||
weights::{DispatchClass, Pays, Weight},
|
||||
storage::types::{StorageValue, StorageMap, StorageDoubleMap, ValueQuery, OptionQuery},
|
||||
@@ -1362,6 +1362,10 @@ pub mod pallet_prelude {
|
||||
///
|
||||
/// It declare `type Module` type alias for `Pallet`, used by [`construct_runtime`].
|
||||
///
|
||||
/// It implements [`traits::PalletInfoAccess`] on `Pallet` to ease access to pallet informations
|
||||
/// given by [`frame_support::traits::PalletInfo`].
|
||||
/// (The implementation use the associated type `frame_system::Config::PalletInfo`).
|
||||
///
|
||||
/// If attribute generate_store then macro create the trait `Store` and implement it on `Pallet`.
|
||||
///
|
||||
/// # Hooks: `#[pallet::hooks]` mandatory
|
||||
|
||||
@@ -61,7 +61,7 @@ pub use randomness::Randomness;
|
||||
mod metadata;
|
||||
pub use metadata::{
|
||||
CallMetadata, GetCallMetadata, GetCallName, PalletInfo, PalletVersion, GetPalletVersion,
|
||||
PALLET_VERSION_STORAGE_KEY_POSTFIX,
|
||||
PALLET_VERSION_STORAGE_KEY_POSTFIX, PalletInfoAccess,
|
||||
};
|
||||
|
||||
mod hooks;
|
||||
|
||||
@@ -31,6 +31,16 @@ pub trait PalletInfo {
|
||||
fn name<P: 'static>() -> Option<&'static str>;
|
||||
}
|
||||
|
||||
/// Provides information about the pallet setup in the runtime.
|
||||
///
|
||||
/// Access the information provided by [`PalletInfo`] for a specific pallet.
|
||||
pub trait PalletInfoAccess {
|
||||
/// Index of the pallet as configured in the runtime.
|
||||
fn index() -> usize;
|
||||
/// Name of the pallet as configured in the runtime.
|
||||
fn name() -> &'static str;
|
||||
}
|
||||
|
||||
/// The function and pallet name of the Call.
|
||||
#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug)]
|
||||
pub struct CallMetadata {
|
||||
|
||||
@@ -861,3 +861,14 @@ fn metadata() {
|
||||
|
||||
pretty_assertions::assert_eq!(pallet_metadata, expected_pallet_metadata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pallet_info_access() {
|
||||
assert_eq!(<System as frame_support::traits::PalletInfoAccess>::name(), "System");
|
||||
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::name(), "Example");
|
||||
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::name(), "Example2");
|
||||
|
||||
assert_eq!(<System as frame_support::traits::PalletInfoAccess>::index(), 0);
|
||||
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::index(), 1);
|
||||
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::index(), 2);
|
||||
}
|
||||
|
||||
@@ -712,3 +712,18 @@ fn metadata() {
|
||||
pretty_assertions::assert_eq!(pallet_metadata, expected_pallet_metadata);
|
||||
pretty_assertions::assert_eq!(pallet_instance1_metadata, expected_pallet_instance1_metadata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pallet_info_access() {
|
||||
assert_eq!(<System as frame_support::traits::PalletInfoAccess>::name(), "System");
|
||||
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::name(), "Example");
|
||||
assert_eq!(<Instance1Example as frame_support::traits::PalletInfoAccess>::name(), "Instance1Example");
|
||||
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::name(), "Example2");
|
||||
assert_eq!(<Instance1Example2 as frame_support::traits::PalletInfoAccess>::name(), "Instance1Example2");
|
||||
|
||||
assert_eq!(<System as frame_support::traits::PalletInfoAccess>::index(), 0);
|
||||
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::index(), 1);
|
||||
assert_eq!(<Instance1Example as frame_support::traits::PalletInfoAccess>::index(), 2);
|
||||
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::index(), 3);
|
||||
assert_eq!(<Instance1Example2 as frame_support::traits::PalletInfoAccess>::index(), 4);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ frame_support::decl_module! {
|
||||
const Foo: u32 = u32::max_value();
|
||||
|
||||
#[weight = 0]
|
||||
fn accumulate_dummy(origin, increase_by: T::Balance) {
|
||||
fn accumulate_dummy(_origin, _increase_by: T::Balance) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ pub trait Config: 'static + Eq + Clone {
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::Origin, system=self {
|
||||
#[weight = 0]
|
||||
fn noop(origin) {}
|
||||
fn noop(_origin) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user