Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Doctest experiment #1

Closed
wants to merge 1 commit into from
Closed
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
Doctest experiment
  • Loading branch information
taiki-e committed Nov 27, 2022
commit 4e9b4e2cb81f1c349d996a95cb6ff3337d460523
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}