From edbcc991ddafddf2df39d36807917dad61b04d90 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Tue, 7 Mar 2023 16:37:35 -0500 Subject: [PATCH] clean up + cargo fmt --- frame/examples/basic/src/lib.rs | 4 +- frame/nomination-pools/src/lib.rs | 2 +- frame/support/procedural/src/derive_impl.rs | 91 ++++++++++--------- frame/support/procedural/src/lib.rs | 2 +- .../procedural/src/pallet/expand/config.rs | 3 +- .../procedural/src/pallet/parse/helper.rs | 4 +- frame/support/src/lib.rs | 1 - frame/system/src/lib.rs | 2 +- 8 files changed, 57 insertions(+), 52 deletions(-) diff --git a/frame/examples/basic/src/lib.rs b/frame/examples/basic/src/lib.rs index 389750a1503f4..56e7578ddda58 100644 --- a/frame/examples/basic/src/lib.rs +++ b/frame/examples/basic/src/lib.rs @@ -368,7 +368,8 @@ pub mod pallet { // Setting a constant config parameter from the runtime #[pallet::constant] #[pallet::no_default] - // It is very unfortunate that we cannot have this have a default either, because it relies on `` + // It is very unfortunate that we cannot have this have a default either, because it relies + // on `` type MagicNumber: Get; /// The overarching event type. @@ -376,7 +377,6 @@ pub mod pallet { /// Type representing the weight of this pallet type WeightInfo: WeightInfo; - } // Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and diff --git a/frame/nomination-pools/src/lib.rs b/frame/nomination-pools/src/lib.rs index 8557a08cbf756..f36ced2110d17 100644 --- a/frame/nomination-pools/src/lib.rs +++ b/frame/nomination-pools/src/lib.rs @@ -1171,7 +1171,7 @@ pub mod pallet { use super::*; use frame_support::traits::StorageVersion; use frame_system::{ensure_signed, pallet_prelude::*}; -use sp_core::parameter_types; + use sp_core::parameter_types; /// The current storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(3); diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 70c5901722a9f..f9b864dc9dbe5 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -19,73 +19,77 @@ use frame_support_procedural_tools::generate_crate_access_2018; use proc_macro2::TokenStream; -use quote::ToTokens; -use syn::{punctuated::Punctuated, ItemImpl, Result, Token}; +use syn::{ + braced, bracketed, + parse::{Parse, ParseStream}, + parse2, + punctuated::Punctuated, + token::{Brace, Bracket}, + Ident, ImplItem, ItemImpl, Result, Token, TypePath, +}; mod keywords { - syn::custom_keyword!(derive_impl); - syn::custom_keyword!(partial_impl_block); - syn::custom_keyword!(implementing_type); - syn::custom_keyword!(type_items); - syn::custom_keyword!(fn_items); - syn::custom_keyword!(const_items); + use syn::custom_keyword; + + custom_keyword!(derive_impl); + custom_keyword!(partial_impl_block); + custom_keyword!(implementing_type); + custom_keyword!(type_items); + custom_keyword!(fn_items); + custom_keyword!(const_items); } pub struct DeriveImplDef { /// The partial impl block that the user provides. This should be interpreted as "override". - partial_impl_block: syn::ItemImpl, + partial_impl_block: ItemImpl, /// The full path to the type that can be used to receive defaults form. - implementing_type: syn::TypePath, + implementing_type: TypePath, /// All of the associated type items that we must eventually implement. - type_items: Punctuated, + type_items: Punctuated, /// All of the function items that we must eventually implement. - fn_items: Punctuated, + fn_items: Punctuated, /// All of the constant items that we must eventually implement. - const_items: Punctuated, + const_items: Punctuated, } -impl syn::parse::Parse for DeriveImplDef { - fn parse(input: syn::parse::ParseStream) -> Result { +impl Parse for DeriveImplDef { + fn parse(input: ParseStream) -> Result { // NOTE: unfortunately, the order the keywords here must match what the pallet macro // expands. We can probably used a shared set of keywords later. let mut partial_impl_block; let _ = input.parse::()?; - let _ = input.parse::()?; - let _replace_with_bracket: syn::token::Bracket = - syn::bracketed!(partial_impl_block in input); - let _replace_with_brace: syn::token::Brace = - syn::braced!(partial_impl_block in partial_impl_block); + let _ = input.parse::()?; + let _replace_with_bracket: Bracket = bracketed!(partial_impl_block in input); + let _replace_with_brace: Brace = braced!(partial_impl_block in partial_impl_block); let partial_impl_block = partial_impl_block.parse()?; let mut implementing_type; let _ = input.parse::()?; - let _ = input.parse::()?; - let _replace_with_bracket: syn::token::Bracket = - syn::bracketed!(implementing_type in input); - let _replace_with_brace: syn::token::Brace = - syn::braced!(implementing_type in implementing_type); + let _ = input.parse::()?; + let _replace_with_bracket: Bracket = bracketed!(implementing_type in input); + let _replace_with_brace: Brace = braced!(implementing_type in implementing_type); let implementing_type = implementing_type.parse()?; let mut type_items; let _ = input.parse::()?; - let _ = input.parse::()?; - let _replace_with_bracket: syn::token::Bracket = syn::bracketed!(type_items in input); - let _replace_with_brace: syn::token::Brace = syn::braced!(type_items in type_items); - let type_items = Punctuated::::parse_terminated(&type_items)?; + let _ = input.parse::()?; + let _replace_with_bracket: Bracket = bracketed!(type_items in input); + let _replace_with_brace: Brace = braced!(type_items in type_items); + let type_items = Punctuated::::parse_terminated(&type_items)?; let mut fn_items; let _ = input.parse::()?; - let _ = input.parse::()?; - let _replace_with_bracket: syn::token::Bracket = syn::bracketed!(fn_items in input); - let _replace_with_brace: syn::token::Brace = syn::braced!(fn_items in fn_items); - let fn_items = Punctuated::::parse_terminated(&fn_items)?; + let _ = input.parse::()?; + let _replace_with_bracket: Bracket = bracketed!(fn_items in input); + let _replace_with_brace: Brace = braced!(fn_items in fn_items); + let fn_items = Punctuated::::parse_terminated(&fn_items)?; let mut const_items; let _ = input.parse::()?; - let _ = input.parse::()?; - let _replace_with_bracket: syn::token::Bracket = syn::bracketed!(const_items in input); - let _replace_with_brace: syn::token::Brace = syn::braced!(const_items in const_items); - let const_items = Punctuated::::parse_terminated(&const_items)?; + let _ = input.parse::()?; + let _replace_with_bracket: Bracket = bracketed!(const_items in input); + let _replace_with_brace: Brace = braced!(const_items in const_items); + let const_items = Punctuated::::parse_terminated(&const_items)?; Ok(Self { partial_impl_block, type_items, fn_items, const_items, implementing_type }) } @@ -93,11 +97,10 @@ impl syn::parse::Parse for DeriveImplDef { pub(crate) fn derive_impl_inner(input: TokenStream) -> Result { println!("input: {}", input); - let DeriveImplDef { partial_impl_block, implementing_type, type_items, .. } = - syn::parse2(input)?; + let DeriveImplDef { partial_impl_block, implementing_type, type_items, .. } = parse2(input)?; - let type_item_name = |i: &syn::ImplItem| { - if let syn::ImplItem::Type(t) = i { + let type_item_name = |i: &ImplItem| { + if let ImplItem::Type(t) = i { t.ident.clone() } else { panic!("only support type items for now") @@ -125,8 +128,8 @@ pub(crate) fn derive_impl_inner(input: TokenStream) -> Result { } else { // add it let tokens = quote::quote!(type #ident = <#implementing_type as #source_crate_path::pallet::DefaultConfig>::#ident;); - let parsed: syn::ImplItem = syn::parse2(tokens).expect("it is a valid type item"); - debug_assert!(matches!(parsed, syn::ImplItem::Type(_))); + let parsed: ImplItem = parse2(tokens).expect("it is a valid type item"); + debug_assert!(matches!(parsed, ImplItem::Type(_))); final_impl_block.items.push(parsed) } @@ -136,7 +139,7 @@ pub(crate) fn derive_impl_inner(input: TokenStream) -> Result { } pub fn derive_impl(attrs: TokenStream, input: TokenStream) -> Result { - let implementing_type: syn::TypePath = syn::parse2(attrs.clone())?; + let implementing_type: TypePath = parse2(attrs.clone())?; // ideas for sam: // let other_path_tokens = magic_macro!(path_to_other_path_token); // let foreign_trait_def_token: Syn::TraitItem = magic_macro!(frame_system::Config); diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 1b0ea2fda7008..23d9d9e672616 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -25,6 +25,7 @@ mod construct_runtime; mod crate_version; mod debug_no_bound; mod default_no_bound; +mod derive_impl; mod dummy_part_checker; mod key_prefix; mod match_and_insert; @@ -35,7 +36,6 @@ mod storage; mod storage_alias; mod transactional; mod tt_macro; -mod derive_impl; use proc_macro::TokenStream; use quote::quote; diff --git a/frame/support/procedural/src/pallet/expand/config.rs b/frame/support/procedural/src/pallet/expand/config.rs index c488f89ab96c2..ff95a3a72a364 100644 --- a/frame/support/procedural/src/pallet/expand/config.rs +++ b/frame/support/procedural/src/pallet/expand/config.rs @@ -50,7 +50,8 @@ pub fn expand_config(def: &mut Def) -> proc_macro2::TokenStream { ) .collect::>(); - // we rarely use const and fns in config traits anyways... maybe not supporting them is good enough. + // we rarely use const and fns in config traits anyways... maybe not supporting them is good + // enough. let const_names = Vec::::default(); let fn_names = Vec::::default(); diff --git a/frame/support/procedural/src/pallet/parse/helper.rs b/frame/support/procedural/src/pallet/parse/helper.rs index 3ea39d677583a..26ef0f1d59aba 100644 --- a/frame/support/procedural/src/pallet/parse/helper.rs +++ b/frame/support/procedural/src/pallet/parse/helper.rs @@ -47,7 +47,9 @@ pub trait MutItemAttrs { } /// Take the first pallet attribute (e.g. attribute like `#[pallet..]`) and decode it to `Attr` -pub(crate) fn take_first_item_pallet_attr(item: &mut impl MutItemAttrs) -> syn::Result> +pub(crate) fn take_first_item_pallet_attr( + item: &mut impl MutItemAttrs, +) -> syn::Result> where Attr: syn::parse::Parse, { diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 21c43d850aee6..dce086cb4c54b 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -215,7 +215,6 @@ pub use frame_support_procedural::derive_impl; #[doc(hidden)] pub use frame_support_procedural::derive_impl_inner; - /// Create new implementations of the [`Get`](crate::traits::Get) trait. /// /// The so-called parameter type can be created in four different ways: diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 3c1a05b647a63..012ce99d9da2f 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -205,8 +205,8 @@ pub mod pallet { use super::*; pub mod testing { type AccountId = u64; - use sp_runtime::traits::IdentityLookup; use super::*; + use sp_runtime::traits::IdentityLookup; pub struct Impl {} impl DefaultConfig for Impl {