proc_util_macros/
lib.rs

1use proc_macro::TokenStream;
2use syn::{parse::Nothing, parse_macro_input};
3
4/// Completely suppresses the attached item from being emitted into the surrounding source
5/// file. If you put this attribute last, the previous attributes will still see the item.
6///
7/// This is extremely useful for debugging in general, as it allows you to suppress mundane
8/// errors resulting from the generated code of an item so you can focus on compile errors that
9/// originated from proc macros related to that item.
10///
11/// ## Example
12///
13/// The following will compile only if `#[suppress_item]` is attached:
14///
15/// ```ignore
16/// #[suppress_item]
17/// fn invalid_rust() {
18///     return value_that_doesnt_exist;
19/// }
20/// ```
21///
22/// This compiles because the item expands to nothing, so rustc doesn't have a chance to detect
23/// any problems with this syntax.
24#[proc_macro_attribute]
25pub fn suppress_item(attr: TokenStream, _tokens: TokenStream) -> TokenStream {
26    parse_macro_input!(attr as Nothing);
27    TokenStream::new()
28}
29
30/// Similar to [`macro@suppress_item`], but will instead entirely replace the item this
31/// attribute is attached to with whatever item is specified in the attribute arguments.
32///
33/// ## Example
34///
35/// The following test will pass because `invalid_rust()` is not emitted and is instead
36/// replaced with `hello_world()`, which can then be referred to on subsequent lines:
37///
38/// ```ignore
39/// #[overwrite_with {
40///     fn hello_world() -> usize {
41///         return 3;
42///     }
43/// }]
44/// fn invalid_rust() {
45///     return nonexistent_value;
46/// }
47/// assert_eq!(hello_world(), 3);
48/// ```
49#[proc_macro_attribute]
50pub fn overwrite_with(attr: TokenStream, _tokens: TokenStream) -> TokenStream {
51    attr
52}