Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP add docs feature
  • Loading branch information
ascjones committed Jun 23, 2021
commit ac69385af9578d7163e34a611862ce6b266e5524
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ std = [
derive = [
"scale-info-derive"
]
# Include code docs in type metadata.
docs = [
"scale-info-derive/docs"
]
# enables decoding and deserialization of portable scale-info type metadata
decode = [
"scale/full"
Expand Down
5 changes: 5 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ quote = "1.0"
syn = { version = "1.0", features = ["derive", "visit", "visit-mut", "extra-traits"] }
proc-macro2 = "1.0"
proc-macro-crate = "1"

[features]
default = []
# Include code docs in type metadata.
docs = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we want the docs to be included by default? In the steady-state future scenario we'll want the full metadata I think, and excluding it is something we want to do only while figuring out where to store it yes?

31 changes: 23 additions & 8 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> {
Data::Enum(ref e) => generate_variant_type(e, &scale_info),
Data::Union(_) => return Err(Error::new_spanned(input, "Unions not supported")),
};
let docs = utils::get_doc_literals(&ast.attrs);
let docs = generate_docs(&ast.attrs);

let type_info_impl = quote! {
impl #impl_generics :: #scale_info ::TypeInfo for #ident #ty_generics #where_clause {
Expand All @@ -113,7 +113,7 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> {
:: #scale_info ::Type::builder()
.path(:: #scale_info ::Path::new(::core::stringify!(#ident), ::core::module_path!()))
.type_params(:: #scale_info ::prelude::vec![ #( #generic_type_ids ),* ])
.docs(&[ #( #docs ),* ])
#docs
.#build_type
}
}
Expand Down Expand Up @@ -160,7 +160,7 @@ fn generate_fields(fields: &FieldsList) -> Vec<TokenStream2> {
StaticLifetimesReplace.visit_type_mut(&mut ty);

let type_name = clean_type_string(&quote!(#ty).to_string());
let docs = utils::get_doc_literals(&f.attrs);
let docs = generate_docs(&f.attrs);
let type_of_method = if utils::is_compact(f) {
quote!(compact)
} else {
Expand All @@ -176,7 +176,7 @@ fn generate_fields(fields: &FieldsList) -> Vec<TokenStream2> {
.#type_of_method::<#ty>()
#name
.type_name(#type_name)
.docs(&[ #( #docs ),* ])
#docs
)
)
})
Expand Down Expand Up @@ -235,12 +235,12 @@ fn generate_c_like_enum_def(variants: &VariantList, scale_info: &Ident) -> Token
.map(|(i, v)| {
let name = &v.ident;
let discriminant = utils::variant_index(v, i);
let docs = utils::get_doc_literals(&v.attrs);
let docs = generate_docs(&v.attrs);
quote! {
.variant(::core::stringify!(#name), |v|
v
.discriminant(#discriminant as ::core::primitive::u64)
.docs(&[ #( #docs ),* ])
#docs
)
}
});
Expand Down Expand Up @@ -272,7 +272,7 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea
.map(|v| {
let ident = &v.ident;
let v_name = quote! {::core::stringify!(#ident) };
let docs = utils::get_doc_literals(&v.attrs);
let docs = generate_docs(&v.attrs);
let index = utils::maybe_index(v).map(|i| quote!(.index(#i)));

let fields = match v.fields {
Expand Down Expand Up @@ -301,7 +301,7 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea
.variant(#v_name, |v|
v
.fields(#fields)
.docs(&[ #( #docs ),* ])
#docs
#index
)
}
Expand All @@ -313,3 +313,18 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea
)
}
}

// #[cfg(feature = "docs")]
#[cfg(not(feature = "docs"))]
fn generate_docs(attrs: &[syn::Attribute]) -> Option<TokenStream2> {
let docs = utils::get_doc_literals(attrs);
Some(quote! {
.docs(&[ #( #docs ),* ])
})
}

// #[cfg(not(feature = "docs"))]
#[cfg(feature = "docs")]
fn generate_docs(_: &[syn::Attribute]) -> Option<TokenStream2> {
None
}