mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 15:51:12 +00:00
Implement #[pallet::composite_enum] (#13722)
* Implement #[pallet::hold_reason] * Appease clippy * cargo fmt * Update test expectations * Update test expectations * Support composite_enum attribute instead * Update test expectations * Change hold_reason to composite_enum * Add UI test for unsupported identifier when using composite_enum * Fix comment * Add documentation for pallet::composable_enum * More docs * cargo fmt
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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::construct_runtime::{parse::PalletPath, Pallet};
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::quote;
|
||||
|
||||
pub fn expand_outer_freeze_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -> TokenStream {
|
||||
let mut conversion_fns = Vec::new();
|
||||
let mut freeze_reason_variants = Vec::new();
|
||||
for decl in pallet_decls {
|
||||
if let Some(_) = decl.find_part("FreezeReason") {
|
||||
let variant_name = &decl.name;
|
||||
let path = &decl.path;
|
||||
let index = decl.index;
|
||||
|
||||
conversion_fns.push(expand_conversion_fn(path, variant_name));
|
||||
|
||||
freeze_reason_variants.push(expand_variant(index, path, variant_name));
|
||||
}
|
||||
}
|
||||
|
||||
quote! {
|
||||
#[derive(
|
||||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd,
|
||||
#scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen,
|
||||
#scrate::scale_info::TypeInfo,
|
||||
#scrate::RuntimeDebug,
|
||||
)]
|
||||
pub enum RuntimeFreezeReason {
|
||||
#( #freeze_reason_variants )*
|
||||
}
|
||||
|
||||
#( #conversion_fns )*
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_conversion_fn(path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
impl From<#path::FreezeReason> for RuntimeFreezeReason {
|
||||
fn from(hr: #path::FreezeReason) -> Self {
|
||||
RuntimeFreezeReason::#variant_name(hr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_variant(index: u8, path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
#[codec(index = #index)]
|
||||
#variant_name(#path::FreezeReason),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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::construct_runtime::{parse::PalletPath, Pallet};
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::quote;
|
||||
|
||||
pub fn expand_outer_hold_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -> TokenStream {
|
||||
let mut conversion_fns = Vec::new();
|
||||
let mut hold_reason_variants = Vec::new();
|
||||
for decl in pallet_decls {
|
||||
if let Some(_) = decl.find_part("HoldReason") {
|
||||
let variant_name = &decl.name;
|
||||
let path = &decl.path;
|
||||
let index = decl.index;
|
||||
|
||||
conversion_fns.push(expand_conversion_fn(path, variant_name));
|
||||
|
||||
hold_reason_variants.push(expand_variant(index, path, variant_name));
|
||||
}
|
||||
}
|
||||
|
||||
quote! {
|
||||
#[derive(
|
||||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd,
|
||||
#scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen,
|
||||
#scrate::scale_info::TypeInfo,
|
||||
#scrate::RuntimeDebug,
|
||||
)]
|
||||
pub enum RuntimeHoldReason {
|
||||
#( #hold_reason_variants )*
|
||||
}
|
||||
|
||||
#( #conversion_fns )*
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_conversion_fn(path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
impl From<#path::HoldReason> for RuntimeHoldReason {
|
||||
fn from(hr: #path::HoldReason) -> Self {
|
||||
RuntimeHoldReason::#variant_name(hr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_variant(index: u8, path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
#[codec(index = #index)]
|
||||
#variant_name(#path::HoldReason),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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::construct_runtime::{parse::PalletPath, Pallet};
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::quote;
|
||||
|
||||
pub fn expand_outer_lock_id(pallet_decls: &[Pallet], scrate: &TokenStream) -> TokenStream {
|
||||
let mut conversion_fns = Vec::new();
|
||||
let mut lock_id_variants = Vec::new();
|
||||
for decl in pallet_decls {
|
||||
if let Some(_) = decl.find_part("LockId") {
|
||||
let variant_name = &decl.name;
|
||||
let path = &decl.path;
|
||||
let index = decl.index;
|
||||
|
||||
conversion_fns.push(expand_conversion_fn(path, variant_name));
|
||||
|
||||
lock_id_variants.push(expand_variant(index, path, variant_name));
|
||||
}
|
||||
}
|
||||
|
||||
quote! {
|
||||
#[derive(
|
||||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd,
|
||||
#scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen,
|
||||
#scrate::scale_info::TypeInfo,
|
||||
#scrate::RuntimeDebug,
|
||||
)]
|
||||
pub enum RuntimeLockId {
|
||||
#( #lock_id_variants )*
|
||||
}
|
||||
|
||||
#( #conversion_fns )*
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_conversion_fn(path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
impl From<#path::LockId> for RuntimeLockId {
|
||||
fn from(hr: #path::LockId) -> Self {
|
||||
RuntimeLockId::#variant_name(hr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_variant(index: u8, path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
#[codec(index = #index)]
|
||||
#variant_name(#path::LockId),
|
||||
}
|
||||
}
|
||||
@@ -18,15 +18,23 @@
|
||||
mod call;
|
||||
mod config;
|
||||
mod event;
|
||||
mod freeze_reason;
|
||||
mod hold_reason;
|
||||
mod inherent;
|
||||
mod lock_id;
|
||||
mod metadata;
|
||||
mod origin;
|
||||
mod slash_reason;
|
||||
mod unsigned;
|
||||
|
||||
pub use call::expand_outer_dispatch;
|
||||
pub use config::expand_outer_config;
|
||||
pub use event::expand_outer_event;
|
||||
pub use freeze_reason::expand_outer_freeze_reason;
|
||||
pub use hold_reason::expand_outer_hold_reason;
|
||||
pub use inherent::expand_outer_inherent;
|
||||
pub use lock_id::expand_outer_lock_id;
|
||||
pub use metadata::expand_runtime_metadata;
|
||||
pub use origin::expand_outer_origin;
|
||||
pub use slash_reason::expand_outer_slash_reason;
|
||||
pub use unsigned::expand_outer_validate_unsigned;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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::construct_runtime::{parse::PalletPath, Pallet};
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::quote;
|
||||
|
||||
pub fn expand_outer_slash_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -> TokenStream {
|
||||
let mut conversion_fns = Vec::new();
|
||||
let mut slash_reason_variants = Vec::new();
|
||||
for decl in pallet_decls {
|
||||
if let Some(_) = decl.find_part("SlashReason") {
|
||||
let variant_name = &decl.name;
|
||||
let path = &decl.path;
|
||||
let index = decl.index;
|
||||
|
||||
conversion_fns.push(expand_conversion_fn(path, variant_name));
|
||||
|
||||
slash_reason_variants.push(expand_variant(index, path, variant_name));
|
||||
}
|
||||
}
|
||||
|
||||
quote! {
|
||||
#[derive(
|
||||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd,
|
||||
#scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen,
|
||||
#scrate::scale_info::TypeInfo,
|
||||
#scrate::RuntimeDebug,
|
||||
)]
|
||||
pub enum RuntimeSlashReason {
|
||||
#( #slash_reason_variants )*
|
||||
}
|
||||
|
||||
#( #conversion_fns )*
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_conversion_fn(path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
impl From<#path::SlashReason> for RuntimeSlashReason {
|
||||
fn from(hr: #path::SlashReason) -> Self {
|
||||
RuntimeSlashReason::#variant_name(hr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_variant(index: u8, path: &PalletPath, variant_name: &Ident) -> TokenStream {
|
||||
quote! {
|
||||
#[codec(index = #index)]
|
||||
#variant_name(#path::SlashReason),
|
||||
}
|
||||
}
|
||||
@@ -268,6 +268,10 @@ fn construct_runtime_final_expansion(
|
||||
let inherent =
|
||||
expand::expand_outer_inherent(&name, &block, &unchecked_extrinsic, &pallets, &scrate);
|
||||
let validate_unsigned = expand::expand_outer_validate_unsigned(&name, &pallets, &scrate);
|
||||
let freeze_reason = expand::expand_outer_freeze_reason(&pallets, &scrate);
|
||||
let hold_reason = expand::expand_outer_hold_reason(&pallets, &scrate);
|
||||
let lock_id = expand::expand_outer_lock_id(&pallets, &scrate);
|
||||
let slash_reason = expand::expand_outer_slash_reason(&pallets, &scrate);
|
||||
let integrity_test = decl_integrity_test(&scrate);
|
||||
let static_assertions = decl_static_assertions(&name, &pallets, &scrate);
|
||||
|
||||
@@ -310,6 +314,14 @@ fn construct_runtime_final_expansion(
|
||||
|
||||
#validate_unsigned
|
||||
|
||||
#freeze_reason
|
||||
|
||||
#hold_reason
|
||||
|
||||
#lock_id
|
||||
|
||||
#slash_reason
|
||||
|
||||
#integrity_test
|
||||
|
||||
#static_assertions
|
||||
|
||||
@@ -38,6 +38,10 @@ mod keyword {
|
||||
syn::custom_keyword!(Origin);
|
||||
syn::custom_keyword!(Inherent);
|
||||
syn::custom_keyword!(ValidateUnsigned);
|
||||
syn::custom_keyword!(FreezeReason);
|
||||
syn::custom_keyword!(HoldReason);
|
||||
syn::custom_keyword!(LockId);
|
||||
syn::custom_keyword!(SlashReason);
|
||||
syn::custom_keyword!(exclude_parts);
|
||||
syn::custom_keyword!(use_parts);
|
||||
}
|
||||
@@ -370,6 +374,10 @@ pub enum PalletPartKeyword {
|
||||
Origin(keyword::Origin),
|
||||
Inherent(keyword::Inherent),
|
||||
ValidateUnsigned(keyword::ValidateUnsigned),
|
||||
FreezeReason(keyword::FreezeReason),
|
||||
HoldReason(keyword::HoldReason),
|
||||
LockId(keyword::LockId),
|
||||
SlashReason(keyword::SlashReason),
|
||||
}
|
||||
|
||||
impl Parse for PalletPartKeyword {
|
||||
@@ -392,6 +400,14 @@ impl Parse for PalletPartKeyword {
|
||||
Ok(Self::Inherent(input.parse()?))
|
||||
} else if lookahead.peek(keyword::ValidateUnsigned) {
|
||||
Ok(Self::ValidateUnsigned(input.parse()?))
|
||||
} else if lookahead.peek(keyword::FreezeReason) {
|
||||
Ok(Self::FreezeReason(input.parse()?))
|
||||
} else if lookahead.peek(keyword::HoldReason) {
|
||||
Ok(Self::HoldReason(input.parse()?))
|
||||
} else if lookahead.peek(keyword::LockId) {
|
||||
Ok(Self::LockId(input.parse()?))
|
||||
} else if lookahead.peek(keyword::SlashReason) {
|
||||
Ok(Self::SlashReason(input.parse()?))
|
||||
} else {
|
||||
Err(lookahead.error())
|
||||
}
|
||||
@@ -410,6 +426,10 @@ impl PalletPartKeyword {
|
||||
Self::Origin(_) => "Origin",
|
||||
Self::Inherent(_) => "Inherent",
|
||||
Self::ValidateUnsigned(_) => "ValidateUnsigned",
|
||||
Self::FreezeReason(_) => "FreezeReason",
|
||||
Self::HoldReason(_) => "HoldReason",
|
||||
Self::LockId(_) => "LockId",
|
||||
Self::SlashReason(_) => "SlashReason",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,6 +455,10 @@ impl Spanned for PalletPartKeyword {
|
||||
Self::Origin(inner) => inner.span(),
|
||||
Self::Inherent(inner) => inner.span(),
|
||||
Self::ValidateUnsigned(inner) => inner.span(),
|
||||
Self::FreezeReason(inner) => inner.span(),
|
||||
Self::HoldReason(inner) => inner.span(),
|
||||
Self::LockId(inner) => inner.span(),
|
||||
Self::SlashReason(inner) => inner.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1411,3 +1411,30 @@ pub fn validate_unsigned(_: TokenStream, _: TokenStream) -> TokenStream {
|
||||
pub fn origin(_: TokenStream, _: TokenStream) -> TokenStream {
|
||||
pallet_macro_stub()
|
||||
}
|
||||
|
||||
/// The `#[pallet::composable_enum]` attribute allows you to define an enum that gets composed as an
|
||||
/// aggregate enum by `construct_runtime`. This is similar in principle with `#[pallet::event]` and
|
||||
/// `#[pallet::error]`.
|
||||
///
|
||||
/// The attribute currently only supports enum definitions, and identifiers that are named
|
||||
/// `FreezeReason`, `HoldReason`, `LockId` or `SlashReason`. Arbitrary identifiers for the enum are
|
||||
/// not supported. The aggregate enum generated by `construct_runtime` will have the name of
|
||||
/// `RuntimeFreezeReason`, `RuntimeHoldReason`, `RuntimeLockId` and `RuntimeSlashReason`
|
||||
/// respectively.
|
||||
///
|
||||
/// NOTE: The aggregate enum generated by `construct_runtime` generates a conversion function from
|
||||
/// the pallet enum to the aggregate enum, and automatically derives the following traits:
|
||||
///
|
||||
/// ```ignore
|
||||
/// Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo,
|
||||
/// RuntimeDebug
|
||||
/// ```
|
||||
///
|
||||
/// For ease of usage, when no `#[derive]` attributes are found for the enum under
|
||||
/// `#[pallet::composable_enum]`, the aforementioned traits are automatically derived for it. The
|
||||
/// inverse is also true: if there are any `#[derive]` attributes found for the enum, then no traits
|
||||
/// will automatically be derived for it.
|
||||
#[proc_macro_attribute]
|
||||
pub fn composable_enum(_: TokenStream, _: TokenStream) -> TokenStream {
|
||||
pallet_macro_stub()
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::{pallet::Def, COUNTER};
|
||||
use crate::{
|
||||
pallet::{CompositeKeyword, Def},
|
||||
COUNTER,
|
||||
};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// Generate the `tt_default_parts` macro.
|
||||
@@ -48,6 +51,30 @@ pub fn expand_tt_default_parts(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
let validate_unsigned_part =
|
||||
def.validate_unsigned.as_ref().map(|_| quote::quote!(ValidateUnsigned,));
|
||||
|
||||
let freeze_reason_part = def
|
||||
.composites
|
||||
.iter()
|
||||
.any(|c| matches!(c.composite_keyword, CompositeKeyword::FreezeReason(_)))
|
||||
.then_some(quote::quote!(FreezeReason,));
|
||||
|
||||
let hold_reason_part = def
|
||||
.composites
|
||||
.iter()
|
||||
.any(|c| matches!(c.composite_keyword, CompositeKeyword::HoldReason(_)))
|
||||
.then_some(quote::quote!(HoldReason,));
|
||||
|
||||
let lock_id_part = def
|
||||
.composites
|
||||
.iter()
|
||||
.any(|c| matches!(c.composite_keyword, CompositeKeyword::LockId(_)))
|
||||
.then_some(quote::quote!(LockId,));
|
||||
|
||||
let slash_reason_part = def
|
||||
.composites
|
||||
.iter()
|
||||
.any(|c| matches!(c.composite_keyword, CompositeKeyword::SlashReason(_)))
|
||||
.then_some(quote::quote!(SlashReason,));
|
||||
|
||||
quote::quote!(
|
||||
// This macro follows the conventions as laid out by the `tt-call` crate. It does not
|
||||
// accept any arguments and simply returns the pallet parts, separated by commas, then
|
||||
@@ -70,7 +97,8 @@ pub fn expand_tt_default_parts(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
tokens = [{
|
||||
::{
|
||||
Pallet, #call_part #storage_part #event_part #origin_part #config_part
|
||||
#inherent_part #validate_unsigned_part
|
||||
#inherent_part #validate_unsigned_part #freeze_reason_part
|
||||
#hold_reason_part #lock_id_part #slash_reason_part
|
||||
}
|
||||
}]
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
mod expand;
|
||||
mod parse;
|
||||
|
||||
pub use parse::Def;
|
||||
pub use parse::{composite::keyword::CompositeKeyword, Def};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
mod keyword {
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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 quote::ToTokens;
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
pub mod keyword {
|
||||
use super::*;
|
||||
|
||||
syn::custom_keyword!(FreezeReason);
|
||||
syn::custom_keyword!(HoldReason);
|
||||
syn::custom_keyword!(LockId);
|
||||
syn::custom_keyword!(SlashReason);
|
||||
pub enum CompositeKeyword {
|
||||
FreezeReason(FreezeReason),
|
||||
HoldReason(HoldReason),
|
||||
LockId(LockId),
|
||||
SlashReason(SlashReason),
|
||||
}
|
||||
|
||||
impl Spanned for CompositeKeyword {
|
||||
fn span(&self) -> proc_macro2::Span {
|
||||
use CompositeKeyword::*;
|
||||
match self {
|
||||
FreezeReason(inner) => inner.span(),
|
||||
HoldReason(inner) => inner.span(),
|
||||
LockId(inner) => inner.span(),
|
||||
SlashReason(inner) => inner.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl syn::parse::Parse for CompositeKeyword {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(FreezeReason) {
|
||||
Ok(Self::FreezeReason(input.parse()?))
|
||||
} else if lookahead.peek(HoldReason) {
|
||||
Ok(Self::HoldReason(input.parse()?))
|
||||
} else if lookahead.peek(LockId) {
|
||||
Ok(Self::LockId(input.parse()?))
|
||||
} else if lookahead.peek(SlashReason) {
|
||||
Ok(Self::SlashReason(input.parse()?))
|
||||
} else {
|
||||
Err(lookahead.error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CompositeKeyword {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
use CompositeKeyword::*;
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
FreezeReason(_) => "FreezeReason",
|
||||
HoldReason(_) => "HoldReason",
|
||||
LockId(_) => "LockId",
|
||||
SlashReason(_) => "SlashReason",
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompositeDef {
|
||||
/// The index of the HoldReason item in the pallet module.
|
||||
pub index: usize,
|
||||
/// The composite keyword used (contains span).
|
||||
pub composite_keyword: keyword::CompositeKeyword,
|
||||
/// The span of the pallet::composite_enum attribute.
|
||||
pub attr_span: proc_macro2::Span,
|
||||
}
|
||||
|
||||
impl CompositeDef {
|
||||
pub fn try_from(
|
||||
attr_span: proc_macro2::Span,
|
||||
index: usize,
|
||||
scrate: &proc_macro2::Ident,
|
||||
item: &mut syn::Item,
|
||||
) -> syn::Result<Self> {
|
||||
let item = if let syn::Item::Enum(item) = item {
|
||||
item
|
||||
} else {
|
||||
return Err(syn::Error::new(
|
||||
item.span(),
|
||||
"Invalid pallet::composite_enum, expected enum item",
|
||||
))
|
||||
};
|
||||
|
||||
if !matches!(item.vis, syn::Visibility::Public(_)) {
|
||||
let msg = format!("Invalid pallet::composite_enum, `{}` must be public", item.ident);
|
||||
return Err(syn::Error::new(item.span(), msg))
|
||||
}
|
||||
|
||||
let has_derive_attr = item.attrs.iter().any(|attr| {
|
||||
attr.parse_meta()
|
||||
.ok()
|
||||
.map(|meta| match meta {
|
||||
syn::Meta::List(syn::MetaList { path, .. }) =>
|
||||
path.get_ident().map(|ident| ident == "derive").unwrap_or(false),
|
||||
_ => false,
|
||||
})
|
||||
.unwrap_or(false)
|
||||
});
|
||||
|
||||
if !has_derive_attr {
|
||||
let derive_attr: syn::Attribute = syn::parse_quote! {
|
||||
#[derive(
|
||||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd,
|
||||
#scrate::codec::Encode, #scrate::codec::Decode, #scrate::codec::MaxEncodedLen,
|
||||
#scrate::scale_info::TypeInfo,
|
||||
#scrate::RuntimeDebug,
|
||||
)]
|
||||
};
|
||||
item.attrs.push(derive_attr);
|
||||
}
|
||||
|
||||
let composite_keyword =
|
||||
syn::parse2::<keyword::CompositeKeyword>(item.ident.to_token_stream())?;
|
||||
|
||||
Ok(CompositeDef { index, composite_keyword, attr_span })
|
||||
}
|
||||
}
|
||||
@@ -104,7 +104,7 @@ impl EventDef {
|
||||
let item = if let syn::Item::Enum(item) = item {
|
||||
item
|
||||
} else {
|
||||
return Err(syn::Error::new(item.span(), "Invalid pallet::event, expected item enum"))
|
||||
return Err(syn::Error::new(item.span(), "Invalid pallet::event, expected enum item"))
|
||||
};
|
||||
|
||||
let event_attrs: Vec<PalletEventDepositAttr> =
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
//! Parse the module into `Def` struct through `Def::try_from` function.
|
||||
|
||||
pub mod call;
|
||||
pub mod composite;
|
||||
pub mod config;
|
||||
pub mod error;
|
||||
pub mod event;
|
||||
@@ -35,6 +36,7 @@ pub mod storage;
|
||||
pub mod type_value;
|
||||
pub mod validate_unsigned;
|
||||
|
||||
use composite::{keyword::CompositeKeyword, CompositeDef};
|
||||
use frame_support_procedural_tools::generate_crate_access_2018;
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
@@ -56,6 +58,7 @@ pub struct Def {
|
||||
pub genesis_build: Option<genesis_build::GenesisBuildDef>,
|
||||
pub validate_unsigned: Option<validate_unsigned::ValidateUnsignedDef>,
|
||||
pub extra_constants: Option<extra_constants::ExtraConstantsDef>,
|
||||
pub composites: Vec<composite::CompositeDef>,
|
||||
pub type_values: Vec<type_value::TypeValueDef>,
|
||||
pub frame_system: syn::Ident,
|
||||
pub frame_support: syn::Ident,
|
||||
@@ -91,6 +94,7 @@ impl Def {
|
||||
let mut extra_constants = None;
|
||||
let mut storages = vec![];
|
||||
let mut type_values = vec![];
|
||||
let mut composites: Vec<CompositeDef> = vec![];
|
||||
|
||||
for (index, item) in items.iter_mut().enumerate() {
|
||||
let pallet_attr: Option<PalletAttr> = helper::take_first_item_pallet_attr(item)?;
|
||||
@@ -135,6 +139,32 @@ impl Def {
|
||||
Some(PalletAttr::ExtraConstants(_)) =>
|
||||
extra_constants =
|
||||
Some(extra_constants::ExtraConstantsDef::try_from(index, item)?),
|
||||
Some(PalletAttr::Composite(span)) => {
|
||||
let composite =
|
||||
composite::CompositeDef::try_from(span, index, &frame_support, item)?;
|
||||
if composites.iter().any(|def| {
|
||||
match (&def.composite_keyword, &composite.composite_keyword) {
|
||||
(
|
||||
CompositeKeyword::FreezeReason(_),
|
||||
CompositeKeyword::FreezeReason(_),
|
||||
) |
|
||||
(CompositeKeyword::HoldReason(_), CompositeKeyword::HoldReason(_)) |
|
||||
(CompositeKeyword::LockId(_), CompositeKeyword::LockId(_)) |
|
||||
(
|
||||
CompositeKeyword::SlashReason(_),
|
||||
CompositeKeyword::SlashReason(_),
|
||||
) => true,
|
||||
_ => false,
|
||||
}
|
||||
}) {
|
||||
let msg = format!(
|
||||
"Invalid duplicated `{}` definition",
|
||||
composite.composite_keyword
|
||||
);
|
||||
return Err(syn::Error::new(composite.composite_keyword.span(), &msg))
|
||||
}
|
||||
composites.push(composite);
|
||||
},
|
||||
Some(attr) => {
|
||||
let msg = "Invalid duplicated attribute";
|
||||
return Err(syn::Error::new(attr.span(), msg))
|
||||
@@ -171,6 +201,7 @@ impl Def {
|
||||
origin,
|
||||
inherent,
|
||||
storages,
|
||||
composites,
|
||||
type_values,
|
||||
frame_system,
|
||||
frame_support,
|
||||
@@ -385,6 +416,7 @@ mod keyword {
|
||||
syn::custom_keyword!(generate_store);
|
||||
syn::custom_keyword!(Store);
|
||||
syn::custom_keyword!(extra_constants);
|
||||
syn::custom_keyword!(composite_enum);
|
||||
}
|
||||
|
||||
/// Parse attributes for item in pallet module
|
||||
@@ -404,6 +436,7 @@ enum PalletAttr {
|
||||
ValidateUnsigned(proc_macro2::Span),
|
||||
TypeValue(proc_macro2::Span),
|
||||
ExtraConstants(proc_macro2::Span),
|
||||
Composite(proc_macro2::Span),
|
||||
}
|
||||
|
||||
impl PalletAttr {
|
||||
@@ -423,6 +456,7 @@ impl PalletAttr {
|
||||
Self::ValidateUnsigned(span) => *span,
|
||||
Self::TypeValue(span) => *span,
|
||||
Self::ExtraConstants(span) => *span,
|
||||
Self::Composite(span) => *span,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -464,6 +498,8 @@ impl syn::parse::Parse for PalletAttr {
|
||||
Ok(PalletAttr::TypeValue(content.parse::<keyword::type_value>()?.span()))
|
||||
} else if lookahead.peek(keyword::extra_constants) {
|
||||
Ok(PalletAttr::ExtraConstants(content.parse::<keyword::extra_constants>()?.span()))
|
||||
} else if lookahead.peek(keyword::composite_enum) {
|
||||
Ok(PalletAttr::Composite(content.parse::<keyword::composite_enum>()?.span()))
|
||||
} else {
|
||||
Err(lookahead.error())
|
||||
}
|
||||
|
||||
@@ -1552,6 +1552,7 @@ pub mod pallet_prelude {
|
||||
/// * [`pallet::inherent`](#inherent-palletinherent-optional)
|
||||
/// * [`pallet::validate_unsigned`](#validate-unsigned-palletvalidate_unsigned-optional)
|
||||
/// * [`pallet::origin`](#origin-palletorigin-optional)
|
||||
/// * [`pallet::composable_enum`](#composable-enum-palletcomposable_enum-optional)
|
||||
///
|
||||
/// Note that at compile-time, the `#[pallet]` macro will analyze and expand all of these
|
||||
/// attributes, ultimately removing their AST nodes before they can be parsed as real
|
||||
@@ -2277,6 +2278,29 @@ pub mod pallet_prelude {
|
||||
///
|
||||
/// Also see [`pallet::origin`](`frame_support::pallet_macros::origin`)
|
||||
///
|
||||
/// # Composable enum `#[pallet::composable_enum]` (optional)
|
||||
///
|
||||
/// The `#[pallet::composable_enum]` attribute allows you to define an enum on the pallet which
|
||||
/// will then instruct `construct_runtime` to amalgamate all similarly-named enums from other
|
||||
/// pallets into an aggregate enum. This is similar in principle with how the aggregate enum is
|
||||
/// generated for `#[pallet::event]` or `#[pallet::error]`.
|
||||
///
|
||||
/// The item tagged with `#[pallet::composable_enum]` MUST be an enum declaration, and can ONLY
|
||||
/// be the following identifiers: `FreezeReason`, `HoldReason`, `LockId` or `SlashReason`.
|
||||
/// Custom identifiers are not supported.
|
||||
///
|
||||
/// NOTE: For ease of usage, when no `#[derive]` attributes are detected, the
|
||||
/// `#[pallet::composable_enum]` attribute will automatically derive the following traits for
|
||||
/// the enum:
|
||||
///
|
||||
/// ```ignore
|
||||
/// Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo,
|
||||
/// RuntimeDebug
|
||||
/// ```
|
||||
///
|
||||
/// The inverse is also true: if there are any #[derive] attributes present for the enum, then
|
||||
/// the attribute will not automatically derive any of the traits described above.
|
||||
///
|
||||
/// # General notes on instantiable pallets
|
||||
///
|
||||
/// An instantiable pallet is one where Config is generic, i.e. `Config<I>`. This allows
|
||||
@@ -2808,10 +2832,11 @@ pub use frame_support_procedural::pallet;
|
||||
/// Contains macro stubs for all of the pallet:: macros
|
||||
pub mod pallet_macros {
|
||||
pub use frame_support_procedural::{
|
||||
call_index, compact, config, constant, disable_frame_system_supertrait_check, error, event,
|
||||
extra_constants, generate_deposit, generate_storage_info, generate_store, genesis_build,
|
||||
genesis_config, getter, hooks, inherent, origin, storage, storage_prefix, storage_version,
|
||||
type_value, unbounded, validate_unsigned, weight, whitelist_storage,
|
||||
call_index, compact, composable_enum, config, constant,
|
||||
disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit,
|
||||
generate_storage_info, generate_store, genesis_build, genesis_config, getter, hooks,
|
||||
inherent, origin, storage, storage_prefix, storage_version, type_value, unbounded,
|
||||
validate_unsigned, weight, whitelist_storage,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`
|
||||
error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `LockId`, `SlashReason`
|
||||
--> $DIR/invalid_module_details_keyword.rs:9:20
|
||||
|
|
||||
9 | system: System::{enum},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`
|
||||
error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `LockId`, `SlashReason`
|
||||
--> $DIR/invalid_module_entry.rs:10:23
|
||||
|
|
||||
10 | Balance: balances::{Error},
|
||||
|
||||
@@ -476,6 +476,11 @@ pub mod pallet {
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::composite_enum]
|
||||
pub enum HoldReason {
|
||||
Staking,
|
||||
}
|
||||
|
||||
#[derive(codec::Encode, sp_runtime::RuntimeDebug)]
|
||||
#[cfg_attr(feature = "std", derive(codec::Decode))]
|
||||
pub enum InherentError {
|
||||
@@ -570,6 +575,16 @@ pub mod pallet2 {
|
||||
{
|
||||
fn build(&self) {}
|
||||
}
|
||||
|
||||
#[pallet::composite_enum]
|
||||
pub enum HoldReason {
|
||||
Governance,
|
||||
}
|
||||
|
||||
#[pallet::composite_enum]
|
||||
pub enum SlashReason {
|
||||
Equivocation,
|
||||
}
|
||||
}
|
||||
|
||||
/// Test that the supertrait check works when we pass some parameter to the `frame_system::Config`.
|
||||
@@ -974,6 +989,17 @@ fn validate_unsigned_expand() {
|
||||
assert_eq!(validity, ValidTransaction::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composite_expand() {
|
||||
let hold_reason: RuntimeHoldReason = pallet::HoldReason::Staking.into();
|
||||
let hold_reason2: RuntimeHoldReason = pallet2::HoldReason::Governance.into();
|
||||
let slash_reason: RuntimeSlashReason = pallet2::SlashReason::Equivocation.into();
|
||||
|
||||
assert_eq!(hold_reason, RuntimeHoldReason::Example(pallet::HoldReason::Staking));
|
||||
assert_eq!(hold_reason2, RuntimeHoldReason::Example2(pallet2::HoldReason::Governance));
|
||||
assert_eq!(slash_reason, RuntimeSlashReason::Example2(pallet2::SlashReason::Equivocation));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pallet_expand_deposit_event() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#[frame_support::pallet]
|
||||
mod pallet {
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(core::marker::PhantomData<T>);
|
||||
|
||||
#[pallet::composite_enum]
|
||||
pub enum HoldReasons {}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
error: expected one of: `FreezeReason`, `HoldReason`, `LockId`, `SlashReason`
|
||||
--> tests/pallet_ui/composite_enum_unsupported_identifier.rs:10:11
|
||||
|
|
||||
10 | pub enum HoldReasons {}
|
||||
| ^^^^^^^^^^^
|
||||
@@ -1,4 +1,4 @@
|
||||
error: Invalid pallet::event, expected item enum
|
||||
error: Invalid pallet::event, expected enum item
|
||||
--> $DIR/event_wrong_item.rs:19:2
|
||||
|
|
||||
19 | pub struct Foo;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#[frame_support::pallet]
|
||||
mod pallet {
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(core::marker::PhantomData<T>);
|
||||
|
||||
#[pallet::composite_enum]
|
||||
pub struct HoldReason;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Invalid pallet::composite_enum, expected enum item
|
||||
--> tests/pallet_ui/hold_reason_non_enum.rs:10:2
|
||||
|
|
||||
10 | pub struct HoldReason;
|
||||
| ^^^
|
||||
@@ -0,0 +1,14 @@
|
||||
#[frame_support::pallet]
|
||||
mod pallet {
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(core::marker::PhantomData<T>);
|
||||
|
||||
#[pallet::composite_enum]
|
||||
enum HoldReason {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Invalid pallet::composite_enum, `HoldReason` must be public
|
||||
--> tests/pallet_ui/hold_reason_not_pub.rs:10:5
|
||||
|
|
||||
10 | enum HoldReason {}
|
||||
| ^^^^
|
||||
@@ -0,0 +1,17 @@
|
||||
#[frame_support::pallet]
|
||||
mod pallet {
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(core::marker::PhantomData<T>);
|
||||
|
||||
#[pallet::composite_enum]
|
||||
pub enum LockId {}
|
||||
|
||||
#[pallet::composite_enum]
|
||||
pub enum LockId {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Invalid duplicated `LockId` definition
|
||||
--> tests/pallet_ui/lock_id_duplicate.rs:13:14
|
||||
|
|
||||
13 | pub enum LockId {}
|
||||
| ^^^^^^
|
||||
Reference in New Issue
Block a user