1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use heck::{ShoutySnakeCase, SnakeCase};
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::export::TokenStream2;
use syn::parse::{Parse, ParseStream};
use syn::{token, FnArg, ImplItem};
use syn::{Attribute, Ident, ReturnType, Visibility};

/// // request type
/// ```
/// #[derive(Serialize, Deserialize, Default, Debug)]
/// pub struct HelloRequest {}
/// ```
///
/// // response type
/// ```
/// #[derive(Serialize, Deserialize, Default, Debug)]
/// pub struct HelloReply {}
/// ```
///
/// // user defined trait (`ident`)
/// ```
/// #[service]
/// pub trait Greeter {
///     ...
/// }
/// ```
///
/// // generated create service function (`service_create_fn_ident`)
/// ```
/// pub fn create_greeter<S: Greeter + Send + Clone + 'static>(s: S) -> ::bincode_grpc::grpcio::Service {
///     let mut builder = ::bincode_grpc::grpcio::ServiceBuilder::new();
///     let mut instance = s;
///     builder = builder.add_unary_handler(&METHOD_GREETER_SAY_HELLO, move |ctx, req, resp| {
///         instance.say_hello(ctx, req, resp)
///     });
///     builder.build()
/// }
/// ```
struct Service {
    attrs: Vec<Attribute>,
    vis: Visibility,
    ident: Ident,
    rpcs: Vec<RpcMethod>,
}

impl Parse for Service {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let vis: Visibility = input.parse()?;
        input.parse::<token::Trait>()?;
        let ident: Ident = input.parse()?;
        let content;
        syn::braced!(content in input);
        let mut rpcs = Vec::<RpcMethod>::new();
        while !content.is_empty() {
            rpcs.push(content.parse()?);
        }

        Ok(Self {
            attrs,
            vis,
            ident,
            rpcs,
        })
    }
}

impl Service {
    fn service_create_fn_ident(&self) -> Ident {
        quote::format_ident!("create_{}", self.ident.to_string().as_str().to_snake_case())
    }

    fn client_ident(&self) -> Ident {
        quote::format_ident!("{}Client", self.ident)
    }

    fn client_struct(&self) -> TokenStream2 {
        let vis = &self.vis;
        let ident = self.client_ident();
        quote::quote! {
            #[derive(Clone)]
            #vis struct #ident {
                client: ::bincode_grpc::grpcio::Client,
            }
        }
    }

    fn client_deref(&self) -> TokenStream2 {
        let ident = self.client_ident();
        quote::quote! {
            impl std::ops::Deref for #ident {
                type Target = ::bincode_grpc::grpcio::Client;

                fn deref(&self) -> &Self::Target {
                    &self.client
                }
            }
        }
    }

    fn client_impl(&self) -> TokenStream2 {
        let vis = &self.vis;
        let ident = &self.ident;
        let client_methods: Vec<_> = self
            .rpcs
            .iter()
            .flat_map(|x| {
                vec![
                    x.client_method(),
                    x.client_method_opt(&ident),
                    x.client_method_async(),
                    x.client_method_async_opt(&ident),
                ]
            })
            .collect();

        let client_ident = self.client_ident();
        quote::quote! {
            impl #client_ident {
                #vis fn new(channel: ::bincode_grpc::grpcio::Channel) -> Self {
                    Self {
                        client: ::bincode_grpc::grpcio::Client::new(channel)
                    }
                }

                #( #vis #client_methods )*
            }
        }
    }

    fn method_declarations(&self) -> TokenStream2 {
        let vis = &self.vis;
        let ident = &self.ident;
        let method_declarations = self.rpcs.iter().map(|rpc| rpc.method_declaration(&ident));

        quote::quote! {
            #( #vis #method_declarations )*
        }
    }

    fn trait_service(&self) -> TokenStream2 {
        let attrs = &self.attrs;
        let vis = &self.vis;
        let ident = &self.ident;

        // let original_fns = self.rpcs.iter().map(|rpc| rpc.original_method());
        let grpc_fns = self.rpcs.iter().map(|rpc| rpc.grpc_method());

        quote::quote! {
            #( #attrs )*
            #vis trait #ident {
                // #( #original_fns )*
                #( #grpc_fns )*
            }
        }
    }

    fn create_service(&self) -> TokenStream2 {
        let vis = &self.vis;
        let ident = &self.ident;
        let fn_ident = self.service_create_fn_ident();
        let method_registrations = self.rpcs.iter().map(|rpc| {
            let declaration_ident = rpc.method_declaration_ident(ident);
            let grpc_ident = rpc.grpc_method_ident();
            quote::quote! {
                let mut instance = s.clone();
                builder = builder.add_unary_handler(&#declaration_ident, move |ctx, req, resp| {
                    instance.#grpc_ident(ctx, req, resp)
                });
            }
        });
        quote::quote! {
            #vis fn #fn_ident<S: #ident + Send + Clone + 'static>(s: S) -> ::bincode_grpc::grpcio::Service {
                let mut builder = ::bincode_grpc::grpcio::ServiceBuilder::new();
                #( #method_registrations )*
                builder.build()
            }
        }
    }
}

impl ToTokens for Service {
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        tokens.extend(vec![
            self.method_declarations(),
            self.trait_service(),
            self.create_service(),
            self.client_struct(),
            self.client_deref(),
            self.client_impl(),
        ])
    }
}

///
/// // generated grpc method declarations (`method_declaration_ident`)
/// ```
/// pub const METHOD_GREETER_SAY_HELLO: grpcio::Method<HelloRequest, HelloReply> = grpcio::Method {
///     ty: MethodType::Unary,
///     name: "hello",
///     req_mar: Marshaller {
///         ser: grpcio::bi_ser,
///         de: grpcio::bi_de,
///     },
///     resp_mar: Marshaller {
///         ser: grpcio::bi_ser,
///         de: grpcio::bi_de,
///     },
/// };
/// ```
///
/// Original trait method declaration:
/// ```
///     fn say_hello(
///         &self,
///         arg1: HelloRequest,
///     ) -> HelloReply;
/// ```
///
/// a transformed method should be added to the trait (`grpc_method_ident`):
/// ```
///     fn say_hello_grpc(
///         &mut self,
///         ctx: ::bincode_grpc::grpcio::RpcContext,
///         req: (HelloRequest, ), // the tuple is used for the case where the original trait method has multiple arguments
///         sink: ::bincode_grpc::grpcio::UnarySink<HelloReply>,
///     );
/// ```
struct RpcMethod {
    attrs: Vec<Attribute>,
    ident: Ident,
    args: Vec<syn::PatType>,
    receiver: syn::Receiver,
    output: ReturnType,
}

impl Parse for RpcMethod {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        input.parse::<token::Fn>()?;
        let ident: Ident = input.parse()?;
        let content;
        syn::parenthesized!(content in input);
        let mut args = vec![];
        let mut receiver = None;
        for arg in content.parse_terminated::<syn::FnArg, token::Comma>(syn::FnArg::parse)? {
            match arg {
                FnArg::Receiver(captures) => {
                    if captures.mutability.is_none() {
                        panic!("should be &mut self");
                    } else if receiver.is_some() {
                        panic!("duplicated self");
                    } else {
                        receiver = Some(captures);
                    }
                }
                FnArg::Typed(captures) => match *captures.pat {
                    syn::Pat::Ident(_) => args.push(captures),
                    _ => panic!("patterns aren't allowd in RPC args"),
                },
            }
        }
        let output: syn::ReturnType = input.parse()?;
        input.parse::<token::Semi>()?;
        Ok(Self {
            attrs,
            ident,
            args,
            receiver: receiver.unwrap(),
            output,
        })
    }
}

impl RpcMethod {
    /// `some_method` to `METHOD_SOME_METHOD`
    fn method_declaration_ident(&self, service_name: &Ident) -> Ident {
        quote::format_ident!(
            "{}_METHOD_{}",
            service_name.to_string().as_str().to_shouty_snake_case(),
            self.ident.to_string().as_str().to_shouty_snake_case()
        )
    }

    /// `method` to `method_grpc`
    fn grpc_method_ident(&self) -> Ident {
        quote::format_ident!("{}_grpc", self.ident)
    }

    /// original user defined methods
    fn original_method(&self) -> TokenStream2 {
        let attrs = &self.attrs;
        let ident = &self.ident;
        let args = &self.args;
        let receiver = &self.receiver;
        let output = &self.output;
        quote::quote! {
            #( #attrs )*
            fn #ident(#receiver, #( #args ),*) #output;
        }
    }

    fn req_type(&self) -> TokenStream2 {
        let args = &self.args;
        let all_arg_types: Vec<_> = args.iter().map(|x| &x.ty).collect();
        if all_arg_types.len() > 0 {
            quote::quote! {
                (#( #all_arg_types ),*,)
            }
        } else {
            quote::quote! {
                ()
            }
        }
    }

    fn resp_type(&self) -> TokenStream2 {
        let output = &self.output;
        let output = match output {
            ReturnType::Default => syn::Type::Verbatim(quote::quote! {()}),
            ReturnType::Type(_, t) => (**t).clone(),
        };
        quote::quote! {
            #output
        }
    }

    fn client_method(&self) -> TokenStream2 {
        let ident = &self.ident;
        let req_type = self.req_type();
        let resp_type = self.resp_type();
        let opt_method_ident = quote::format_ident!("{}_opt", ident);

        quote::quote! {
            fn #ident(&self, req: &#req_type) -> ::bincode_grpc::grpcio::Result<#resp_type> {
                self.#opt_method_ident(req, ::bincode_grpc::grpcio::CallOption::default())
            }
        }
    }

    fn client_method_opt(&self, server_name: &Ident) -> TokenStream2 {
        let ident = &self.ident;
        let req_type = self.req_type();
        let resp_type = self.resp_type();
        let opt_method_ident = quote::format_ident!("{}_opt", ident);
        let method_ident = self.method_declaration_ident(&server_name);

        quote::quote! {
            fn #opt_method_ident(&self, req: &#req_type, opt: ::bincode_grpc::grpcio::CallOption) -> ::bincode_grpc::grpcio::Result<#resp_type> {
                self.client.unary_call(&#method_ident, req, opt)
            }
        }
    }

    fn client_method_async_opt(&self, server_name: &Ident) -> TokenStream2 {
        let ident = &self.ident;
        let req_type = self.req_type();
        let resp_type = self.resp_type();
        let async_opt_method_ident = quote::format_ident!("{}_async_opt", ident);
        let method_ident = self.method_declaration_ident(&server_name);

        quote::quote! {
            fn #async_opt_method_ident(&self, req: &#req_type, opt: ::bincode_grpc::grpcio::CallOption) -> ::bincode_grpc::grpcio::Result<::grpcio::ClientUnaryReceiver<#resp_type>> {
                self.client.unary_call_async(&#method_ident, req, opt)
            }
        }
    }
    fn client_method_async(&self) -> TokenStream2 {
        let ident = &self.ident;
        let req_type = self.req_type();
        let resp_type = self.resp_type();
        let async_method_ident = quote::format_ident!("{}_async", ident);
        let async_opt_method_ident = quote::format_ident!("{}_async_opt", ident);

        quote::quote! {
            fn #async_method_ident(&self, req: &#req_type) -> ::bincode_grpc::grpcio::Result<::grpcio::ClientUnaryReceiver<#resp_type>> {
                self.#async_opt_method_ident(req, ::bincode_grpc::grpcio::CallOption::default())
            }
        }
    }

    /// transformed grpc compliant methods
    fn grpc_method(&self) -> TokenStream2 {
        let attrs = &self.attrs;
        let ident = &self.grpc_method_ident();
        let receiver = &self.receiver;
        let req_type = self.req_type();
        let resp_type = self.resp_type();

        quote::quote! {
            #( #attrs )*
            fn #ident(
                #receiver,
                ctx: ::bincode_grpc::grpcio::RpcContext,
                req: #req_type,
                sink: ::bincode_grpc::grpcio::UnarySink<#resp_type>
              );
        }
    }

    fn method_declaration(&self, service_name: &Ident) -> TokenStream2 {
        let ident = self.method_declaration_ident(&service_name);
        let req_type = self.req_type();
        let resp_type = self.resp_type();
        quote::quote! {
            const #ident: ::bincode_grpc::grpcio::Method<#req_type, #resp_type> = ::bincode_grpc::grpcio::Method {
                ty: ::bincode_grpc::grpcio::MethodType::Unary,
                name: stringify!(#ident),
                req_mar: ::bincode_grpc::grpcio::Marshaller {
                    ser: ::bincode_grpc::bi_codec::ser,
                    de: ::bincode_grpc::bi_codec::de,
                },
                resp_mar: ::bincode_grpc::grpcio::Marshaller {
                    ser: ::bincode_grpc::bi_codec::ser,
                    de: ::bincode_grpc::bi_codec::de,
                },
            };
        }
    }
}

#[proc_macro_attribute]
pub fn service(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
    syn::parse_macro_input!(tokens as Service)
        .into_token_stream()
        .into()
}

/// ```
/// struct GreeterServer;
///
/// #[server]
/// impl Greeter for GreeterServer {
///     fn say_hello(&mut self, req: HelloRequest) -> HelloReply {
///         HelloReply::default()
///     }
/// }
/// ```
///
/// The above code should generate
///
/// ```
/// impl Greeter for GreeterServer {
///     fn say_hello(&mut self, req: HelloRequest) -> HelloReply {
///         HelloReply::default()
///     }
///
///     fn say_hello_grpc(&mut self, ctx: RpcContext<'_>, req: (HelloRequest,), sink: UnarySink<HelloReply>) {
///         let mut resp = self.say_hello(req);
///         let f = sink
///             .success(resp)
///             .map_err(move |e| error!("failed to reply {:?}: {:?}", req, e))
///             .map(|_| ());
///         ctx.spawn(f)
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn server(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
    let mut item = syn::parse_macro_input!(tokens as syn::ItemImpl);
    let new_methods: Vec<_> = item
        .items
        .iter()
        .filter_map(|item| match item {
            ImplItem::Method(m) => Some(m),
            _ => None,
        })
        .map(|m| {
            let method_ident = &m.sig.ident;
            let vis = &m.vis;
            let grpc_method_ident = quote::format_ident!("{}_grpc", method_ident);

            let req_type = {
                let args = &m.sig.inputs;
                let all_arg_types: Vec<_> = args.iter().filter_map(|x| match x {
                    FnArg::Receiver(_) => None,
                    FnArg::Typed(x) => Some(x.ty.clone()),
                }).collect();
                if all_arg_types.len() > 0 {
                    quote::quote! {
                        (#( #all_arg_types ),*,)
                    }
                } else {
                    quote::quote! {
                        ()
                    }
                }
            };

            let req_args: Vec<_> = {
                let args = &m.sig.inputs;
                let all_arg_types = args.iter().filter_map(|x| match x {
                    FnArg::Receiver(_) => None,
                    FnArg::Typed(x) => Some(x.pat.clone()),
                });
                all_arg_types
            }.collect();

            let req_args2 = req_args.clone();

            let resp_type = {
                let output = &m.sig.output;
                let output = match output {
                    ReturnType::Default => syn::Type::Verbatim(quote::quote! {()}),
                    ReturnType::Type(_, t) => (**t).clone(),
                };
                quote::quote! {
                    #output
                }
            };

            if req_args.len() > 0 {
                quote::quote! {
                    #vis fn #grpc_method_ident(&mut self, ctx: ::bincode_grpc::grpcio::RpcContext, req: #req_type, sink: ::bincode_grpc::grpcio::UnarySink<#resp_type>) {
                         let (#( #req_args, )*) = req;
                         let mut resp = self.#method_ident(#( #req_args2, )* );
                         let f = sink
                             .success(resp)
                             .map_err(move |e| ::bincode_grpc::tracing::error!("failed to reply {:?}", e))
                             .map(|_| ());
                         ctx.spawn(f)
                    }
                }
            } else {
                quote::quote! {
                    #vis fn #grpc_method_ident(&mut self, ctx: ::bincode_grpc::grpcio::RpcContext, _req: #req_type, sink: ::bincode_grpc::grpcio::UnarySink<#resp_type>) {
                         let mut resp = self.#method_ident();
                         let f = sink
                             .success(resp)
                             .map_err(move |e| ::bincode_grpc::tracing::error!("failed to reply {:?}", e))
                             .map(|_| ());
                         ctx.spawn(f)
                    }
                }
            }
        })
        .collect();

    let original_items = std::mem::replace(&mut item.items, vec![]);

    let impl_ident = item.self_ty.clone();
    let original_impl = quote::quote! {
        impl #impl_ident {
            #( #original_items )*
        }
    };

    for method in new_methods {
        let method: syn::ImplItemMethod =
            syn::parse(method.into_token_stream().into()).expect("cannot parse method");
        item.items.push(syn::ImplItem::Method(method));
    }

    let new_item = item.into_token_stream();

    (quote::quote! {
        #original_impl
        #new_item
    }).into()
}