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
|
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{
parse_macro_input, spanned::Spanned, FnArg, ImplItem, ItemImpl, ReturnType, Signature, Type,
TypePath,
};
macro_rules! err {
($($tt:tt)*) => {
return syn::Error::new($($tt)*).to_compile_error().into()
};
}
#[proc_macro_attribute]
pub fn rpc_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item2 = item.clone();
let parsed_input = parse_macro_input!(item2 as ItemImpl);
let self_ty = match *parsed_input.self_ty {
Type::Path(p) => p,
_ => err!(
parsed_input.span(),
"Implementing the trait `RPCService` on this type is unsupported"
),
};
let methods = match parse_struct_methods(&self_ty, parsed_input.items) {
Ok(res) => res,
Err(err) => return err.to_compile_error().into(),
};
let mut method_idents = vec![];
for method in methods.iter() {
method_idents.push(method.ident.clone());
if method.inputs.len() != 2 {
err!(
method.span(),
"requires `&self` and a parameter of type `serde_json::Value`"
);
}
if let Err(err) = validate_method(method) {
return err.to_compile_error().into();
}
}
let impl_methods: Vec<TokenStream2> = method_idents.iter().map(
|m| quote! {
stringify!(#m) => Some(Box::new(move |params: serde_json::Value| Box::pin(self.#m(params)))),
},
).collect();
let item: TokenStream2 = item.into();
quote! {
impl karyon_jsonrpc::RPCService for #self_ty {
fn get_method<'a>(
&'a self,
name: &'a str
) -> Option<karyon_jsonrpc::RPCMethod> {
match name {
#(#impl_methods)*
_ => None,
}
}
fn name(&self) -> String{
stringify!(#self_ty).to_string()
}
}
#item
}
.into()
}
#[proc_macro_attribute]
pub fn rpc_pubsub_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item2 = item.clone();
let parsed_input = parse_macro_input!(item2 as ItemImpl);
let self_ty = match *parsed_input.self_ty {
Type::Path(p) => p,
_ => err!(
parsed_input.span(),
"Implementing the trait `PubSubRPCService` on this type is unsupported"
),
};
let methods = match parse_struct_methods(&self_ty, parsed_input.items) {
Ok(res) => res,
Err(err) => return err.to_compile_error().into(),
};
let mut method_idents = vec![];
for method in methods.iter() {
method_idents.push(method.ident.clone());
if method.inputs.len() != 4 {
err!(method.span(), "requires `&self` and three parameters: `Arc<Channel>`, method: `String`, and `serde_json::Value`");
}
if let Err(err) = validate_method(method) {
return err.to_compile_error().into();
}
}
let impl_methods: Vec<TokenStream2> = method_idents.iter().map(
|m| quote! {
stringify!(#m) => {
Some(Box::new(
move |chan: std::sync::Arc<karyon_jsonrpc::Channel>, method: String, params: serde_json::Value| {
Box::pin(self.#m(chan, method, params))
}))
},
},
).collect();
let item: TokenStream2 = item.into();
quote! {
impl karyon_jsonrpc::PubSubRPCService for #self_ty {
fn get_pubsub_method<'a>(
&'a self,
name: &'a str
) -> Option<karyon_jsonrpc::PubSubRPCMethod> {
match name {
#(#impl_methods)*
_ => None,
}
}
fn name(&self) -> String{
stringify!(#self_ty).to_string()
}
}
#item
}
.into()
}
fn parse_struct_methods(
self_ty: &TypePath,
items: Vec<ImplItem>,
) -> Result<Vec<Signature>, syn::Error> {
let mut methods: Vec<Signature> = vec![];
if items.is_empty() {
return Err(syn::Error::new(
self_ty.span(),
"At least one method should be implemented",
));
}
for item in items {
match item {
ImplItem::Fn(method) => {
methods.push(method.sig);
}
_ => return Err(syn::Error::new(item.span(), "Unexpected item!")),
}
}
Ok(methods)
}
fn validate_method(method: &Signature) -> Result<(), syn::Error> {
if let FnArg::Typed(_) = method.inputs[0] {
return Err(syn::Error::new(method.span(), "requires `&self` parameter"));
}
if let ReturnType::Default = method.output {
return Err(syn::Error::new(
method.span(),
"requires `Result<serde_json::Value, RPCError>` as return type",
));
}
Ok(())
}
|