diff --git a/src/args.rs b/src/args.rs deleted file mode 100644 index 62894d3..0000000 --- a/src/args.rs +++ /dev/null @@ -1,39 +0,0 @@ -// Syntax: -// -// #[readonly::make(doc = mycratename_doc_cfg)] -// pub struct MyStruct {...} -// -// If provided, the `doc` attribute will make readonly fields fully pub, meaning -// writeable, if the given cfg is set. This way the readonly fields end up -// visible in rustdoc. The caller is responsible for providing a doc comment -// that explains that the field is readonly. -// -// Intended usage is by placing the following in Cargo.toml: -// -// [package.metadata.docs.rs] -// rustdoc-args = ["--cfg", "mycratename_doc_cfg"] - -use syn::parse::{Parse, ParseStream, Result}; -use syn::{Ident, Token}; - -mod kw { - syn::custom_keyword!(doc); -} - -pub struct Args { - pub doc_cfg: Option, -} - -impl Parse for Args { - fn parse(input: ParseStream) -> Result { - let doc_cfg = if input.is_empty() { - None - } else { - input.parse::()?; - input.parse::()?; - Some(input.parse()?) - }; - - Ok(Args { doc_cfg }) - } -} diff --git a/src/expand.rs b/src/expand.rs index 4edb779..fa35279 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -1,4 +1,3 @@ -use crate::args::Args; use proc_macro2::{Span, TokenStream}; use quote::quote; use syn::visit_mut::{self, VisitMut}; @@ -9,7 +8,7 @@ use syn::{ type Punctuated = syn::punctuated::Punctuated; -pub fn readonly(args: Args, input: DeriveInput) -> Result { +pub fn readonly(input: DeriveInput) -> Result { let call_site = Span::call_site(); match &input.data { @@ -27,12 +26,10 @@ pub fn readonly(args: Args, input: DeriveInput) -> Result { let indices = find_and_strip_readonly_attrs(&mut input); - let original_input = args.doc_cfg.as_ref().map(|doc_cfg| { - quote! { - #[cfg(all(#doc_cfg, doc))] - #input - } - }); + let original_input = quote! { + #[cfg(doc)] + #input + }; if !has_defined_repr(&input) { input.attrs.push(parse_quote!(#[repr(C)])); @@ -69,10 +66,7 @@ pub fn readonly(args: Args, input: DeriveInput) -> Result { readonly.ident = Ident::new(&format!("ReadOnly{}", input.ident), call_site); let readonly_ident = &readonly.ident; - if let Some(doc_cfg) = args.doc_cfg { - let not_doc_cfg = parse_quote!(#[cfg(not(all(#doc_cfg, doc)))]); - input.attrs.insert(0, not_doc_cfg); - } + input.attrs.insert(0, parse_quote!(#[cfg(not(doc))])); Ok(quote! { #original_input diff --git a/src/lib.rs b/src/lib.rs index 752c6b8..3978c29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,21 +70,20 @@ extern crate proc_macro; -mod args; mod expand; -use crate::args::Args; use proc_macro::TokenStream; use quote::quote; +use syn::parse::Nothing; use syn::{parse_macro_input, DeriveInput}; #[proc_macro_attribute] pub fn make(args: TokenStream, tokens: TokenStream) -> TokenStream { let original = tokens.clone(); - let args = parse_macro_input!(args as Args); + parse_macro_input!(args as Nothing); let input = parse_macro_input!(tokens as DeriveInput); - expand::readonly(args, input) + expand::readonly(input) .unwrap_or_else(|e| { let original = proc_macro2::TokenStream::from(original); let compile_error = e.to_compile_error();