This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Implement #[pallet::composite_enum] #13722
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dc5050b
Implement #[pallet::hold_reason]
KiChjang 284cf9a
Appease clippy
KiChjang d179822
cargo fmt
KiChjang e24a75d
Merge remote-tracking branch 'origin/master' into kckyeung/pallet-hol…
KiChjang 30d7c4a
Update test expectations
KiChjang c9e8b5f
Update test expectations
KiChjang 1dfea46
Support composite_enum attribute instead
KiChjang 5ae02ee
Update test expectations
KiChjang 5203114
Change hold_reason to composite_enum
KiChjang cda2f56
Add UI test for unsupported identifier when using composite_enum
KiChjang 5c8f46a
Fix comment
KiChjang f52054b
Add documentation for pallet::composable_enum
KiChjang 0a0502a
More docs
KiChjang b2b36d2
Merge remote-tracking branch 'origin/master' into kckyeung/pallet-hol…
KiChjang cc0353f
cargo fmt
KiChjang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
frame/support/procedural/src/construct_runtime/expand/freeze_reason.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
frame/support/procedural/src/construct_runtime/expand/hold_reason.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
frame/support/procedural/src/construct_runtime/expand/lock_id.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
frame/support/procedural/src/construct_runtime/expand/slash_reason.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.