Support pallet::storage conditional compilation (#8324)

* Support pallet::storage conditional compilation.

* Add docs for cfg attributes.

* Keep strong types for get cfg attrs return.

* Update frame/support/procedural/src/pallet/parse/helper.rs

* Update frame/support/procedural/src/pallet/parse/storage.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Shaun Wang
2021-03-16 21:51:51 +13:00
committed by GitHub
parent 900bf2832a
commit 844e25522c
13 changed files with 117 additions and 12 deletions
@@ -47,7 +47,7 @@ pub trait MutItemAttrs {
}
/// Take the first pallet attribute (e.g. attribute like `#[pallet..]`) and decode it to `Attr`
pub fn take_first_item_attr<Attr>(item: &mut impl MutItemAttrs) -> syn::Result<Option<Attr>> where
pub fn take_first_item_pallet_attr<Attr>(item: &mut impl MutItemAttrs) -> syn::Result<Option<Attr>> where
Attr: syn::parse::Parse,
{
let attrs = if let Some(attrs) = item.mut_item_attrs() {
@@ -69,18 +69,29 @@ pub fn take_first_item_attr<Attr>(item: &mut impl MutItemAttrs) -> syn::Result<O
}
/// Take all the pallet attributes (e.g. attribute like `#[pallet..]`) and decode them to `Attr`
pub fn take_item_attrs<Attr>(item: &mut impl MutItemAttrs) -> syn::Result<Vec<Attr>> where
pub fn take_item_pallet_attrs<Attr>(item: &mut impl MutItemAttrs) -> syn::Result<Vec<Attr>> where
Attr: syn::parse::Parse,
{
let mut pallet_attrs = Vec::new();
while let Some(attr) = take_first_item_attr(item)? {
while let Some(attr) = take_first_item_pallet_attr(item)? {
pallet_attrs.push(attr)
}
Ok(pallet_attrs)
}
/// Get all the cfg attributes (e.g. attribute like `#[cfg..]`) and decode them to `Attr`
pub fn get_item_cfg_attrs(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> {
attrs.iter().filter_map(|attr| {
if attr.path.segments.first().map_or(false, |segment| segment.ident == "cfg") {
Some(attr.clone())
} else {
None
}
}).collect::<Vec<_>>()
}
impl MutItemAttrs for syn::Item {
fn mut_item_attrs(&mut self) -> Option<&mut Vec<syn::Attribute>> {
match self {