use crate::pallet::Def;
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{spanned::Spanned, Attribute, Lit, LitStr};
const DOC: &'static str = "doc";
const PALLET_DOC: &'static str = "pallet_doc";
fn parse_pallet_doc_value(attr: &Attribute) -> syn::Result<DocMetaValue> {
let lit: syn::LitStr = attr.parse_args().map_err(|_| {
let msg = "The `pallet_doc` received an unsupported argument. Supported format: `pallet_doc(\"PATH\")`";
syn::Error::new(attr.span(), msg)
})?;
Ok(DocMetaValue::Path(lit))
}
fn parse_doc_value(attr: &Attribute) -> syn::Result<Option<DocMetaValue>> {
if !attr.path().is_ident(DOC) {
return Ok(None);
}
let meta = attr.meta.require_name_value()?;
match &meta.value {
syn::Expr::Lit(lit) => Ok(Some(DocMetaValue::Lit(lit.lit.clone()))),
syn::Expr::Macro(mac) if mac.mac.path.is_ident("include_str") => {
Ok(Some(DocMetaValue::Path(mac.mac.parse_body()?)))
},
_ => {
Err(syn::Error::new(attr.span(), "Expected `= \"docs\"` or `= include_str!(\"PATH\")`"))
},
}
}
#[derive(Debug)]
enum DocMetaValue {
Lit(Lit),
Path(LitStr),
}
impl ToTokens for DocMetaValue {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
DocMetaValue::Lit(lit) => lit.to_tokens(tokens),
DocMetaValue::Path(path_lit) => {
let decl = quote::quote!(include_str!(#path_lit));
tokens.extend(decl)
},
}
}
}
pub fn expand_documentation(def: &mut Def) -> proc_macro2::TokenStream {
let frame_support = &def.frame_support;
let type_impl_gen = &def.type_impl_generics(proc_macro2::Span::call_site());
let type_use_gen = &def.type_use_generics(proc_macro2::Span::call_site());
let pallet_ident = &def.pallet_struct.pallet;
let where_clauses = &def.config.where_clause;
let mut pallet_docs = Vec::with_capacity(def.item.attrs.len());
let mut index = 0;
while index < def.item.attrs.len() {
let attr = &def.item.attrs[index];
if attr.path().get_ident().map_or(false, |i| *i == PALLET_DOC) {
pallet_docs.push(def.item.attrs.remove(index));
continue;
}
index += 1;
}
let docs = match def
.item
.attrs
.iter()
.filter_map(|v| parse_doc_value(v).transpose())
.collect::<syn::Result<Vec<_>>>()
{
Ok(r) => r,
Err(err) => return err.into_compile_error(),
};
let pallet_docs = match pallet_docs
.into_iter()
.map(|attr| parse_pallet_doc_value(&attr))
.collect::<syn::Result<Vec<_>>>()
{
Ok(docs) => docs,
Err(err) => return err.into_compile_error(),
};
let docs = docs.iter().chain(pallet_docs.iter());
quote::quote!(
impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clauses{
#[doc(hidden)]
pub fn pallet_documentation_metadata()
-> #frame_support::__private::Vec<&'static str>
{
#frame_support::__private::vec![ #( #docs ),* ]
}
}
)
}