Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
clean up + cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Mar 7, 2023
1 parent f4a52ae commit edbcc99
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 52 deletions.
4 changes: 2 additions & 2 deletions frame/examples/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ 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 `<Self as pallet_balances::Config>`
// It is very unfortunate that we cannot have this have a default either, because it relies
// on `<Self as pallet_balances::Config>`
type MagicNumber: Get<Self::Balance>;

/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// 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
Expand Down
2 changes: 1 addition & 1 deletion frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
91 changes: 47 additions & 44 deletions frame/support/procedural/src/derive_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,85 +19,88 @@

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<syn::Ident, Token![,]>,
type_items: Punctuated<Ident, Token![,]>,
/// All of the function items that we must eventually implement.
fn_items: Punctuated<syn::Ident, Token![,]>,
fn_items: Punctuated<Ident, Token![,]>,
/// All of the constant items that we must eventually implement.
const_items: Punctuated<syn::Ident, Token![,]>,
const_items: Punctuated<Ident, Token![,]>,
}

impl syn::parse::Parse for DeriveImplDef {
fn parse(input: syn::parse::ParseStream) -> Result<Self> {
impl Parse for DeriveImplDef {
fn parse(input: ParseStream) -> Result<Self> {
// 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::<keywords::partial_impl_block>()?;
let _ = input.parse::<syn::Token![=]>()?;
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::<Token![=]>()?;
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::<keywords::implementing_type>()?;
let _ = input.parse::<syn::Token![=]>()?;
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::<Token![=]>()?;
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::<keywords::type_items>()?;
let _ = input.parse::<syn::Token![=]>()?;
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::<syn::Ident, Token![,]>::parse_terminated(&type_items)?;
let _ = input.parse::<Token![=]>()?;
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::<Ident, Token![,]>::parse_terminated(&type_items)?;

let mut fn_items;
let _ = input.parse::<keywords::fn_items>()?;
let _ = input.parse::<syn::Token![=]>()?;
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::<syn::Ident, Token![,]>::parse_terminated(&fn_items)?;
let _ = input.parse::<Token![=]>()?;
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::<Ident, Token![,]>::parse_terminated(&fn_items)?;

let mut const_items;
let _ = input.parse::<keywords::const_items>()?;
let _ = input.parse::<syn::Token![=]>()?;
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::<syn::Ident, Token![,]>::parse_terminated(&const_items)?;
let _ = input.parse::<Token![=]>()?;
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::<Ident, Token![,]>::parse_terminated(&const_items)?;

Ok(Self { partial_impl_block, type_items, fn_items, const_items, implementing_type })
}
}

pub(crate) fn derive_impl_inner(input: TokenStream) -> Result<TokenStream> {
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")
Expand Down Expand Up @@ -125,8 +128,8 @@ pub(crate) fn derive_impl_inner(input: TokenStream) -> Result<TokenStream> {
} 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)
}
Expand All @@ -136,7 +139,7 @@ pub(crate) fn derive_impl_inner(input: TokenStream) -> Result<TokenStream> {
}

pub fn derive_impl(attrs: TokenStream, input: TokenStream) -> Result<TokenStream> {
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);
Expand Down
2 changes: 1 addition & 1 deletion frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +36,6 @@ mod storage;
mod storage_alias;
mod transactional;
mod tt_macro;
mod derive_impl;

use proc_macro::TokenStream;
use quote::quote;
Expand Down
3 changes: 2 additions & 1 deletion frame/support/procedural/src/pallet/expand/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pub fn expand_config(def: &mut Def) -> proc_macro2::TokenStream {
)
.collect::<Vec<_>>();

// 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::<syn::Ident>::default();
let fn_names = Vec::<syn::Ident>::default();

Expand Down
4 changes: 3 additions & 1 deletion frame/support/procedural/src/pallet/parse/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attr>(item: &mut impl MutItemAttrs) -> syn::Result<Option<Attr>>
pub(crate) fn take_first_item_pallet_attr<Attr>(
item: &mut impl MutItemAttrs,
) -> syn::Result<Option<Attr>>
where
Attr: syn::parse::Parse,
{
Expand Down
1 change: 0 additions & 1 deletion frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit edbcc99

Please sign in to comment.