This repository has been 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.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
209 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
frame/support/procedural/src/construct_runtime/expand/hold_reason.rs
This file contains 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,60 @@ | ||
// 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]) -> TokenStream { | ||
let mut conversion_fns = Vec::new(); | ||
let hold_reason_variants = pallet_decls.iter().filter_map(|decl| { | ||
decl.find_part("HoldReason").map(|_| { | ||
let variant_name = &decl.name; | ||
let path = &decl.path; | ||
let index = &decl.index; | ||
|
||
conversion_fns.push(expand_conversion_fn(path, variant_name)); | ||
|
||
quote! { | ||
#[codec(index = #index)] | ||
#variant_name(#path::HoldReason), | ||
} | ||
}) | ||
}); | ||
|
||
quote! { | ||
#[derive( | ||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, Debug, | ||
TypeInfo, | ||
)] | ||
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 | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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,83 @@ | ||
// 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; | ||
|
||
mod keyword { | ||
syn::custom_keyword!(HoldReason); | ||
} | ||
|
||
pub struct HoldReasonDef { | ||
/// The index of the HoldReason item in the pallet module. | ||
pub index: usize, | ||
/// The HoldReason keyword used (contains span). | ||
pub hold_reason: keyword::HoldReason, | ||
/// The span of the pallet::hold_reason attribute. | ||
pub attr_span: proc_macro2::Span, | ||
} | ||
|
||
impl HoldReasonDef { | ||
pub fn try_from( | ||
attr_span: proc_macro2::Span, | ||
index: usize, | ||
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::hold_reason, expected enum item", | ||
)) | ||
}; | ||
|
||
if !matches!(item.vis, syn::Visibility::Public(_)) { | ||
let msg = "Invalid pallet::hold_reason, `HoldReason` must be public"; | ||
return Err(syn::Error::new(item.span(), msg)) | ||
} | ||
|
||
let has_derive_attr = item | ||
.attrs | ||
.iter() | ||
.find(|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) | ||
}) | ||
.is_some(); | ||
|
||
if !has_derive_attr { | ||
let derive_attr: syn::Attribute = syn::parse_quote! { | ||
#[derive( | ||
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, | ||
Debug, TypeInfo, | ||
)] | ||
}; | ||
item.attrs.push(derive_attr); | ||
} | ||
|
||
let hold_reason = syn::parse2::<keyword::HoldReason>(item.ident.to_token_stream())?; | ||
|
||
Ok(HoldReasonDef { index, hold_reason, attr_span }) | ||
} | ||
} |
This file contains 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 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
14 changes: 14 additions & 0 deletions
14
frame/support/test/tests/pallet_ui/hold_reason_non_enum.rs
This file contains 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,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::hold_reason] | ||
pub struct HoldReason; | ||
} | ||
|
||
fn main() { | ||
} |
5 changes: 5 additions & 0 deletions
5
frame/support/test/tests/pallet_ui/hold_reason_non_enum.stderr
This file contains 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,5 @@ | ||
error: Invalid pallet::hold_reason, expected enum item | ||
--> tests/pallet_ui/hold_reason_non_enum.rs:10:2 | ||
| | ||
10 | pub struct HoldReason; | ||
| ^^^ |
This file contains 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,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::hold_reason] | ||
enum HoldReason {} | ||
} | ||
|
||
fn main() { | ||
} |
5 changes: 5 additions & 0 deletions
5
frame/support/test/tests/pallet_ui/hold_reason_not_pub.stderr
This file contains 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,5 @@ | ||
error: Invalid pallet::hold_reason, `HoldReason` must be public | ||
--> tests/pallet_ui/hold_reason_not_pub.rs:10:5 | ||
| | ||
10 | enum HoldReason {} | ||
| ^^^^ |