|
| 1 | +extern crate proc_macro; |
| 2 | + |
| 3 | +use attribution::attr_args; |
| 4 | +use attribution::DynamicParameters; |
| 5 | +use attribution::ParamVal; |
| 6 | +use proc_macro::TokenStream; |
| 7 | +use proc_macro2::Span; |
| 8 | +use proc_macro2::TokenStream as TokenStream2; |
| 9 | +use quote::quote; |
| 10 | +use syn::Field; |
| 11 | +use syn::Fields; |
| 12 | +use syn::Ident; |
| 13 | +use syn::Item; |
| 14 | +use syn::ItemStruct; |
| 15 | + |
| 16 | +#[attr_args] |
| 17 | +struct GetterSetterArgs { |
| 18 | + field_names: DynamicParameters, |
| 19 | +} |
| 20 | + |
| 21 | +#[proc_macro_attribute] |
| 22 | +pub fn gs(attr: TokenStream, tagged: TokenStream) -> TokenStream { |
| 23 | + impl_gs(attr, tagged).into() |
| 24 | +} |
| 25 | + |
| 26 | +fn impl_gs( |
| 27 | + into_attr: impl Into<TokenStream2>, |
| 28 | + into_tagged: impl Into<TokenStream2>, |
| 29 | +) -> TokenStream2 { |
| 30 | + let attr = into_attr.into(); |
| 31 | + let tagged = into_tagged.into(); |
| 32 | + let args: GetterSetterArgs = syn::parse2(attr).expect("Unabled to parse attribute"); |
| 33 | + |
| 34 | + if let Ok(Item::Struct(tagged_struct)) = syn::parse2(tagged) { |
| 35 | + add_methods(tagged_struct, args.field_names) |
| 36 | + } else { |
| 37 | + panic!("The attribute can only be applied to structs") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +fn add_methods(tagged_struct: ItemStruct, parameters: DynamicParameters) -> TokenStream2 { |
| 42 | + if let Fields::Named(fields) = &tagged_struct.fields { |
| 43 | + // Gets all the methods for the identifiers that were marked as true |
| 44 | + let methods = fields |
| 45 | + .named |
| 46 | + .iter() |
| 47 | + .filter(|field| { |
| 48 | + let ident_text: String = field.ident.as_ref().unwrap().to_string(); |
| 49 | + |
| 50 | + *parameters |
| 51 | + .get(&ident_text) |
| 52 | + .map(|param_val| { |
| 53 | + if let ParamVal::Bool(b) = param_val { |
| 54 | + b |
| 55 | + } else { |
| 56 | + &false |
| 57 | + } |
| 58 | + }) |
| 59 | + .unwrap_or(&false) |
| 60 | + }) |
| 61 | + .map(make_method); |
| 62 | + |
| 63 | + let impl_block_name = tagged_struct.ident.clone(); |
| 64 | + quote! { |
| 65 | + #tagged_struct |
| 66 | + |
| 67 | + impl #impl_block_name { |
| 68 | + #(#methods)* |
| 69 | + } |
| 70 | + } |
| 71 | + } else { |
| 72 | + panic!("The tagged struct must have named fields") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn make_method(field: &Field) -> TokenStream2 { |
| 77 | + let field_ident = field.ident.as_ref().unwrap(); |
| 78 | + let field_type = &field.ty; |
| 79 | + let setter_field = field_ident.clone(); |
| 80 | + let setter_type = field_type.clone(); |
| 81 | + let setter_name = Ident::new( |
| 82 | + &format!("set_{}", field_ident.to_string()), |
| 83 | + Span::call_site(), |
| 84 | + ); |
| 85 | + |
| 86 | + let getter_field = field_ident.clone(); |
| 87 | + let getter_type = syn::TypeReference { |
| 88 | + and_token: <syn::Token![&]>::default(), |
| 89 | + lifetime: None, |
| 90 | + mutability: None, |
| 91 | + elem: Box::new(field_type.clone()), |
| 92 | + }; |
| 93 | + let getter_name = Ident::new( |
| 94 | + &format!("get_{}", field_ident.to_string()), |
| 95 | + Span::call_site(), |
| 96 | + ); |
| 97 | + |
| 98 | + quote! { |
| 99 | + fn #getter_name(&self) -> #getter_type { |
| 100 | + self.#getter_field |
| 101 | + } |
| 102 | + |
| 103 | + fn #setter_name(&mut self, new_val: #setter_type) { |
| 104 | + self.#setter_field = new_val; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[cfg(test)] |
| 110 | +mod tests { |
| 111 | + use super::*; |
| 112 | + use quote::ToTokens; |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn usage_test() { |
| 116 | + let raw_attr: syn::Attribute = syn::parse_quote! { |
| 117 | + #[gs(foo = true)] |
| 118 | + }; |
| 119 | + |
| 120 | + let raw_struct: Item = syn::parse_quote! { |
| 121 | + struct MyStruct { |
| 122 | + foo: u32, |
| 123 | + bar: bool, |
| 124 | + baz: String |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + let expected = quote! { |
| 129 | + struct MyStruct { |
| 130 | + foo: u32, |
| 131 | + bar: bool, |
| 132 | + baz: String |
| 133 | + } |
| 134 | + |
| 135 | + impl MyStruct { |
| 136 | + fn get_foo(&self) -> &u32 { |
| 137 | + self.foo |
| 138 | + } |
| 139 | + |
| 140 | + fn set_foo(&mut self, new_val: u32) { |
| 141 | + self.foo = new_val; |
| 142 | + } |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + if let proc_macro2::TokenTree::Group(group) = raw_attr.tts.into_iter().next().unwrap() { |
| 147 | + let actual = impl_gs(group.stream(), raw_struct.into_token_stream()); |
| 148 | + assert_eq!(actual.to_string(), expected.to_string()); |
| 149 | + } else { |
| 150 | + panic!() |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments