handle doc on type_value (#10132)

This commit is contained in:
Guillaume Thiolliere
2021-11-04 11:11:39 +01:00
committed by GitHub
parent 15406835a8
commit 2755a97fa3
3 changed files with 19 additions and 3 deletions
@@ -59,7 +59,10 @@ pub fn expand_type_values(def: &mut Def) -> proc_macro2::TokenStream {
(Default::default(), Default::default())
};
let docs = &type_value.docs;
expand.extend(quote::quote_spanned!(type_value.attr_span =>
#( #[doc = #docs] )*
#vis struct #ident<#struct_use_gen>(core::marker::PhantomData<((), #struct_use_gen)>);
impl<#struct_impl_gen> #frame_support::traits::Get<#type_> for #ident<#struct_use_gen>
#where_clause
@@ -38,6 +38,8 @@ pub struct TypeValueDef {
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::type_value attribute.
pub attr_span: proc_macro2::Span,
/// Docs on the item.
pub docs: Vec<syn::Lit>,
}
impl TypeValueDef {
@@ -53,9 +55,18 @@ impl TypeValueDef {
return Err(syn::Error::new(item.span(), msg))
};
if !item.attrs.is_empty() {
let msg = "Invalid pallet::type_value, unexpected attribute";
return Err(syn::Error::new(item.attrs[0].span(), msg))
let mut docs = vec![];
for attr in &item.attrs {
if let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() {
if meta.path.get_ident().map_or(false, |ident| ident == "doc") {
docs.push(meta.lit);
continue
}
}
let msg = "Invalid pallet::type_value, unexpected attribute, only doc attribute are \
allowed";
return Err(syn::Error::new(attr.span(), msg))
}
if let Some(span) = item
@@ -106,6 +117,7 @@ impl TypeValueDef {
type_,
instances,
where_clause,
docs,
})
}
}