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
562
563
564
565
566
567
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{format_ident, quote, ToTokens};
use syn::{
    braced, meta, parenthesized,
    parse::{Parse, ParseStream, Parser},
    parse2, parse_macro_input, parse_quote, Attribute, FnArg, GenericArgument, Ident, ImplItemFn,
    ItemTrait, LitStr, Pat, PathArguments, Result, ReturnType, Token, TraitItem, TraitItemFn, Type,
    Visibility,
};

#[proc_macro_attribute]
pub fn rpc(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as RpcArgs);

    let mut item_trait = parse_macro_input!(input as ItemTrait);
    let vis = &item_trait.vis;
    let trait_name = &item_trait.ident;
    let trait_name_snake = to_snake_case(trait_name.to_string());
    let add_method_name = format_ident!("add_{}_methods", trait_name_snake);

    let doc_func = if args.openrpc {
        let doc_func_name = format_ident!("{}_doc", trait_name_snake);
        let method_defs = item_trait
            .items
            .iter_mut()
            .filter_map(|m| match m {
                TraitItem::Fn(m) => Some(m),
                _ => None,
            })
            .map(|m| method_def(m))
            .collect::<Result<Vec<_>>>();
        let method_defs = match method_defs {
            Ok(x) => x,
            Err(e) => return e.to_compile_error().into(),
        };
        let title = &*trait_name_snake;
        quote!(
            #vis fn #doc_func_name() -> jsonrpc_utils::serde_json::Value {
                #[allow(unused)]
                use schemars::JsonSchema;

                let mut gen = schemars::gen::SchemaSettings::draft07().with(|s|
                    s.definitions_path = "#/components/schemas/".into()
                ).into_generator();

                let methods = jsonrpc_utils::serde_json::json!([#(#method_defs)*]);

                jsonrpc_utils::serde_json::json!({
                    "openrpc": "1.2.6",
                    "info": {
                        "title": #title,
                        "version": "1.0.0",
                    },
                    "methods": methods,
                    "components": {
                        "schemas": gen.take_definitions(),
                    }
                })
            }
        )
    } else {
        quote!()
    };

    let add_methods = item_trait
        .items
        .iter_mut()
        .filter_map(|m| match m {
            TraitItem::Fn(m) => Some(m),
            _ => None,
        })
        .map(add_method)
        .collect::<Result<Vec<_>>>();
    let add_methods = match add_methods {
        Ok(x) => x,
        Err(e) => return e.to_compile_error().into(),
    };

    let result = quote! {
        #item_trait

        #vis fn #add_method_name(rpc: &mut jsonrpc_utils::jsonrpc_core::MetaIoHandler<Option<jsonrpc_utils::pub_sub::Session>>, rpc_impl: impl #trait_name + Clone + Send + Sync + 'static) {
            #(#add_methods)*
        }

        #doc_func
    };

    result.into()
}

#[proc_macro_attribute]
pub fn rpc_client(_attr: TokenStream, input: TokenStream) -> TokenStream {
    rpc_client_impl(input.into())
        .unwrap_or_else(|e| e.into_compile_error())
        .into()
}

struct ImplMethods {
    attributes: Vec<Attribute>,
    ident: Ident,
    items: Vec<ImplItemFn>,
}

impl Parse for ImplMethods {
    fn parse(input: ParseStream) -> Result<Self> {
        let attributes = input.call(Attribute::parse_outer)?;
        input.parse::<Token![impl]>()?;
        let ident: Ident = input.parse()?;
        let mut items: Vec<ImplItemFn> = Vec::new();
        let content;
        braced!(content in input);
        while !content.is_empty() {
            let vis: Visibility = content.parse()?;
            items.push({
                let m: TraitItemFn = content.parse()?;
                ImplItemFn {
                    attrs: m.attrs,
                    vis,
                    defaultness: None,
                    sig: m.sig,
                    block: parse_quote!({}),
                }
            });
        }
        Ok(Self {
            attributes,
            ident,
            items,
        })
    }
}

fn rpc_client_impl(input: proc_macro2::TokenStream) -> Result<proc_macro2::TokenStream> {
    let mut impl_block: ImplMethods = parse2(input)?;

    for item in &mut impl_block.items {
        rewrite_method(item)?;
    }

    let attributes = impl_block.attributes;
    let ident = impl_block.ident;
    let items = impl_block.items;

    Ok(quote! {
        #(#attributes)*
        impl #ident {
            #(#items)*
        }
    })
}

fn rewrite_method(m: &mut ImplItemFn) -> Result<()> {
    let args = ClientMethodArgs::parse_attrs(&m.attrs)?;

    m.attrs.retain(|a| !a.path().is_ident("rpc"));

    let method_name = args.name.unwrap_or_else(|| m.sig.ident.to_string());

    let mut ident_counter = 0u32;
    let mut next_ident_counter = || {
        ident_counter += 1;
        ident_counter
    };

    let mut params_names = Vec::new();
    for arg in &mut m.sig.inputs {
        match arg {
            FnArg::Receiver(_) => {}
            FnArg::Typed(pat_type) => match &*pat_type.pat {
                Pat::Ident(ident) => {
                    params_names.push(ident.ident.clone());
                }
                _ => {
                    let ident = format_ident!("param_{}", next_ident_counter());
                    params_names.push(ident.clone());
                    pat_type.pat = parse_quote!(#ident);
                }
            },
        }
    }
    let params_names = quote!(#(#params_names ,)*);

    if m.sig.asyncness.is_some() {
        m.block = parse_quote!({
            let result = self
                .inner
                .rpc(#method_name, &jsonrpc_utils::serde_json::value::to_raw_value(&(#params_names))?)
                .await?;
            Ok(jsonrpc_utils::serde_json::from_value(result)?)
        });
    } else {
        m.block = parse_quote!({
            let result = self
                .inner
                .rpc(#method_name, &jsonrpc_utils::serde_json::value::to_raw_value(&(#params_names))?)?;
            Ok(jsonrpc_utils::serde_json::from_value(result)?)
        });
    }
    Ok(())
}

fn method_def(m: &TraitItemFn) -> Result<proc_macro2::TokenStream> {
    let doc_attr = m.attrs.iter().rev().find(|a| a.path().is_ident("doc"));
    let doc = if let Some(doc_attr) = doc_attr {
        let v = &doc_attr.meta.require_name_value()?.value;
        Some(parse2::<LitStr>(v.to_token_stream())?)
    } else {
        None
    };

    let description = if let Some(doc) = doc {
        let doc = LitStr::new(doc.value().trim_start(), doc.span());
        quote!( "description": #doc, )
    } else {
        quote!()
    };

    let attrs = MethodArgs::parse_attrs(&m.attrs)?;
    if attrs.pub_sub.is_some() {
        return Ok(quote!());
    }
    let name = attrs.name.unwrap_or_else(|| m.sig.ident.to_string());
    let params: Vec<_> = m
        .sig
        .inputs
        .iter()
        .filter_map(|input| match input {
            FnArg::Receiver(_) => None,
            FnArg::Typed(pat_type) => {
                let name = match &*pat_type.pat {
                    Pat::Ident(i) => LitStr::new(&i.ident.to_string(), i.ident.span()),
                    _ => LitStr::new("parameter", Span::call_site()),
                };
                let ty = &pat_type.ty;
                Some(quote! {
                    {
                        "name": #name,
                        "schema": gen.subschema_for::<#ty>(),
                    }
                })
            }
        })
        .collect();
    let result_type = match &m.sig.output {
        ReturnType::Default => todo!(),
        ReturnType::Type(_, t) => match &**t {
            Type::Path(tp) => {
                if tp.qself.is_none() && tp.path.segments.len() == 1 {
                    let seg = tp.path.segments.first().unwrap();
                    if seg.ident == "Result" {
                        match &seg.arguments {
                            PathArguments::AngleBracketed(ang) => match ang.args.first() {
                                Some(GenericArgument::Type(t)) => t,
                                _ => t,
                            },
                            _ => t,
                        }
                    } else {
                        t
                    }
                } else {
                    t
                }
            }
            _ => t,
        },
    };
    // TODO: more meaningful result name.
    let result = quote! {
        {
            "name": #name,
            "schema": gen.subschema_for::<#result_type>(),
        }
    };
    Ok(quote! {
        jsonrpc_utils::serde_json::json!({
            "name": #name,
            #description
            "params": [#(#params),*],
            "result": #result,
        }),
    })
}

fn add_method(m: &mut TraitItemFn) -> Result<proc_macro2::TokenStream> {
    let method_name = &m.sig.ident;

    let (rpc_attributes, other_attributes) = m
        .attrs
        .drain(..)
        .partition::<Vec<_>, _>(|a| a.path().is_ident("rpc"));
    let args = MethodArgs::parse_attrs(&rpc_attributes)?;
    m.attrs = other_attributes;

    let rpc_method_name = args.name.unwrap_or_else(|| method_name.to_string());

    let mut params_names = Vec::new();
    let mut params_tys = Vec::new();
    let mut ident_counter = 0u32;
    let mut next_ident_counter = || {
        ident_counter += 1;
        ident_counter
    };
    for arg in &m.sig.inputs {
        match arg {
            FnArg::Receiver(_) => {}
            FnArg::Typed(pat_type) => match &*pat_type.pat {
                Pat::Ident(ident) => {
                    params_names.push(ident.ident.clone());
                    params_tys.push(&*pat_type.ty);
                }
                _ => {
                    params_names.push(format_ident!("param_{}", next_ident_counter()));
                    params_tys.push(&*pat_type.ty);
                }
            },
        }
    }
    let no_params = params_names.is_empty();
    // Number of tailing optional parameters.
    let optional_params = params_tys
        .iter()
        .rev()
        .take_while(|t| match t {
            Type::Path(t) => t
                .path
                .segments
                .first()
                .map_or(false, |s| s.ident == "Option"),
            _ => false,
        })
        .count();
    let params_names1 = quote!(#(#params_names ,)*);
    let params_tys1 = quote!(#(#params_tys ,)*);
    let parse_params = if optional_params > 0 {
        let required_params = params_names.len() - optional_params;
        let mut parse_params = quote! {
            let mut arr = match params {
                jsonrpc_utils::jsonrpc_core::Params::Array(arr) => arr.into_iter(),
                jsonrpc_utils::jsonrpc_core::Params::None => Vec::new().into_iter(),
                _ => return Err(jsonrpc_utils::jsonrpc_core::Error::invalid_params("")),
            };
        };
        for i in 0..required_params {
            let p = &params_names[i];
            let ty = params_tys[i];
            parse_params.extend(quote! {
                let #p: #ty = jsonrpc_utils::serde_json::from_value(arr.next().ok_or_else(|| jsonrpc_utils::jsonrpc_core::Error::invalid_params(""))?).map_err(|_|
                    jsonrpc_utils::jsonrpc_core::Error::invalid_params("")
                )?;
            });
        }
        for i in required_params..params_names.len() {
            let p = &params_names[i];
            let ty = params_tys[i];
            parse_params.extend(quote! {
                let #p: #ty = match arr.next() {
                    Some(v) => jsonrpc_utils::serde_json::from_value(v).map_err(|_| jsonrpc_utils::jsonrpc_core::Error::invalid_params(""))?,
                    None => None,
                };
            });
        }
        parse_params
    } else if no_params {
        quote!(params.expect_no_params()?;)
    } else {
        quote!(let (#params_names1): (#params_tys1) = params.parse()?;)
    };
    let result = if m.sig.asyncness.is_some() {
        quote!(rpc_impl.#method_name(#params_names1).await)
    } else {
        quote!(rpc_impl.#method_name(#params_names1))
    };

    Ok(if let Some(pub_sub) = args.pub_sub {
        let notify_method_lit = &*pub_sub.notify;
        let unsubscribe_method_lit = &*pub_sub.unsubscribe;
        quote! {
            jsonrpc_utils::pub_sub::add_pub_sub(rpc, #rpc_method_name, #notify_method_lit.into(), #unsubscribe_method_lit, {
                let rpc_impl = rpc_impl.clone();
                move |params: jsonrpc_utils::jsonrpc_core::Params| {
                    #parse_params
                    rpc_impl.#method_name(#params_names1).map_err(Into::into)
                }
            });
        }
    } else {
        quote! {
            rpc.add_method(#rpc_method_name, {
                let rpc_impl = rpc_impl.clone();
                move |params: jsonrpc_utils::jsonrpc_core::Params| {
                    let rpc_impl = rpc_impl.clone();
                    async move {
                        #parse_params
                        jsonrpc_utils::serde_json::to_value(#result?).map_err(|_| jsonrpc_utils::jsonrpc_core::Error::internal_error())
                    }
                }
            });
        }
    })
}

struct RpcArgs {
    openrpc: bool,
}

impl Parse for RpcArgs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut openrpc = false;
        if !input.is_empty() {
            let parser = meta::parser(|m| {
                if m.path.is_ident("openrpc") {
                    openrpc = true;
                    Ok(())
                } else {
                    Err(m.error("unknown arg"))
                }
            });
            parser.parse2(input.parse()?)?;
        }
        Ok(Self { openrpc })
    }
}

struct ClientMethodArgs {
    name: Option<String>,
}

impl ClientMethodArgs {
    fn parse_attrs(attrs: &[Attribute]) -> Result<Self> {
        let mut name: Option<LitStr> = None;

        for a in attrs {
            if a.path().is_ident("rpc") {
                a.parse_nested_meta(|m| {
                    if m.path.is_ident("name") {
                        name = Some(m.value()?.parse()?);
                    } else {
                        return Err(m.error("unknown arg"));
                    }
                    Ok(())
                })?;
            }
        }

        let name = name.map(|n| n.value());
        Ok(Self { name })
    }
}

struct MethodArgs {
    pub_sub: Option<PubSubArgs>,
    name: Option<String>,
}

impl MethodArgs {
    fn parse_attrs(attrs: &[Attribute]) -> Result<Self> {
        let mut pub_sub: Option<PubSubArgs> = None;
        let mut name: Option<LitStr> = None;
        for a in attrs {
            if a.path().is_ident("rpc") {
                a.parse_nested_meta(|m| {
                    if m.path.is_ident("pub_sub") {
                        let content;
                        parenthesized!(content in m.input);
                        pub_sub = Some(content.parse()?);
                        Ok(())
                    } else if m.path.is_ident("name") {
                        name = Some(m.value()?.parse()?);
                        Ok(())
                    } else {
                        Err(m.error("unknown arg"))
                    }
                })?;
            }
        }
        let name = name.map(|n| n.value());
        Ok(Self { name, pub_sub })
    }
}

struct PubSubArgs {
    notify: String,
    unsubscribe: String,
}

impl Parse for PubSubArgs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut notify: Option<LitStr> = None;
        let mut unsubscribe: Option<LitStr> = None;

        let parser = meta::parser(|m| {
            if m.path.is_ident("notify") {
                notify = Some(m.value()?.parse()?);
            } else if m.path.is_ident("unsubscribe") {
                unsubscribe = Some(m.value()?.parse()?);
            } else {
                return Err(m.error("unknown arg"));
            }
            Ok(())
        });
        parser.parse2(input.parse()?)?;

        let notify = notify
            .ok_or_else(|| input.error("missing arg notify"))?
            .value();
        let unsubscribe = unsubscribe
            .ok_or_else(|| input.error("missing arg unsubscribe"))?
            .value();

        Ok(Self {
            notify,
            unsubscribe,
        })
    }
}

fn to_snake_case(ident: String) -> String {
    let mut result = String::with_capacity(ident.len());
    for c in ident.chars() {
        if c.is_ascii_uppercase() {
            if !result.is_empty() {
                result.push('_');
            }
            result.push(c.to_ascii_lowercase());
        } else {
            result.push(c)
        }
    }
    result
}

#[cfg(test)]
mod tests {
    use syn::{parse2, Stmt};

    use super::*;

    fn test_method(m: proc_macro2::TokenStream) -> Stmt {
        let output = add_method(&mut parse2(m).unwrap()).unwrap();
        println!("output: {}", output);
        parse2(output).unwrap()
    }

    #[test]
    fn test_methods() {
        test_method(quote!(
            async fn no_param(&self) -> Result<u64>;
        ));
        test_method(quote!(
            async fn sleep(&self, x: u64) -> Result<u64>;
        ));
        test_method(quote!(
            #[rpc(name = "@sleep")]
            fn sleep(&self, a: i32, b: i32) -> Result<i32>;
        ));
        test_method(quote!(
            fn sleep2(&self, a: Option<i32>, b: Option<i32>) -> Result<i32>;
        ));
        test_method(quote!(
            #[rpc(pub_sub(notify = "subscription", unsubscribe = "unsubscribe"))]
            fn subscribe(&self, a: i32, b: i32) -> Result<S>;
        ));
    }
}