Skip to main content

anchor_attribute_constant/
lib.rs

1extern crate proc_macro;
2
3/// A marker attribute used to mark const values that should be included in the
4/// generated IDL but functionally does nothing.
5#[proc_macro_attribute]
6pub fn constant(
7    _attr: proc_macro::TokenStream,
8    input: proc_macro::TokenStream,
9) -> proc_macro::TokenStream {
10    #[cfg(feature = "idl-build")]
11    {
12        use quote::quote;
13
14        #[allow(
15            clippy::unwrap_used,
16            reason = "attribute macro input is always a valid syn::Item"
17        )]
18        let ts = match syn::parse(input).unwrap() {
19            syn::Item::Const(item) => {
20                let idl_print = anchor_syn::idl::gen_idl_print_fn_constant(&item);
21                quote! {
22                    #item
23                    #idl_print
24                }
25            }
26            item => quote! {#item},
27        };
28
29        return proc_macro::TokenStream::from(quote! {
30            #ts
31        });
32    };
33
34    #[allow(unreachable_code)]
35    input
36}