Skip to content

Commit 455b2bf

Browse files
author
Peter Glotfelty
committed
Merge branch 'master' of https://github.com/linclelinkpart5/strum into linclelinkpart5-master-2
2 parents 85d0ae6 + e56136f commit 455b2bf

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

strum_macros/src/helpers/metadata.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod kw {
2626
custom_keyword!(derive);
2727
custom_keyword!(name);
2828
custom_keyword!(vis);
29+
custom_keyword!(doc);
2930

3031
// variant metadata
3132
custom_keyword!(message);
@@ -115,6 +116,7 @@ pub enum EnumDiscriminantsMeta {
115116
Derive { _kw: kw::derive, paths: Vec<Path> },
116117
Name { kw: kw::name, name: Ident },
117118
Vis { kw: kw::vis, vis: Visibility },
119+
Doc { _kw: kw::doc, doc: LitStr },
118120
Other { path: Path, nested: TokenStream },
119121
}
120122

@@ -141,6 +143,11 @@ impl Parse for EnumDiscriminantsMeta {
141143
parenthesized!(content in input);
142144
let vis = content.parse()?;
143145
Ok(EnumDiscriminantsMeta::Vis { kw, vis })
146+
} else if input.peek(kw::doc) {
147+
let _kw = input.parse()?;
148+
input.parse::<Token![=]>()?;
149+
let doc = input.parse()?;
150+
Ok(EnumDiscriminantsMeta::Doc { _kw, doc })
144151
} else {
145152
let path = input.parse()?;
146153
let content;

strum_macros/src/helpers/type_props.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct StrumTypeProperties {
2626
pub prefix: Option<LitStr>,
2727
pub enum_repr: Option<TokenStream>,
2828
pub const_into_str: bool,
29+
pub discriminant_docs: Vec<LitStr>,
2930
}
3031

3132
impl HasTypeProperties for DeriveInput {
@@ -139,6 +140,9 @@ impl HasTypeProperties for DeriveInput {
139140
vis_kw = Some(kw);
140141
output.discriminant_vis = Some(vis);
141142
}
143+
EnumDiscriminantsMeta::Doc { doc, .. } => {
144+
output.discriminant_docs.push(doc);
145+
}
142146
EnumDiscriminantsMeta::Other { path, nested } => {
143147
output.discriminant_others.push(quote! { #path(#nested) });
144148
}

strum_macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ pub fn enum_properties(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
853853
/// # #[allow(dead_code)]
854854
/// #[derive(Debug, EnumDiscriminants)]
855855
/// #[strum_discriminants(derive(EnumString, EnumMessage))]
856+
/// #[strum_discriminants(doc = "This is the docstring on the generated type.")]
856857
/// enum MyEnum {
857858
/// #[strum_discriminants(strum(message = "Variant zero"))]
858859
/// Variant0(NonDefault),

strum_macros/src/macros/enum_discriminants.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
3030
#[derive(Clone, Copy, Debug, PartialEq, Eq, #(#derives),*)]
3131
};
3232

33+
// Create #[doc] attrs for new generated type.
34+
let docs = type_properties.discriminant_docs;
35+
36+
let docs = quote! {
37+
#(#[doc = #docs])*
38+
};
39+
3340
// Work out the name
3441
let default_name = syn::Ident::new(&format!("{}Discriminants", name), Span::call_site());
3542

@@ -181,7 +188,7 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
181188
};
182189

183190
Ok(quote! {
184-
/// Auto-generated discriminant enum variants
191+
#docs
185192
#derives
186193
#repr
187194
#(#[ #pass_though_attributes ])*

0 commit comments

Comments
 (0)