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

Adding support for doc comments on EnumDiscriminants generated type… #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions strum_macros/src/helpers/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod kw {
custom_keyword!(derive);
custom_keyword!(name);
custom_keyword!(vis);
custom_keyword!(doc);

// variant metadata
custom_keyword!(message);
Expand Down Expand Up @@ -58,6 +59,7 @@ pub enum EnumDiscriminantsMeta {
Derive { kw: kw::derive, paths: Vec<Path> },
Name { kw: kw::name, name: Ident },
Vis { kw: kw::vis, vis: Visibility },
Doc { kw: kw::doc, doc: LitStr },
Other { path: Path, nested: TokenStream },
}

Expand All @@ -84,6 +86,12 @@ impl Parse for EnumDiscriminantsMeta {
parenthesized!(content in input);
let vis = content.parse()?;
Ok(EnumDiscriminantsMeta::Vis { kw, vis })
} else if input.peek(kw::doc) {
let kw = input.parse()?;
let content;
parenthesized!(content in input);
let doc = content.parse()?;
Ok(EnumDiscriminantsMeta::Doc { kw, doc })
} else {
let path = input.parse()?;
let content;
Expand All @@ -100,6 +108,7 @@ impl Spanned for EnumDiscriminantsMeta {
EnumDiscriminantsMeta::Derive { kw, .. } => kw.span,
EnumDiscriminantsMeta::Name { kw, .. } => kw.span,
EnumDiscriminantsMeta::Vis { kw, .. } => kw.span,
EnumDiscriminantsMeta::Doc { kw, .. } => kw.span,
EnumDiscriminantsMeta::Other { path, .. } => path.span(),
}
}
Expand Down
6 changes: 5 additions & 1 deletion strum_macros/src/helpers/type_props.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;
use std::default::Default;
use syn::{DeriveInput, Ident, Path, Visibility};
use syn::{DeriveInput, Ident, LitStr, Path, Visibility};

use super::case_style::CaseStyle;
use super::metadata::{DeriveInputExt, EnumDiscriminantsMeta, EnumMeta};
Expand All @@ -18,6 +18,7 @@ pub struct StrumTypeProperties {
pub discriminant_name: Option<Ident>,
pub discriminant_others: Vec<TokenStream>,
pub discriminant_vis: Option<Visibility>,
pub discriminant_docs: Vec<LitStr>,
}

impl HasTypeProperties for DeriveInput {
Expand Down Expand Up @@ -64,6 +65,9 @@ impl HasTypeProperties for DeriveInput {
vis_kw = Some(kw);
output.discriminant_vis = Some(vis);
}
EnumDiscriminantsMeta::Doc { doc, .. } => {
output.discriminant_docs.push(doc);
}
EnumDiscriminantsMeta::Other { path, nested } => {
output.discriminant_others.push(quote! { #path(#nested) });
}
Expand Down
11 changes: 10 additions & 1 deletion strum_macros/src/macros/enum_discriminants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
#[derive(Clone, Copy, Debug, PartialEq, Eq, #(#derives),*)]
};

// Create #[doc] attrs for new generated type.
let docs = type_properties.discriminant_docs;

let docs = quote! {
#(#[doc = #docs])*
};

println!("{:?}", docs);

// Work out the name
let default_name = syn::Ident::new(
&format!("{}Discriminants", name.to_string()),
Expand Down Expand Up @@ -125,7 +134,7 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
};

Ok(quote! {
/// Auto-generated discriminant enum variants
#docs
#derives
#(#[ #pass_though_attributes ])*
#discriminants_vis enum #discriminants_name {
Expand Down