use super::helper;
use quote::ToTokens;
use syn::spanned::Spanned;
mod keyword {
syn::custom_keyword!(pallet);
syn::custom_keyword!(Pallet);
syn::custom_keyword!(without_storage_info);
syn::custom_keyword!(storage_version);
}
pub struct PalletStructDef {
pub index: usize,
pub instances: Vec<helper::InstanceUsage>,
pub pallet: keyword::Pallet,
pub attr_span: proc_macro2::Span,
pub without_storage_info: Option<proc_macro2::Span>,
pub storage_version: Option<syn::Path>,
}
pub enum PalletStructAttr {
WithoutStorageInfoTrait(proc_macro2::Span),
StorageVersion { storage_version: syn::Path, span: proc_macro2::Span },
}
impl PalletStructAttr {
fn span(&self) -> proc_macro2::Span {
match self {
Self::WithoutStorageInfoTrait(span) | Self::StorageVersion { span, .. } => *span,
}
}
}
impl syn::parse::Parse for PalletStructAttr {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
input.parse::<syn::Token![#]>()?;
let content;
syn::bracketed!(content in input);
content.parse::<keyword::pallet>()?;
content.parse::<syn::Token![::]>()?;
let lookahead = content.lookahead1();
if lookahead.peek(keyword::without_storage_info) {
let span = content.parse::<keyword::without_storage_info>()?.span();
Ok(Self::WithoutStorageInfoTrait(span))
} else if lookahead.peek(keyword::storage_version) {
let span = content.parse::<keyword::storage_version>()?.span();
let version_content;
syn::parenthesized!(version_content in content);
let storage_version = version_content.parse::<syn::Path>()?;
Ok(Self::StorageVersion { storage_version, span })
} else {
Err(lookahead.error())
}
}
}
impl PalletStructDef {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Struct(item) = item {
item
} else {
let msg = "Invalid pallet::pallet, expected struct definition";
return Err(syn::Error::new(item.span(), msg))
};
let mut without_storage_info = None;
let mut storage_version_found = None;
let struct_attrs: Vec<PalletStructAttr> = helper::take_item_pallet_attrs(&mut item.attrs)?;
for attr in struct_attrs {
match attr {
PalletStructAttr::WithoutStorageInfoTrait(span)
if without_storage_info.is_none() =>
{
without_storage_info = Some(span);
},
PalletStructAttr::StorageVersion { storage_version, .. }
if storage_version_found.is_none() =>
{
storage_version_found = Some(storage_version);
},
attr => {
let msg = "Unexpected duplicated attribute";
return Err(syn::Error::new(attr.span(), msg))
},
}
}
let pallet = syn::parse2::<keyword::Pallet>(item.ident.to_token_stream())?;
if !matches!(item.vis, syn::Visibility::Public(_)) {
let msg = "Invalid pallet::pallet, Pallet must be public";
return Err(syn::Error::new(item.span(), msg))
}
if item.generics.where_clause.is_some() {
let msg = "Invalid pallet::pallet, where clause not supported on Pallet declaration";
return Err(syn::Error::new(item.generics.where_clause.span(), msg))
}
let instances =
vec![helper::check_type_def_gen_no_bounds(&item.generics, item.ident.span())?];
Ok(Self {
index,
instances,
pallet,
attr_span,
without_storage_info,
storage_version: storage_version_found,
})
}
}