Skip to content

Commit 42b5450

Browse files
committed
f cleanup macro
1 parent 2e2df96 commit 42b5450

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

ext-test-macro/src/lib.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use proc_macro::TokenStream;
22
use proc_macro2::TokenStream as TokenStream2;
33
use quote::quote;
4-
use syn::{parse2, ItemFn, ItemMod};
4+
use syn::{parse_macro_input, Item};
55

66
/// An exposed test. This is a test that will run locally and also be
77
/// made available to other crates that want to run it in their own context.
@@ -40,34 +40,41 @@ use syn::{parse2, ItemFn, ItemMod};
4040
/// is on.
4141
#[proc_macro_attribute]
4242
pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
43-
let input = syn::parse_macro_input!(item as TokenStream2);
44-
let attrs = syn::parse_macro_input!(attrs as TokenStream2);
43+
let attrs = parse_macro_input!(attrs as TokenStream2);
44+
let input = parse_macro_input!(item as Item);
4545

46-
let expanded = if is_module_definition(input.clone()) {
47-
let cfg = if attrs.is_empty() {
48-
quote! { #[cfg(test)] }
49-
} else {
50-
quote! { #[cfg(any(test, #attrs))] }
51-
};
52-
quote! {
53-
#cfg
54-
#input
46+
let expanded = match input {
47+
Item::Mod(item_mod) => {
48+
let cfg = if attrs.is_empty() {
49+
quote! { #[cfg(test)] }
50+
} else {
51+
quote! { #[cfg(any(test, #attrs))] }
52+
};
53+
quote! {
54+
#cfg
55+
#item_mod
56+
}
5557
}
56-
} else if is_function_definition(input.clone()) {
57-
quote! {
58-
#[cfg_attr(test, ::core::prelude::v1::test)]
59-
#input
58+
Item::Fn(item_fn) => {
59+
let cfg_attr = if attrs.is_empty() {
60+
quote! { #[cfg(test)] }
61+
} else {
62+
quote! { #[cfg(any(test, #attrs))] }
63+
};
64+
quote! {
65+
#cfg_attr
66+
#item_fn
67+
}
68+
}
69+
_ => {
70+
return syn::Error::new_spanned(
71+
input,
72+
"xtest can only be applied to functions or modules",
73+
)
74+
.to_compile_error()
75+
.into();
6076
}
61-
} else {
62-
panic!("xtest can only be applied to functions or modules");
6377
};
64-
expanded.into()
65-
}
66-
67-
fn is_module_definition(input: TokenStream2) -> bool {
68-
parse2::<ItemMod>(input).is_ok()
69-
}
7078

71-
fn is_function_definition(input: TokenStream2) -> bool {
72-
parse2::<ItemFn>(input).is_ok()
79+
TokenStream::from(expanded)
7380
}

0 commit comments

Comments
 (0)