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
//! Macros to reduce boilerplate for RPC implementations.

/// Derive a set of RPC types and message implementation from a declaration.
///
/// The macros are completely optional. They generate the request and response
/// message enums and the service zerosized struct.
/// Optionally, a function can be created to dispatch RPC calls to methods
/// on a struct of your choice.
/// It can also create a type-safe RPC client for the service.
///
/// Usage is as follows:
///
/// ```no_run
/// # use serde::{Serialize,Deserialize};
/// # use quic_rpc::*;
///
/// // Define your message types
///
/// #[derive(Debug, Serialize, Deserialize)]
/// struct Add(pub i32, pub i32);
/// #[derive(Debug, Serialize, Deserialize)]
/// pub struct Sum(pub i32);
/// #[derive(Debug, Serialize, Deserialize)]
/// pub struct Multiply(pub i32);
/// #[derive(Debug, Serialize, Deserialize)]
/// pub struct MultiplyUpdate(pub i32);
/// #[derive(Debug, Serialize, Deserialize)]
/// pub struct MultiplyOutput(pub i32);
///
/// // Derive the RPC types.
///
/// rpc_service! {
///     // Name of the created request enum.
///     Request = MyRequest;
///     // Name of the created response enum.
///     Response = MyResponse;
///     // Name of the created service struct enum.
///     Service = MyService;
///     // Name of the macro to create a dispatch function.
///     // Optional, if not needed pass _ (underscore) as name.
///     CreateDispatch = create_my_dispatch;
///     // Name of the macro to create an RPC client.
///     // Optional, if not needed pass _ (underscore) as name.
///     CreateClient = create_my_client;
///
///     Rpc add = Add, _ -> Sum;
///     BidiStreaming multiply = Multiply, MultiplyUpdate -> MultiplyOutput
/// }
/// ```
///
/// This will generate a request enum `MyRequest`, a response enum `MyRespone`
/// and a service declaration `MyService`.
///
/// It will also generate two macros to create an RPC client and a dispatch function.
///
/// To use the client, invoke the macro with a name. The macro will generate a struct that
/// takes a client channel and exposes typesafe methods for each RPC method.
///
/// ```ignore
/// create_store_client!(MyClient);
/// let client = quic_rpc::quinn::Channel::new(client);
/// let client = quic_rpc::client::RpcClient::<MyService, QuinnChannelTypes>::new(client);
/// let mut client = MyClient(client);
/// let sum = client.add(Add(3, 4)).await?;
/// // Sum(7)
/// let (send, mut recv) = client.multiply(Multiply(2));
/// send(Update(3));
/// let res = recv.next().await?;
/// // Some(MultiplyOutput(6))
/// ```
///
/// To use the dispatch function, invoke the macro with a struct that implements your RPC
/// methods and the name of the generated function. You can then use this dispatch function
/// to dispatch the RPC calls to the methods on your target struct.
///
/// ```ignore
/// #[derive(Clone)]
/// pub struct Calculator;
/// impl Calculator {
///     async fn add(self, req: Add) -> Sum {
///         Sum(req.0 + req.1)
///     }
///     async fn multiply(
///         self,
///         req: Multiply,
///         updates: impl Stream<Item = MultiplyUpdate>
///     ) -> impl Stream<Item = MultiplyOutput> {
///        stream! {
///            tokio::pin!(updates);
///            while let Some(MultiplyUpdate(n)) = updates.next().await {
///                yield MultiplyResponse(req.0 * n);
///            }
///        }
///     }
/// }
///
/// create_my_dispatch!(Calculator, dispatch_calculator_request);
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
///    let server_addr: std::net::SocketAddr = "127.0.0.1:12345".parse()?;
///    let (server, _server_certs) = make_server_endpoint(server_addr)?;
///    let accept = server.accept().await.context("accept failed")?.await?;
///    let connection = quic_rpc::quinn::Channel::new(accept);
///    let calculator = Calculator;
///    let server_handle = spawn_server(
///        StoreService,
///        quic_rpc::quinn::QuinnChannelTypes,
///        connection,
///        calculator,
///        dispatch_calculator_request,
///    );
///    server_handle.await??;
///    Ok(())
/// }
///
/// ```
///
/// The generation of the macros in `CreateDispatch` and `CreateClient`
/// is optional. If you don't need them, pass `_` instead:
///
/// ```ignore
/// # use quic_rpc::*;
/// rpc_service! {
///     Request = MyRequest;
///     Response = MyResponse;
///     Service = MyService;
///     CreateDispatch = _;
///     CreateClient = _;
///
///     Rpc add = Add, _ -> Sum;
///     ClientStreaming stream = Input, Update -> Output;
/// }
/// ```
/// `
#[macro_export]
macro_rules! rpc_service {
    (
        Request = $request:ident;
        Response = $response:ident;
        Service = $service:ident;
        CreateDispatch = $create_dispatch:tt;
        CreateClient = $create_client:tt;

        $($m_pattern:ident $m_name:ident = $m_input:ident, $m_update:tt -> $m_output:ident);+$(;)?
    ) => {

        $crate::__request_enum! {
            $service,
            $request {
                $($m_input,)*
                $($m_update,)*
            }
        }

        #[doc=concat!("Response messages for ", stringify!($service))]
        #[allow(clippy::enum_variant_names)]
        #[derive(::std::fmt::Debug, ::derive_more::From, ::derive_more::TryInto, ::serde::Serialize, ::serde::Deserialize)]
        pub enum $response {
            $($m_output($m_output),)*
        }

        $(
            $crate::__rpc_message!($service, $m_pattern, $m_input, $m_update, $m_output);
        )*

        #[doc=concat!("RPC service ", stringify!($service))]
        #[derive(::std::clone::Clone, ::std::fmt::Debug)]
        pub struct $service;

        impl $crate::Service for $service {
            type Req = $request;
            type Res = $response;
        }

        $crate::__derive_create_dispatch!(
            $service,
            $request,
            $create_dispatch,
            [ $($m_pattern $m_name = $m_input, $m_update -> $m_output);+ ]
        );

        $crate::__derive_create_client!(
            $service,
            $create_client,
            [ $($m_pattern $m_name = $m_input, $m_update -> $m_output);+ ]
        );
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __derive_create_dispatch {
    (
        $service:ident,
        $request:ident,
        _,
        [ $($tt:tt)* ]
    ) => {};
    (
        $service:ident,
        $request:ident,
        $create_dispatch:ident,
        [ $($m_pattern:ident $m_name:ident = $m_input:ident, $m_update:tt -> $m_output:ident);+ ]
    ) => {
        #[doc = concat!("Create an RPC request dispatch function for ", stringify!($service), "\n\nSee the docs for [quic_rpc::rpc_service] for usage docs.")]
        #[macro_export]
        macro_rules! $create_dispatch {
            ($target:ident, $handler:ident) => {
                pub async fn $handler<C: $crate::ChannelTypes>(
                    mut server: $crate::server::RpcServer<$service, C>,
                    msg: <$service as $crate::Service>::Req,
                    chan: (C::SendSink<<$service as $crate::Service>::Res>, C::RecvStream<<$service as $crate::Service>::Req>),
                    target: $target,
                ) -> Result<$crate::server::RpcServer<$service, C>, $crate::server::RpcServerError<C>> {
                    let res = match msg {
                        $(
                            $request::$m_input(msg) => { $crate::__rpc_invoke!($m_pattern, $m_name, $target, server, msg, chan, target) },
                        )*
                        _ => Err($crate::server::RpcServerError::<C>::UnexpectedStartMessage),
                    };
                    res?;
                    Ok(server)
                }
            }
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __request_enum {
    // User entry points.
    ($service:ident, $enum_name:ident { $variant_name:ident $($tt:tt)* }) => {
        $crate::__request_enum!(@ {[$service $enum_name] [$variant_name]} $($tt)*);
    };

    // Internal rules to categorize each value
    // This also filters out _ placeholders from non-streaming methods.
    (@ {[$service:ident $enum_name:ident] [$($agg:ident)*]} $(,)? $(_$(,)?)* $variant_name:ident $($tt:tt)*) => {
        $crate::__request_enum!(@ {[$service $enum_name] [$($agg)* $variant_name]} $($tt)*);
    };

    // Internal rules to categorize each value
    (@ {[$service:ident $enum_name:ident] [$($agg:ident)*]} $(,)? $variant_name:ident $($tt:tt)*) => {
        $crate::__request_enum!(@ {[$service $enum_name] [$($agg)* $variant_name]} $($tt)*);
    };

    // Final internal rule that generates the enum from the categorized input
    (@ {[$service:ident $enum_name:ident] [$($n:ident)*]} $(,)? $(_$(,)?)*) => {
        #[doc=concat!("Request messages for ", stringify!($service))]
        #[derive(::std::fmt::Debug, ::derive_more::From, ::derive_more::TryInto, ::serde::Serialize, ::serde::Deserialize)]
        pub enum $enum_name {
            $($n($n),)*
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __rpc_message {
    ($service:ident, Rpc, $m_input:ident, _, $m_output:ident) => {
        impl $crate::message::RpcMsg<$service> for $m_input {
            type Response = $m_output;
        }
    };
    ($service:ident, ServerStreaming, $m_input:ident, _, $m_output:ident) => {
        impl $crate::message::Msg<$service> for $m_input {
            type Pattern = $crate::message::ServerStreaming;
            type Response = $m_output;
            type Update = $m_input;
        }
    };
    ($service:ident, $m_pattern:ident, $m_input:ident, $m_update:ident, $m_output:ident) => {
        impl $crate::message::Msg<$service> for $m_input {
            type Pattern = $crate::message::$m_pattern;
            type Response = $m_output;
            type Update = $m_update;
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __rpc_invoke {
    (Rpc, $m_name:ident, $target_ty:ident, $server:ident, $msg:ident, $chan:ident, $target:ident) => {
        $server.rpc($msg, $chan, $target, $target_ty::$m_name).await
    };
    (ClientStreaming, $m_name:ident, $target_ty:ident, $server:ident, $msg:ident, $chan:ident, $target:ident) => {
        $server
            .client_streaming($msg, $chan, $target, $target_ty::$m_name)
            .await
    };
    (ServerStreaming, $m_name:ident, $target_ty:ident, $server:ident, $msg:ident, $chan:ident, $target:ident) => {
        $server
            .server_streaming($msg, $chan, $target, $target_ty::$m_name)
            .await
    };
    (BidiStreaming, $m_name:ident, $target_ty:ident, $server:ident, $msg:ident, $chan:ident, $target:ident) => {
        $server
            .bidi_streaming($msg, $chan, $target, $target_ty::$m_name)
            .await
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __derive_create_client{
    (
        $service:ident,
        _,
        [ $($tt:tt)* ]
    ) => {};
    (
        $service:ident,
        $create_client:tt,
        [ $($m_pattern:ident $m_name:ident = $m_input:ident, $m_update:tt -> $m_output:ident);+ ]
    ) => {
        #[doc = concat!("Create an RPC client for ", stringify!($service), "\n\nSee the docs for [quic_rpc::rpc_service] for usage docs.")]
        #[macro_export]
        macro_rules! $create_client {
            ($struct:ident) => {
                #[derive(::std::clone::Clone, ::std::fmt::Debug)]
                pub struct $struct<C: $crate::ChannelTypes>(pub $crate::client::RpcClient<$service, C>);

                impl<C: $crate::ChannelTypes> $struct<C> {
                    $(
                        $crate::__rpc_method!($m_pattern, $service, $m_name, $m_input, $m_output, $m_update);
                    )*
                }
            };
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __rpc_method {
    (Rpc, $service:ident, $m_name:ident, $m_input:ident, $m_output:ident, _) => {
        pub async fn $m_name(
            &mut self,
            input: $m_input,
        ) -> ::std::result::Result<$m_output, $crate::client::RpcClientError<C>> {
            self.0.rpc(input).await
        }
    };
    (ClientStreaming, $service:ident, $m_name:ident, $m_input:ident, $m_output:ident, $m_update:ident) => {
        pub async fn $m_name(
            &mut self,
            input: $m_input,
        ) -> ::std::result::Result<
            (
                $crate::client::UpdateSink<$service, C, $m_input>,
                ::futures::future::BoxFuture<
                    'static,
                    ::std::result::Result<$m_output, $crate::client::ClientStreamingItemError<C>>,
                >,
            ),
            $crate::client::ClientStreamingError<C>,
        > {
            self.0.client_streaming(input).await
        }
    };
    (ServerStreaming, $service:ident, $m_name:ident, $m_input:ident, $m_output:ident, _) => {
        pub async fn $m_name(
            &mut self,
            input: $m_input,
        ) -> ::std::result::Result<
            ::futures::stream::BoxStream<
                'static,
                ::std::result::Result<$m_output, $crate::client::StreamingResponseItemError<C>>,
            >,
            $crate::client::StreamingResponseError<C>,
        > {
            self.0.server_streaming(input).await
        }
    };
    (BidiStreaming, $service:ident, $m_name:ident, $m_input:ident, $m_output:ident, $m_update:ident) => {
        pub async fn $m_name(
            &mut self,
            input: $m_input,
        ) -> ::std::result::Result<
            (
                $crate::client::UpdateSink<$service, C, $m_input>,
                ::futures::stream::BoxStream<
                    'static,
                    ::std::result::Result<$m_output, $crate::client::BidiItemError<C>>,
                >,
            ),
            $crate::client::BidiError<C>,
        > {
            self.0.bidi(input).await
        }
    };
}