Skip to content

Commit

Permalink
Merge pull request #946 from dtolnay/dochidden
Browse files Browse the repository at this point in the history
Pass through doc(hidden) attribute
  • Loading branch information
dtolnay authored Oct 5, 2021
2 parents e6cd18e + 9754a72 commit 722089e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
36 changes: 29 additions & 7 deletions syntax/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::syntax::{Derive, Doc, ForeignName};
use proc_macro2::{Ident, TokenStream};
use quote::ToTokens;
use syn::parse::{Nothing, Parse, ParseStream, Parser as _};
use syn::{Attribute, Error, LitStr, Path, Result, Token};
use syn::{parenthesized, token, Attribute, Error, LitStr, Path, Result, Token};

// Intended usage:
//
Expand Down Expand Up @@ -46,9 +46,12 @@ pub fn parse(cx: &mut Errors, attrs: Vec<Attribute>, mut parser: Parser) -> Othe
for attr in attrs {
if attr.path.is_ident("doc") {
match parse_doc_attribute.parse2(attr.tokens.clone()) {
Ok(lit) => {
Ok(attr) => {
if let Some(doc) = &mut parser.doc {
doc.push(lit);
match attr {
DocAttribute::Doc(lit) => doc.push(lit),
DocAttribute::Hidden => doc.hidden = true,
}
continue;
}
}
Expand Down Expand Up @@ -156,10 +159,29 @@ pub fn parse(cx: &mut Errors, attrs: Vec<Attribute>, mut parser: Parser) -> Othe
OtherAttrs(passthrough_attrs)
}

fn parse_doc_attribute(input: ParseStream) -> Result<LitStr> {
input.parse::<Token![=]>()?;
let lit: LitStr = input.parse()?;
Ok(lit)
enum DocAttribute {
Doc(LitStr),
Hidden,
}

mod kw {
syn::custom_keyword!(hidden);
}

fn parse_doc_attribute(input: ParseStream) -> Result<DocAttribute> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![=]) {
input.parse::<Token![=]>()?;
let lit: LitStr = input.parse()?;
Ok(DocAttribute::Doc(lit))
} else if lookahead.peek(token::Paren) {
let content;
parenthesized!(content in input);
content.parse::<kw::hidden>()?;
Ok(DocAttribute::Hidden)
} else {
Err(lookahead.error())
}
}

fn parse_derive_attribute(cx: &mut Errors, input: ParseStream) -> Result<Vec<Derive>> {
Expand Down
9 changes: 6 additions & 3 deletions syntax/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use quote::{quote, ToTokens};
use syn::LitStr;

pub struct Doc {
pub(crate) hidden: bool,
fragments: Vec<LitStr>,
}

impl Doc {
pub fn new() -> Self {
Doc {
hidden: false,
fragments: Vec::new(),
}
}
Expand All @@ -34,8 +36,9 @@ impl Doc {
impl ToTokens for Doc {
fn to_tokens(&self, tokens: &mut TokenStream) {
let fragments = &self.fragments;
tokens.extend(quote! {
#(#[doc = #fragments])*
});
tokens.extend(quote! { #(#[doc = #fragments])* });
if self.hidden {
tokens.extend(quote! { #[doc(hidden)] });
}
}
}

0 comments on commit 722089e

Please sign in to comment.