constant.rs 919 B

12345678910111213141516171819202122232425262728293031323334353637
  1. use heck::SnakeCase;
  2. use proc_macro2::TokenStream;
  3. use quote::{format_ident, quote};
  4. use super::{
  5. common::{gen_print_section, get_idl_module_path},
  6. defined::gen_idl_type,
  7. };
  8. pub fn gen_idl_print_fn_constant(item: &syn::ItemConst) -> TokenStream {
  9. let idl = get_idl_module_path();
  10. let name = item.ident.to_string();
  11. let expr = &item.expr;
  12. let fn_name = format_ident!("__anchor_private_print_idl_const_{}", name.to_snake_case());
  13. let fn_body = match gen_idl_type(&item.ty, &[]) {
  14. Ok((ty, _)) => gen_print_section(
  15. "const",
  16. quote! {
  17. #idl::IdlConst {
  18. name: #name.into(),
  19. ty: #ty,
  20. value: format!("{:?}", #expr),
  21. }
  22. },
  23. ),
  24. _ => quote! {},
  25. };
  26. quote! {
  27. #[test]
  28. pub fn #fn_name() {
  29. #fn_body
  30. }
  31. }
  32. }