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
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_macro_input, spanned::Spanned, ItemTrait, ReturnType, TraitItem, Type};

#[proc_macro_attribute]
/// This procedural macro should be put on top of a `async_trait` trait with name ending in `...Protocol`, defining all the function signatures in the RPC protocol. Given a trait of name `FooProtocol`, the macro
/// - automatically derives an `nanorpc::RpcService` implementation for `FooService`, a generated type that wraps around anything that implements `FooProtocol` --- these would be types that are server implementations of the protocol.
/// - automatically generates `FooClient`, a client-side struct that wraps a `nanorpc::RpcTransport` and has methods mirroring `FooProtocol`.
pub fn nanorpc_derive(_: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemTrait);
    let input_again = input.clone();
    let protocol_name = input.ident;
    if !protocol_name.to_string().ends_with("Protocol") {
        panic!("trait must end with the word \"Protocol\"")
    }
    let server_struct_name = syn::Ident::new(
        &format!(
            "{}Service",
            protocol_name.to_string().trim_end_matches("Protocol")
        ),
        protocol_name.span(),
    );
    let client_struct_name = syn::Ident::new(
        &format!(
            "{}Client",
            protocol_name.to_string().trim_end_matches("Protocol")
        ),
        protocol_name.span(),
    );
    let error_struct_name = syn::Ident::new(
        &format!(
            "{}Error",
            protocol_name.to_string().trim_end_matches("Protocol")
        ),
        protocol_name.span(),
    );

    // Generate the server implementation.
    let mut server_match = quote! {};
    let mut client_body = quote! {};
    for item in input.items {
        match item {
            TraitItem::Method(inner) => {
                let method_name = inner.sig.ident.clone();
                // create the block of code needed for calling the function
                // TODO check that it does in fact take "self"
                let mut offset = 0;
                let method_call = inner
                    .sig
                    .inputs
                    .iter()
                    .enumerate()
                    .map(|(idx, arg)| match arg {
                        syn::FnArg::Receiver(_) => {
                            offset += 1;
                            quote! {&self.0}
                        }
                        syn::FnArg::Typed(_) => {
                            let index = idx - offset;
                            quote! {::serde_json::from_value(__nrpc_args[#index].clone()).unwrap()}
                            // TODO handle this properly without a stupid clone
                        }
                    })
                    .reduce(|a, b| quote! {#a,#b})
                    .unwrap();
                // let method_call = method_call.to_string();
                let method_name_str = method_name.to_string();

                // TODO a better heuristic here
                let is_fallible = inner
                    .sig
                    .output
                    .to_token_stream()
                    .to_string()
                    .contains("Result");
                if is_fallible {
                    server_match = quote! {
                        #server_match
                        #method_name_str => {
                            let raw = #protocol_name::#method_name(#method_call).await;
                            let ok_mapped = raw.map(|o| ::serde_json::to_value(o).expect("serialization failed"));
                            let err_mapped = ok_mapped.map_err(|e| nanorpc::ServerError{
                                code: 1,
                                message: e.to_string(),
                                details: ::serde_json::to_value(e).expect("serialization failed")
                            });
                            ::std::option::Option::Some(err_mapped)
                        }
                    };
                } else {
                    server_match = quote! {
                        #server_match
                        #method_name_str => {
                            ::std::option::Option::Some(::std::result::Result::Ok(::serde_json::to_value(#protocol_name::#method_name(#method_call).await).expect("serialization failed")))
                        }
                    };
                }

                // Do the client
                let mut client_signature = inner.sig.clone();
                let original_output = match &client_signature.output {
                    ReturnType::Default => quote! {()},
                    ReturnType::Type(_, t) => t.to_token_stream(),
                };
                client_signature.output = ReturnType::Type(
                    syn::Token! [->](client_signature.span()),
                    Box::new(Type::Verbatim(
                        quote! {::std::result::Result<#original_output, #error_struct_name<__nrpc_T::Error>>},
                    )),
                );
                let vec_build = client_signature
                    .inputs
                    .iter()
                    .filter_map(|arg| match arg {
                        syn::FnArg::Receiver(_) => None,
                        syn::FnArg::Typed(t) => match t.pat.as_ref() {
                            syn::Pat::Ident(varname) => {
                                Some(quote! {__vb.push(::serde_json::to_value(&#varname).unwrap())})
                            }
                            v => panic!("wild {:?}", v.to_token_stream()),
                        },
                    })
                    .fold(
                        quote! {
                            let mut __vb: ::std::vec::Vec<::serde_json::Value> = ::std::vec::Vec::with_capacity(8);
                        },
                        |a, b| quote! {#a; #b},
                    );
                let method_name = client_signature.ident.to_string();
                let return_handler = if is_fallible {
                    quote! {
                        match jsval  {
                            Ok(jsval) => {
                                let retval = ::serde_json::from_value(jsval).map_err(#error_struct_name::FailedDecode)?;
                                Ok(Ok(retval))
                            }
                            Err(serverr) => {
                                Ok(Err(::serde_json::from_value(serverr.details).map_err(#error_struct_name::FailedDecode)?))
                            }
                        }
                    }
                } else {
                    quote! {
                        match jsval  {
                            Ok(jsval) => {
                                let retval: #original_output = ::serde_json::from_value(jsval).map_err(#error_struct_name::FailedDecode)?;
                                Ok(retval)
                            }
                            Err(serverr) => {
                                Err(#error_struct_name::ServerFail)
                            }
                        }
                    }
                };
                client_body = quote! {
                    #client_body

                    pub #client_signature {
                        #vec_build;
                        let result = nanorpc::RpcTransport::call(&self.0, #method_name, &__vb).await?;
                        match result {
                            None => Err(#error_struct_name::NotFound),
                            Some(jsval) => {
                                #return_handler
                            }
                        }
                    }
                }
            }
            _ => {
                panic!("does not support things other than methods in the trait definition")
            }
        }
    }

    // Generate the client implementation
    let client_impl = quote! {
        pub struct #client_struct_name<T: nanorpc::RpcTransport>(pub T);

        impl <__nrpc_T: nanorpc::RpcTransport + Send + Sync + 'static> #client_struct_name<__nrpc_T> {
            #client_body
        }
    };

    let assembled = quote! {
        #input_again

        pub struct #server_struct_name<T: #protocol_name>(pub T);

        #[::async_trait::async_trait]
        impl <__nrpc_T: #protocol_name + ::std::marker::Sync + ::std::marker::Send + 'static> nanorpc::RpcService for #server_struct_name<__nrpc_T> {
            async fn respond(&self, __nrpc_method: &str, __nrpc_args: Vec<::serde_json::Value>) -> Option<Result<::serde_json::Value, nanorpc::ServerError>> {
                match __nrpc_method {
                #server_match
                _ => {None}
                }
            }
        }

        #[derive(::thiserror::Error, Debug)]
        pub enum #error_struct_name<T> {
            #[error("verb not found")]
            NotFound,
            #[error("unexpected server error on an infallible verb")]
            ServerFail,
            #[error("failed to decode JSON response: {0}")]
            FailedDecode(::serde_json::Error),
            #[error("transport-level error: {0}")]
            Transport(#[from] T)
        }

        #client_impl
    };
    assembled.into()
}