lib.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use proc_macro::TokenStream;
  2. use quote::quote;
  3. use syn::{parse_macro_input, Fields, ItemStruct, Lit, Meta, NestedMeta};
  4. /// This macro attribute is used to define a BOLT system input.
  5. ///
  6. /// The input can be defined as a struct and will be transformed into an Anchor context.
  7. ///
  8. ///
  9. /// # Example
  10. /// ```ignore
  11. ///#[system_input]
  12. ///pub struct Components {
  13. /// pub position: Position,
  14. ///}
  15. ///
  16. /// ```
  17. #[proc_macro_attribute]
  18. pub fn system_input(_attr: TokenStream, item: TokenStream) -> TokenStream {
  19. // Parse the input TokenStream (the struct) into a Rust data structure
  20. let input = parse_macro_input!(item as ItemStruct);
  21. // Ensure the struct has named fields
  22. let fields = match &input.fields {
  23. Fields::Named(fields) => &fields.named,
  24. _ => panic!("system_input macro only supports structs with named fields"),
  25. };
  26. let name = &input.ident;
  27. // Collect imports for components
  28. let components_imports: Vec<_> = fields
  29. .iter()
  30. .filter_map(|field| {
  31. field.attrs.iter().find_map(|attr| {
  32. if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
  33. if meta_list.path.is_ident("component_id") {
  34. meta_list.nested.first().and_then(|nested_meta| {
  35. if let NestedMeta::Lit(Lit::Str(lit_str)) = nested_meta {
  36. let component_type =
  37. format!("bolt_types::Component{}", lit_str.value());
  38. if let Ok(parsed_component_type) =
  39. syn::parse_str::<syn::Type>(&component_type)
  40. {
  41. let field_type = &field.ty;
  42. let component_import = quote! {
  43. use #parsed_component_type as #field_type;
  44. };
  45. return Some(component_import);
  46. }
  47. }
  48. None
  49. })
  50. } else {
  51. None
  52. }
  53. } else {
  54. None
  55. }
  56. })
  57. })
  58. .collect();
  59. // Transform fields for the struct definition
  60. let transformed_fields = fields.iter().map(|f| {
  61. let field_name = &f.ident;
  62. let field_type = &f.ty;
  63. quote! {
  64. #[account()]
  65. pub #field_name: Account<'info, #field_type>,
  66. }
  67. });
  68. // Generate the new struct with the Accounts derive and transformed fields
  69. let output_struct = quote! {
  70. #[derive(Accounts)]
  71. pub struct #name<'info> {
  72. #(#transformed_fields)*
  73. pub authority: Signer<'info>,
  74. }
  75. };
  76. // Generate the try_to_vec method
  77. let try_to_vec_fields = fields.iter().map(|f| {
  78. let field_name = &f.ident;
  79. quote! {
  80. self.#field_name.try_to_vec()?
  81. }
  82. });
  83. let tuple_elements = (0..try_to_vec_fields.len())
  84. .map(|_| quote! {Vec<u8>})
  85. .collect::<Vec<_>>();
  86. let generated_tuple_type = match tuple_elements.len() {
  87. 0 => panic!("system_input macro only supports structs with named fields"),
  88. 1 => quote! { (Vec<u8>,) },
  89. _ => quote! { (#(#tuple_elements),*) },
  90. };
  91. // Generate the implementation of try_to_vec for the struct
  92. let output_impl = quote! {
  93. impl<'info> #name<'info> {
  94. pub fn try_to_vec(&self) -> Result<#generated_tuple_type> {
  95. Ok((#(#try_to_vec_fields,)*))
  96. }
  97. }
  98. };
  99. // Combine the struct definition and its implementation into the final TokenStream
  100. let output = quote! {
  101. #output_struct
  102. #output_impl
  103. #(#components_imports)*
  104. };
  105. TokenStream::from(output)
  106. }