Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture doc comments, add variant and field builders #87

Merged
merged 38 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
14bbbe9
Capture docs for types, fields, and enum variants
ascjones Apr 26, 2021
4e696dc
Add docs to derive tests
ascjones Apr 26, 2021
55fc739
Fmt
ascjones Apr 26, 2021
ded2f3f
Fmt
ascjones Apr 27, 2021
3b78d47
Make clippy happy
ascjones Apr 27, 2021
c74422d
Fix struct docs tests
ascjones Apr 27, 2021
329d915
Fix struct docs tests
ascjones Apr 27, 2021
637ab5a
Add missing Vec import
ascjones Apr 27, 2021
efdd3e4
Fix test
ascjones Apr 27, 2021
77fc88a
Clear docs
ascjones Apr 27, 2021
912fbcb
Merge branch 'master' into aj-docs
ascjones May 25, 2021
4151589
Promote build to it's own mod, split fields and variant builders
ascjones May 25, 2021
a0743b0
Refactor variant construction with builder
ascjones May 25, 2021
fd6f893
Fix up derive and unit tests with Variant builder
ascjones May 25, 2021
52f5e83
Fmt
ascjones May 25, 2021
2d2ab5c
Condense build module back to single file
ascjones May 25, 2021
c4bf49d
Fix up doc tests
ascjones May 25, 2021
05d875b
Fix up README
ascjones May 25, 2021
790a6b8
Define initial field builder
ascjones May 26, 2021
0f3b795
Fix up field building
ascjones May 26, 2021
655279a
Fix tests
ascjones May 26, 2021
63b986a
Fully qualify calls to stringify
ascjones May 26, 2021
150ef4e
Default impl for FieldBuilder
ascjones May 26, 2021
43551f4
Fix up default impl
ascjones May 26, 2021
0decd01
Fix spelling errors
ascjones May 26, 2021
e031939
Remove clear_docs
ascjones May 26, 2021
fdd96c3
Fully qualify module_path
ascjones May 26, 2021
585a671
Update derive/src/lib.rs
ascjones May 26, 2021
a7179b5
Merge remote-tracking branch 'origin/aj-docs' into aj-docs
ascjones May 26, 2021
4d73ae4
Use callback for variant builder
ascjones May 26, 2021
59ff44c
Update README
ascjones May 26, 2021
e8ea40b
Remove leading space in doc comments
ascjones May 26, 2021
1c01b94
Satisfy clippy
ascjones May 26, 2021
0edec05
Add test for doc capture
ascjones May 27, 2021
fcc8985
Rename index back to discriminant, changes for this will be in https:…
ascjones May 27, 2021
6fb8042
Rename index back to discriminant, changes for this will be in https:…
ascjones May 27, 2021
882bdb4
Update src/build.rs
ascjones May 27, 2021
ebc3e4b
Update src/build.rs
ascjones May 27, 2021
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
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ where
.path(Path::new("Foo", module_path!()))
.type_params(vec![MetaType::new::<T>()])
.composite(Fields::named()
.field_of::<T>("bar", "T")
.field_of::<u64>("data", "u64")
.field(|f| f.ty::<T>().name("bar").type_name("T"))
.field(|f| f.ty::<u64>().name("data").type_name("u64"))
)
}
}
Expand All @@ -125,8 +125,8 @@ impl TypeInfo for Foo {
Type::builder()
.path(Path::new("Foo", module_path!()))
.composite(Fields::unnamed()
.field_of::<u32>("u32")
.field_of::<bool>("bool")
.field(|f| f.ty::<u32>().type_name("u32"))
.field(|f| f.ty::<bool>().type_name("bool"))
)
}
}
Expand Down Expand Up @@ -155,10 +155,10 @@ where
.path(Path::new("Foo", module_path!()))
.type_params(vec![MetaType::new::<T>()])
.variant(
Variants::with_fields()
.variant("A", Fields::unnamed().field_of::<T>("T"))
.variant("B", Fields::named().field_of::<u32>("f", "u32"))
.variant("C", Fields::unit())
Variants::new()
.variant("A", |v| v.fields(Fields::unnamed().field(|f| f.ty::<T>())))
.variant("B", |v| v.fields(Fields::named().field(|f| f.ty::<u32>().name("f").type_name("u32"))))
.variant_unit("C")
)
}
}
Expand All @@ -181,10 +181,10 @@ impl TypeInfo for Foo {
Type::builder()
.path(Path::new("Foo", module_path!()))
.variant(
Variants::fieldless()
.variant("A", 1)
.variant("B", 2)
.variant("C", 33)
Variants::new()
.variant("A", |v| v.index(1))
.variant("B", |v| v.index(2))
.variant("C", |v| v.index(33))
)
}
}
Expand Down
71 changes: 44 additions & 27 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
extern crate proc_macro;

Expand Down Expand Up @@ -96,13 +94,16 @@ 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 type_info_impl = quote! {
impl #impl_generics :: #scale_info ::TypeInfo for #ident #ty_generics #where_clause {
type Identity = Self;
fn type_info() -> :: #scale_info ::Type {
:: #scale_info ::Type::builder()
.path(:: #scale_info ::Path::new(stringify!(#ident), module_path!()))
.path(:: #scale_info ::Path::new(::core::stringify!(#ident), ::core::module_path!()))
.type_params(:: #scale_info ::prelude::vec![ #( #generic_type_ids ),* ])
.docs(&[ #( #docs ),* ])
.#build_type
}
}
Expand Down Expand Up @@ -149,16 +150,25 @@ fn generate_fields(fields: &FieldsList) -> Vec<TokenStream2> {
StaticLifetimesReplace.visit_type_mut(&mut ty);

let type_name = clean_type_string(&quote!(#ty).to_string());
let method_call = if utils::is_compact(f) {
quote!(.compact_of::<#ty>)
let docs = utils::get_doc_literals(&f.attrs);
let type_of_method = if utils::is_compact(f) {
quote!(compact)
} else {
quote!(.field_of::<#ty>)
quote!(ty)
};
if let Some(ident) = ident {
quote!(#method_call(stringify!(#ident), #type_name))
let name = if let Some(ident) = ident {
quote!(.name(::core::stringify!(#ident)))
} else {
quote!(#method_call(#type_name))
}
quote!()
};
quote!(
.field(|f| f
.#type_of_method::<#ty>()
#name
.type_name(#type_name)
.docs(&[ #( #docs ),* ])
)
)
})
.collect()
}
Expand Down Expand Up @@ -214,13 +224,18 @@ 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);
quote! {
.variant(stringify!(#name), #discriminant as u64)
.variant(::core::stringify!(#name), |v|
v
.discriminant(#discriminant as ::core::primitive::u64)
.docs(&[ #( #docs ),* ])
)
}
});
quote! {
variant(
:: #scale_info ::build::Variants::fieldless()
:: #scale_info ::build::Variants::new()
#( #variants )*
)
}
Expand All @@ -245,39 +260,41 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea
.filter(|v| !utils::should_skip(&v.attrs))
.map(|v| {
let ident = &v.ident;
let v_name = quote! {stringify!(#ident) };
match v.fields {
let v_name = quote! {::core::stringify!(#ident) };
let docs = utils::get_doc_literals(&v.attrs);

let fields = match v.fields {
Fields::Named(ref fs) => {
let fields = generate_fields(&fs.named);
quote! {
.variant(
#v_name,
:: #scale_info::build::Fields::named()
#( #fields)*
)
:: #scale_info::build::Fields::named()
#( #fields )*
}
}
Fields::Unnamed(ref fs) => {
let fields = generate_fields(&fs.unnamed);
quote! {
.variant(
#v_name,
:: #scale_info::build::Fields::unnamed()
#( #fields)*
)
:: #scale_info::build::Fields::unnamed()
#( #fields )*
}
}
Fields::Unit => {
quote! {
.variant_unit(#v_name)
:: #scale_info::build::Fields::unit()
}
}
};

quote! {
.variant(#v_name, |v|
v.fields(#fields).docs(&[ #( #docs ),* ])
)
}
});
quote! {
variant(
:: #scale_info ::build::Variants::with_fields()
#( #variants)*
:: #scale_info ::build::Variants::new()
#( #variants )*
)
}
}
27 changes: 27 additions & 0 deletions derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
//!
//! NOTE: The code here is copied verbatim from `parity-scale-codec-derive`.

use alloc::{
string::ToString,
vec::Vec,
};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
parse_quote,
spanned::Spanned,
AttrStyle,
Attribute,
Expand All @@ -28,6 +33,28 @@ use syn::{
Variant,
};

/// Return all doc attributes literals found.
pub fn get_doc_literals(attrs: &[syn::Attribute]) -> Vec<syn::Lit> {
attrs
.iter()
.filter_map(|attr| {
if let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() {
if meta.path.get_ident().map_or(false, |ident| ident == "doc") {
let lit = &meta.lit;
let doc_lit = quote!(#lit).to_string();
let trimmed_doc_lit =
doc_lit.trim_start_matches(r#"" "#).trim_end_matches('"');
Some(parse_quote!(#trimmed_doc_lit))
} else {
None
}
} else {
None
}
})
.collect()
}

/// Look for a `#[codec(index = $int)]` attribute on a variant. If no attribute
/// is found, fall back to the discriminant or just the variant index.
pub fn variant_index(v: &Variant, i: usize) -> TokenStream {
Expand Down
Loading