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        let ts = match syn::parse(input).unwrap() {
15            syn::Item::Const(item) => {
16                let idl_print = anchor_syn::idl::gen_idl_print_fn_constant(&item);
17                quote! {
18                    #item
19                    #idl_print
20                }
21            }
22            item => quote! {#item},
23        };
24
25        return proc_macro::TokenStream::from(quote! {
26            #ts
27        });
28    };
29
30    #[allow(unreachable_code)]
31    input
32}