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:
Keith Yeung
2023-04-04 20:28:46 +08:00
committed by GitHub
parent c268f3d3c8
commit 1a55f961c6
26 changed files with 682 additions and 11 deletions
@@ -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())
}