From 4e9b4e2cb81f1c349d996a95cb6ff3337d460523 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Mon, 28 Nov 2022 00:03:34 +0900 Subject: [PATCH] Doctest experiment --- src/lib.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index fedf946..e5cdb30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,7 +59,9 @@ mod error; #[macro_use] mod quote; -use proc_macro::{Span, TokenStream}; +use std::iter; + +use proc_macro::{Span, TokenStream, TokenTree}; #[proc_macro_attribute] pub fn test(args: TokenStream, input: TokenStream) -> TokenStream { @@ -74,3 +76,32 @@ pub fn test(args: TokenStream, input: TokenStream) -> TokenStream { out.extend(input); out } + +#[doc(hidden)] +#[proc_macro_attribute] +pub fn __doc_crate_attr(args: TokenStream, input: TokenStream) -> TokenStream { + if !args.is_empty() { + return format_err!( + Span::call_site(), + "attribute must be of the form `#![__doc_crate_attr]`" + ) + .into_compile_error(); + } + let mut input = input.into_iter().peekable(); + let mut out = TokenStream::new(); + while let Some(t) = input.next() { + if let TokenTree::Ident(i) = &t { + if i.to_string() == "fn" { + if let Some(TokenTree::Ident(i)) = input.peek() { + if i.to_string() == "main" { + out.extend(quote! { + #[cfg_attr(coverage_nightly, no_coverage)] + }); + } + } + } + } + out.extend(iter::once(t)); + } + out +}