lib.rs 919 B

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