|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use quote::quote; |
| 3 | +use syn::parse::Parser; |
| 4 | +use syn::{parse, parse_macro_input, Field, Generics, Ident, ItemStruct}; |
| 5 | + |
| 6 | +/// Generate fields & implements of `Node` trait. |
| 7 | +/// |
| 8 | +/// Step 1: generate fields (`id`, `name`, `input_channel`, `output_channel`, `action`) |
| 9 | +/// |
| 10 | +/// Step 2: generates methods for `Node` implementation. |
| 11 | +/// |
| 12 | +/// Step 3: append the generated fields to the input struct. |
| 13 | +/// |
| 14 | +/// Step 4: return tokens of the input struct & the generated methods. |
| 15 | +pub(crate) fn auto_node(args: TokenStream, input: TokenStream) -> TokenStream { |
| 16 | + let mut item_struct = parse_macro_input!(input as ItemStruct); |
| 17 | + let _ = parse_macro_input!(args as parse::Nothing); |
| 18 | + |
| 19 | + let generics = &item_struct.generics; |
| 20 | + |
| 21 | + let field_id = syn::Field::parse_named |
| 22 | + .parse2(quote! { |
| 23 | + id: dagrs::NodeId |
| 24 | + }) |
| 25 | + .unwrap(); |
| 26 | + |
| 27 | + let field_name = syn::Field::parse_named |
| 28 | + .parse2(quote! { |
| 29 | + name: String |
| 30 | + }) |
| 31 | + .unwrap(); |
| 32 | + |
| 33 | + let field_in_channels = syn::Field::parse_named |
| 34 | + .parse2(quote! { |
| 35 | + input_channels: dagrs::InChannels |
| 36 | + }) |
| 37 | + .unwrap(); |
| 38 | + |
| 39 | + let field_out_channels = syn::Field::parse_named |
| 40 | + .parse2(quote! { |
| 41 | + output_channels: dagrs::OutChannels |
| 42 | + }) |
| 43 | + .unwrap(); |
| 44 | + |
| 45 | + let field_action = syn::Field::parse_named |
| 46 | + .parse2(quote! { |
| 47 | + action: Box<dyn dagrs::Action> |
| 48 | + }) |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + let auto_impl = auto_impl_node( |
| 52 | + &item_struct.ident, |
| 53 | + generics, |
| 54 | + &field_id, |
| 55 | + &field_name, |
| 56 | + &field_in_channels, |
| 57 | + &field_out_channels, |
| 58 | + &field_action, |
| 59 | + ); |
| 60 | + |
| 61 | + match item_struct.fields { |
| 62 | + syn::Fields::Named(ref mut fields) => { |
| 63 | + fields.named.push(field_id); |
| 64 | + fields.named.push(field_name); |
| 65 | + fields.named.push(field_in_channels); |
| 66 | + fields.named.push(field_out_channels); |
| 67 | + fields.named.push(field_action); |
| 68 | + } |
| 69 | + syn::Fields::Unit => { |
| 70 | + item_struct.fields = syn::Fields::Named(syn::FieldsNamed { |
| 71 | + named: [ |
| 72 | + field_id, |
| 73 | + field_name, |
| 74 | + field_in_channels, |
| 75 | + field_out_channels, |
| 76 | + field_action, |
| 77 | + ] |
| 78 | + .into_iter() |
| 79 | + .collect(), |
| 80 | + brace_token: Default::default(), |
| 81 | + }); |
| 82 | + } |
| 83 | + _ => { |
| 84 | + return syn::Error::new_spanned( |
| 85 | + item_struct.ident, |
| 86 | + "`auto_node` macro can only be annotated on named struct or unit struct.", |
| 87 | + ) |
| 88 | + .into_compile_error() |
| 89 | + .into() |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + return quote! { |
| 94 | + #item_struct |
| 95 | + #auto_impl |
| 96 | + } |
| 97 | + .into(); |
| 98 | +} |
| 99 | + |
| 100 | +fn auto_impl_node( |
| 101 | + struct_ident: &Ident, |
| 102 | + generics: &Generics, |
| 103 | + field_id: &Field, |
| 104 | + field_name: &Field, |
| 105 | + field_in_channels: &Field, |
| 106 | + field_out_channels: &Field, |
| 107 | + field_action: &Field, |
| 108 | +) -> proc_macro2::TokenStream { |
| 109 | + let mut impl_tokens = proc_macro2::TokenStream::new(); |
| 110 | + impl_tokens.extend([ |
| 111 | + impl_id(field_id), |
| 112 | + impl_name(field_name), |
| 113 | + impl_in_channels(field_in_channels), |
| 114 | + impl_out_channels(field_out_channels), |
| 115 | + impl_run(field_action, field_in_channels, field_out_channels), |
| 116 | + ]); |
| 117 | + |
| 118 | + quote::quote!( |
| 119 | + impl #generics dagrs::Node for #struct_ident #generics { |
| 120 | + #impl_tokens |
| 121 | + } |
| 122 | + unsafe impl #generics Send for #struct_ident #generics{} |
| 123 | + unsafe impl #generics Sync for #struct_ident #generics{} |
| 124 | + ) |
| 125 | +} |
| 126 | + |
| 127 | +fn impl_id(field: &Field) -> proc_macro2::TokenStream { |
| 128 | + let ident = &field.ident; |
| 129 | + quote::quote!( |
| 130 | + fn id(&self) -> dagrs::NodeId { |
| 131 | + self.#ident |
| 132 | + } |
| 133 | + ) |
| 134 | +} |
| 135 | + |
| 136 | +fn impl_name(field: &Field) -> proc_macro2::TokenStream { |
| 137 | + let ident = &field.ident; |
| 138 | + quote::quote!( |
| 139 | + fn name(&self) -> dagrs::NodeName { |
| 140 | + self.#ident.clone() |
| 141 | + } |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +fn impl_in_channels(field: &Field) -> proc_macro2::TokenStream { |
| 146 | + let ident = &field.ident; |
| 147 | + quote::quote!( |
| 148 | + fn input_channels(&mut self) -> &mut dagrs::InChannels { |
| 149 | + &mut self.#ident |
| 150 | + } |
| 151 | + ) |
| 152 | +} |
| 153 | + |
| 154 | +fn impl_out_channels(field: &Field) -> proc_macro2::TokenStream { |
| 155 | + let ident = &field.ident; |
| 156 | + quote::quote!( |
| 157 | + fn output_channels(&mut self) -> &mut dagrs::OutChannels { |
| 158 | + &mut self.#ident |
| 159 | + } |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +fn impl_run( |
| 164 | + field: &Field, |
| 165 | + field_in_channels: &Field, |
| 166 | + field_out_channels: &Field, |
| 167 | +) -> proc_macro2::TokenStream { |
| 168 | + let ident = &field.ident; |
| 169 | + let in_channels_ident = &field_in_channels.ident; |
| 170 | + let out_channels_ident = &field_out_channels.ident; |
| 171 | + quote::quote!( |
| 172 | + fn run(&mut self, env: std::sync::Arc<dagrs::EnvVar>) -> dagrs::Output { |
| 173 | + tokio::runtime::Runtime::new().unwrap().block_on(async { |
| 174 | + self.#ident |
| 175 | + .run(&mut self.#in_channels_ident, &self.#out_channels_ident, env) |
| 176 | + .await |
| 177 | + }) |
| 178 | + } |
| 179 | + ) |
| 180 | +} |
0 commit comments