@@ -154,32 +154,108 @@ impl Service {
154
154
} ) . unwrap ( )
155
155
}
156
156
157
+ pub fn client_method_body ( & self , cx : & mut ExtCtxt , method : & ServiceMethod ) -> P < ast:: Expr > {
158
+ let method_name = method. ident . clone ( ) ;
159
+ let struct_name_str = format ! ( "{}_{}_Args" , self . ident. clone( ) , method. ident. clone( ) ) ;
160
+
161
+ quote_expr ! ( cx, {
162
+ use std:: io:: Cursor ;
163
+ let ( res, future) = Future :: <( ThriftMessage , BinaryDeserializer <Cursor <Vec <u8 >>>) >:: channel( ) ;
164
+ let mut buf = Vec :: new( ) ;
165
+
166
+ {
167
+ let mut se = BinarySerializer :: new( & mut buf) ;
168
+ let args = FlockDb_query_Args {
169
+ voodoo: voodoo,
170
+ mission_control: mission_control
171
+ } ;
172
+
173
+ args. serialize( & mut se) ;
174
+ }
175
+
176
+ self . dispatcher. send( Incoming :: Call ( $method_name. to_string( ) , buf, res) ) . unwrap( ) ;
177
+ future. and_then( move |( msg, de) | {
178
+ Async :: Ok ( "foobar" . to_string( ) )
179
+ } )
180
+ } )
181
+ }
182
+
157
183
pub fn generate_client_service_impl ( & self , cx : & mut ExtCtxt ) -> P < ast:: Item > {
158
184
let mut ident = token:: str_to_ident ( & self . ident . clone ( ) ) ;
159
185
let struct_ident = token:: str_to_ident ( & format ! ( "{}Client" , self . ident. clone( ) ) ) ;
186
+ let mut methods = Vec :: new ( ) ;
187
+ let span = cx. call_site ( ) ;
160
188
161
- quote_item ! ( cx, impl $ident for $struct_ident {
162
- fn query( & mut self , voodoo: String , mission_control: i32 ) -> Future <String > {
163
- use std:: io:: Cursor ;
164
- let ( res, future) = Future :: <( ThriftMessage , BinaryDeserializer <Cursor <Vec <u8 >>>) >:: channel( ) ;
165
- let mut buf = Vec :: new( ) ;
189
+ // Generate a Rust method for each method.
190
+ for method in self . methods . iter ( ) {
191
+ let method_ident = token:: str_to_ident ( & method. ident ) ;
192
+ let self_ident = token:: str_to_ident ( "self" ) ;
193
+ let mut inputs = vec ! [
194
+ ast:: Arg :: new_self( span, ast:: Mutability :: Immutable , self_ident. clone( ) )
195
+ ] ;
196
+ let ty = method. ty . to_ast ( cx) ;
166
197
167
- {
168
- let mut se = BinarySerializer :: new( & mut buf) ;
169
- let args = FlockDb_query_Args {
170
- voodoo: voodoo,
171
- mission_control: mission_control
172
- } ;
198
+ for arg in method. args . iter ( ) {
199
+ let arg_ident = token:: str_to_ident ( & arg. ident ) ;
200
+ let arg_ty = arg. ty . to_ast ( cx) ;
201
+ inputs. push (
202
+ cx. arg ( span, arg_ident, arg_ty)
203
+ ) ;
204
+ }
173
205
174
- args. serialize( & mut se) ;
175
- }
206
+ let method_body = self . client_method_body ( cx, method) ;
207
+ let method_node = ast:: ImplItemKind :: Method (
208
+ ast:: MethodSig {
209
+ unsafety : ast:: Unsafety :: Normal ,
210
+ constness : ast:: Constness :: NotConst ,
211
+ abi : syntax:: abi:: Abi :: Rust ,
212
+ decl : P ( ast:: FnDecl {
213
+ inputs : inputs,
214
+ output : ast:: FunctionRetTy :: Ty ( quote_ty ! ( cx, Future <$ty>) ) ,
215
+ variadic : false
216
+ } ) ,
217
+ generics : ast:: Generics :: default ( ) ,
218
+ explicit_self : ast:: ExplicitSelf {
219
+ node : ast:: SelfKind :: Region ( None , ast:: Mutability :: Mutable , self_ident) ,
220
+ span : span
221
+ }
222
+ } ,
223
+ cx. block ( span, Vec :: new ( ) , Some ( method_body) )
224
+ ) ;
176
225
177
- self . dispatcher. send( Incoming :: Call ( "foobar123" . to_string( ) , buf, res) ) . unwrap( ) ;
178
- future. and_then( move |( msg, de) | {
179
- Async :: Ok ( "foobar" . to_string( ) )
180
- } )
181
- }
182
- } ) . unwrap ( )
226
+ let mut item = ast:: ImplItem {
227
+ id : ast:: DUMMY_NODE_ID ,
228
+ ident : method_ident,
229
+ vis : ast:: Visibility :: Inherited ,
230
+ defaultness : ast:: Defaultness :: Final ,
231
+ attrs : Vec :: new ( ) ,
232
+ node : method_node,
233
+ span : span
234
+ } ;
235
+
236
+ methods. push ( item) ;
237
+ }
238
+
239
+ let impl_item = ast:: ItemKind :: Impl (
240
+ ast:: Unsafety :: Normal ,
241
+ ast:: ImplPolarity :: Positive ,
242
+ ast:: Generics :: default ( ) ,
243
+ Some ( ast:: TraitRef {
244
+ path : ast:: Path :: from_ident ( span, vec ! [ struct_ident. clone( ) ] ) ,
245
+ ref_id : ast:: DUMMY_NODE_ID
246
+ } ) , // TraitRef
247
+ cx. ty_ident ( span, struct_ident) ,
248
+ methods
249
+ ) ;
250
+
251
+ P ( ast:: Item {
252
+ ident : token:: str_to_ident ( & self . ident . clone ( ) ) ,
253
+ attrs : Vec :: new ( ) ,
254
+ id : ast:: DUMMY_NODE_ID ,
255
+ node : impl_item,
256
+ vis : ast:: Visibility :: Inherit ,
257
+ span : span
258
+ } )
183
259
}
184
260
}
185
261
0 commit comments