mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-09 14:07:58 +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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user