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()) (Default::default(), Default::default())
}; };
let docs = &type_value.docs;
expand.extend(quote::quote_spanned!(type_value.attr_span => expand.extend(quote::quote_spanned!(type_value.attr_span =>
#( #[doc = #docs] )*
#vis struct #ident<#struct_use_gen>(core::marker::PhantomData<((), #struct_use_gen)>); #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> impl<#struct_impl_gen> #frame_support::traits::Get<#type_> for #ident<#struct_use_gen>
#where_clause #where_clause
@@ -38,6 +38,8 @@ pub struct TypeValueDef {
pub where_clause: Option<syn::WhereClause>, pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::type_value attribute. /// The span of the pallet::type_value attribute.
pub attr_span: proc_macro2::Span, pub attr_span: proc_macro2::Span,
/// Docs on the item.
pub docs: Vec<syn::Lit>,
} }
impl TypeValueDef { impl TypeValueDef {
@@ -53,9 +55,18 @@ impl TypeValueDef {
return Err(syn::Error::new(item.span(), msg)) return Err(syn::Error::new(item.span(), msg))
}; };
if !item.attrs.is_empty() { let mut docs = vec![];
let msg = "Invalid pallet::type_value, unexpected attribute"; for attr in &item.attrs {
return Err(syn::Error::new(item.attrs[0].span(), msg)) 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 if let Some(span) = item
@@ -106,6 +117,7 @@ impl TypeValueDef {
type_, type_,
instances, instances,
where_clause, where_clause,
docs,
}) })
} }
} }
@@ -262,6 +262,7 @@ pub mod pallet {
#[pallet::storage_prefix = "Value2"] #[pallet::storage_prefix = "Value2"]
pub type RenamedValue<T> = StorageValue<Value = u64>; pub type RenamedValue<T> = StorageValue<Value = u64>;
/// Test some doc
#[pallet::type_value] #[pallet::type_value]
pub fn MyDefault<T: Config>() -> u16 pub fn MyDefault<T: Config>() -> u16
where where