qubit 1.0.0-beta.2

Seamless RPC for Rust & TypeScript
Documentation
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
pub mod ctx;
pub mod marker;
pub mod response;
pub mod ts;

use futures::{Stream, StreamExt};
use jsonrpsee::{
    DisconnectError, RpcModule, SubscriptionCloseResponse, SubscriptionMessage,
    types::{Params, ResponsePayload},
};
use serde::Deserialize;
use serde_json::json;
use ts_rs::TS;

use std::pin::pin;

use self::{ctx::FromRequestExtensions, response::ResponseValue, ts::TsTypeTuple};

/// A handler suitable for use with Qubit.
///
/// The `Marker` generic is a utility in order to provide implementations for `Fn` traits which
/// take generics as parameters.
pub trait QubitHandler<Ctx, MSig>: 'static + Send + Sync + Clone {
    /// Context type this handler expects.
    type Ctx: 'static + Send + Sync + FromRequestExtensions<Ctx>;
    /// Parameters that the handler will accept (excluding [`Ctx`](QubitHandler::Ctx)).
    type Params: TsTypeTuple;
    /// Return type of the handler.
    type Return;

    /// Call the handler with the provided `Ctx` and [`Params`]. The handler implementation
    /// must deserialise the parameters as required.
    fn call(&self, ctx: Self::Ctx, params: Params) -> Self::Return;
}

macro_rules! impl_handlers {
    (impl [$($ctx:ident, $($params:ident,)*)?]) => {
        impl<Ctx, F, R, $($ctx, $($params),*)?> QubitHandler<
            Ctx,
            (
                ($($ctx, $($params,)*)?),
                R
            )
        >
        for F
        where
            F: 'static + Send + Sync + Clone + Fn($($ctx, $($params),*)?) -> R,
            impl_handlers!(ctx_ty [$($ctx)?]): 'static + Send + Sync + FromRequestExtensions<Ctx>,
            $($($params: 'static + TS + Send + for<'a> Deserialize<'a>),*)?
        {
            type Ctx = impl_handlers!(ctx_ty [$($ctx)?]);

            type Params = ($($($params,)*)?);
            type Return = R;

            fn call(
                &self,
                #[allow(unused)] ctx: Self::Ctx,
                #[allow(unused)] params: Params
            ) -> Self::Return {
                #[allow(non_snake_case)]
                let ($($($params,)*)?) = match impl_handlers!(parse_impl params -> [$($($params,)*)?]) {
                    Ok(params) => params,
                    Err(e) => {
                        // TODO: Something
                        dbg!(e);
                        panic!("fukc");
                    }
                };

                // Call the handler, optionally with the context and any parameters.
                self($(ctx, $($params,)*)?)
            }
        }
    };

    (ctx_ty [$ctx:ty]) => {
        $ctx
    };
    (ctx_ty []) => {
        Ctx
    };

    // HACK: This is to work around `serde_json` not allowing parsing `()` from `[]`:
    //
    // ```rs
    // serde_json::from_str::<()>("[]")
    // ```
    //
    // Instead, this swaps between two implementations. If the handler takes no parameters, it will
    // try parse `[(); 0]` (to ensure that no unnecessary parameters were passed to the handler).
    // Otherwise if the parameter does require parameters, they will be parsed as normal.
    //
    // This can be reverted once the following PR lands: https://github.com/serde-rs/json/pull/869
    (parse_impl $params:ident -> []) => {
        $params.parse::<[(); 0]>()
            .map(|_| ())
    };
    (parse_impl $params:ident -> [$($param_tys:ident,)*]) => {
        $params.parse::<Self::Params>()
    };

    (count []) => { 0 };
    (count [$param:ident, $($params:ident,)*]) => {
        1 + impl_handlers!(count [$($params,)*])
    };

    (recurse []) => {};
    (recurse [$param:ident, $($params:ident,)*]) => {
        impl_handlers!($($params),*);
    };

    ($($params:ident),* $(,)?) => {
        impl_handlers!(impl [$($params,)*]);
        impl_handlers!(recurse [$($params,)*]);
    };
}

impl_handlers!(
    P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15
);

/// Registration implementation differs depending on the return type of the handler. This
/// is to account for handlers which may return futures, streams, or values directly.
pub trait RegisterableHandler<
    Ctx,
    MSig,
    MValue: marker::ResponseMarker,
    MReturn: marker::HandlerReturnMarker,
>: QubitHandler<Ctx, MSig>
{
    /// The 'response' of the handler, which might not necessarily be the direct return type of the
    /// handler. It may be the [`Future::Output`], a [`Stream::Item`], or some other value that is
    /// derived from a handler return value.
    type Response: ResponseValue<MValue>;

    /// Register this handler against the provided RPC module.
    fn register(self, module: &mut RpcModule<Ctx>, method_name: String);
}

/// Register any handler that directly returns a [`ResponseValue`]. This will generally be the
/// simplest of handlers, without any streaming or futures.
impl<Ctx, T, MSig, MValue> RegisterableHandler<Ctx, MSig, MValue, marker::MResponse<MValue>> for T
where
    Ctx: 'static + Clone + Send + Sync,
    MValue: marker::ResponseMarker,
    T: QubitHandler<Ctx, MSig>,
    T::Return: ResponseValue<MValue>,
{
    /// The response is whatever is returned from the handler (plus any additional processing from
    /// [`ResponseValue::transform`]).
    type Response = T::Return;

    /// These handlers will be registered using [`RpcModule::register_blocking_method`], so that
    /// the handler can be run on a new thread without blocking the server.
    fn register(self, module: &mut RpcModule<Ctx>, method_name: String) {
        module
            .register_async_method(
                Box::leak(method_name.into_boxed_str()),
                move |params, ctx, extensions| {
                    let handler = self.clone();

                    async move {
                        let ctx =
                            match Self::Ctx::from_request_extensions((*ctx).clone(), extensions)
                                .await
                            {
                                Ok(ctx) => ctx,
                                Err(e) => {
                                    return ResponsePayload::error(e);
                                }
                            };
                        let result = handler.call(ctx, params);
                        ResponsePayload::success(result.transform())
                    }
                },
            )
            .unwrap();
    }
}

/// Register any handler that returns a [`Future`] which outputs a [`ResponseValue`]. This
/// implementation covers `async` handlers.
impl<Ctx, T, MSig, MValue>
    RegisterableHandler<Ctx, MSig, MValue, marker::MFuture<marker::MResponse<MValue>>> for T
where
    Ctx: 'static + Clone + Send + Sync,
    MValue: marker::ResponseMarker,
    T: QubitHandler<Ctx, MSig>,
    T::Return: Future + Send,
    <T::Return as Future>::Output: ResponseValue<MValue>,
{
    /// The response will be the `await`ed value of the returned future.
    type Response = <T::Return as Future>::Output;

    /// These handlers will be registered using [`RpcModule::register_async_method`].
    fn register(self, module: &mut RpcModule<Ctx>, method_name: String) {
        module
            .register_async_method(
                Box::leak(method_name.into_boxed_str()),
                move |params, ctx, extensions| {
                    let f = self.clone();

                    async move {
                        let ctx =
                            match Self::Ctx::from_request_extensions((*ctx).clone(), extensions)
                                .await
                            {
                                Ok(ctx) => ctx,
                                Err(e) => {
                                    return ResponsePayload::error(e);
                                }
                            };
                        let result = f.call(ctx, params).await;
                        ResponsePayload::success(result.transform())
                    }
                },
            )
            .unwrap();
    }
}

/// Register any handler that returns a [`Stream`] containing items implementing [`ResponseValue`].
/// This implementation will only handle [`Stream`]s which are directly returned from a handler
/// (not async handlers).
impl<Ctx, T, MValue, MSig> RegisterableHandler<Ctx, MSig, MValue, marker::MStream<MValue>> for T
where
    Ctx: 'static + Clone + Send + Sync,
    MValue: marker::ResponseMarker,
    T: QubitHandler<Ctx, MSig>,
    T::Return: Stream + Send,
    <T::Return as Stream>::Item: Send + ResponseValue<MValue>,
{
    /// The response is the [`Stream::Item`] of the resulting stream. This response value will be
    /// produced multiple times.
    type Response = <T::Return as Stream>::Item;

    /// These handlers will be registered usig [`RpcModule::register_subscription`].
    fn register(self, module: &mut RpcModule<Ctx>, method_name: String) {
        let notif_method_name = format!("{method_name}_notif");
        let unsub_method_name = format!("{method_name}_unsub");

        module
            .register_subscription(
                Box::leak(method_name.into_boxed_str()),
                Box::leak(notif_method_name.into_boxed_str()),
                Box::leak(unsub_method_name.into_boxed_str()),
                move |params, pending, ctx, extensions| {
                    let f = self.clone();

                    async move {
                        let ctx =
                            match Self::Ctx::from_request_extensions((*ctx).clone(), extensions)
                                .await
                            {
                                Ok(ctx) => ctx,
                                Err(e) => {
                                    pending.reject(e).await;
                                    return SubscriptionCloseResponse::None;
                                }
                            };

                        let sink = pending.accept().await.unwrap();

                        // Track the number of items emitted through the subscription
                        let mut count = 0;
                        let subscription_id = sink.subscription_id();

                        let mut stream = pin!(f.call(ctx, params));

                        while let Some(item) = stream.next().await {
                            let item = serde_json::value::to_raw_value(&item.transform()).unwrap();
                            if let Some(DisconnectError(..)) = sink.send(item).await.err() {
                                break;
                            };

                            count += 1;
                        }

                        // Notify that stream is closing
                        SubscriptionCloseResponse::Notif(SubscriptionMessage::from(
                            serde_json::value::to_raw_value(
                                &json!({ "close_stream": subscription_id, "count": count }),
                            )
                            .unwrap(),
                        ))
                    }
                },
            )
            .unwrap();
    }
}

// TODO: Combine the duplicated `register_subscription` logic between sync and async streams.

/// Register any handler that returns a [`Future`] that outputs a [`Stream`] containing items
/// implementing [`ResponseValue`]. This implementation only supports async handlers.
impl<Ctx, T, MValue, MSig>
    RegisterableHandler<Ctx, MSig, MValue, marker::MFuture<marker::MStream<MValue>>> for T
where
    Ctx: 'static + Clone + Send + Sync,
    MValue: marker::ResponseMarker,
    T: QubitHandler<Ctx, MSig>,
    T::Return: Send + Future,
    <T::Return as Future>::Output: Stream + Send,
    <<T::Return as Future>::Output as Stream>::Item: Send + ResponseValue<MValue>,
{
    type Response = <<T::Return as Future>::Output as Stream>::Item;

    fn register(self, module: &mut RpcModule<Ctx>, method_name: String) {
        let notif_method_name = format!("{method_name}_notif");
        let unsub_method_name = format!("{method_name}_unsub");

        module
            .register_subscription(
                Box::leak(method_name.into_boxed_str()),
                Box::leak(notif_method_name.into_boxed_str()),
                Box::leak(unsub_method_name.into_boxed_str()),
                move |params, pending, ctx, extensions| {
                    let f = self.clone();

                    async move {
                        let ctx =
                            match Self::Ctx::from_request_extensions((*ctx).clone(), extensions)
                                .await
                            {
                                Ok(ctx) => ctx,
                                Err(e) => {
                                    pending.reject(e).await;
                                    return SubscriptionCloseResponse::None;
                                }
                            };

                        let sink = pending.accept().await.unwrap();

                        // Track the number of items emitted through the subscription
                        let mut count = 0;
                        let subscription_id = sink.subscription_id();

                        let mut stream = pin!(f.call(ctx, params).await);

                        while let Some(item) = stream.next().await {
                            let item = serde_json::value::to_raw_value(&item.transform()).unwrap();
                            if let Some(DisconnectError(..)) = sink.send(item).await.err() {
                                break;
                            };
                            count += 1;
                        }

                        // Notify that stream is closing
                        SubscriptionCloseResponse::Notif(SubscriptionMessage::from(
                            serde_json::value::to_raw_value(
                                &json!({ "close_stream": subscription_id, "count": count }),
                            )
                            .unwrap(),
                        ))
                    }
                },
            )
            .unwrap();
    }
}

#[cfg(test)]
mod test {
    use crate::{RpcError, handler::ts::TypeCollector};

    use super::{ctx::FromRequestExtensions, *};

    use futures::stream;
    use rstest::rstest;
    use serde_json::{Value, json};

    use std::{fmt::Debug, iter};

    mod register {
        //! Test registering different kinds of handlers to a [`RpcModule`], and call them to
        //! ensure they produce the correct response.

        use jsonrpsee::RpcModule;
        use serde::Deserialize;

        use super::*;

        /// Produce an iterator counting from 0 to 2 (inclusive).
        fn simple_iter() -> impl Iterator<Item = usize> {
            0..3
        }

        /// Register a handler to a module, and return the module. The handler will be
        /// registered at `handler`.
        fn register_handler<
            F,
            MSig,
            MValue: marker::ResponseMarker,
            MReturn: marker::HandlerReturnMarker,
        >(
            handler: F,
        ) -> RpcModule<()>
        where
            F: RegisterableHandler<(), MSig, MValue, MReturn, Ctx = ()>,
        {
            let mut module = RpcModule::new(());
            F::register(handler, &mut module, "handler".to_string());
            module
        }

        /// Register a handler to a module, and call it, returning the value that was
        /// returned from the handler according to [`ReturnType`].
        async fn test_handler<
            F,
            MSig,
            MValue: marker::ResponseMarker,
            MReturn: marker::HandlerReturnMarker,
        >(
            handler: F,
        ) -> <F::Response as ResponseValue<MValue>>::Value
        where
            F: RegisterableHandler<(), MSig, MValue, MReturn, Ctx = ()>,
            <F::Response as ResponseValue<MValue>>::Value: for<'a> Deserialize<'a>,
        {
            let module = register_handler(handler);

            let fut = module
                .call::<[(); 0], <F::Response as ResponseValue<MValue>>::Value>("handler", []);
            fut.await.unwrap()
        }

        /// Primitive `TS` values should be returned as-is.
        #[tokio::test]
        async fn ts() {
            assert_eq!(test_handler(|| 123u32).await, 123);
        }

        /// Iterators should be collected and returned as a `Vec`.
        #[tokio::test]
        async fn iter() {
            assert_eq!(test_handler(simple_iter).await, vec![0, 1, 2]);
        }

        /// Stream should be consumed and each value returned one at a time.
        #[tokio::test]
        async fn stream() {
            let module = register_handler(|| futures::stream::iter(simple_iter()));
            let mut subs = module.subscribe("handler", [] as [(); 0], 3).await.unwrap();

            let mut next = async || subs.next::<usize>().await.unwrap().unwrap().0;

            // Values should be produced in-order.
            assert_eq!(0, next().await);
            assert_eq!(1, next().await);
            assert_eq!(2, next().await);

            // Stream should be over, so the summary object should be sent.
            assert_eq!(
                subs.next::<Value>().await.unwrap().unwrap().0["count"]
                    .as_i64()
                    .unwrap(),
                3
            );
            assert!(subs.next::<Value>().await.is_none());
        }
    }

    /// Register a bunch of different complex handler types.
    #[rstest]
    #[case::ts_value(|| 123)]
    #[case::async_ts_value(|| async { 123 })]
    #[case::stream(|| stream::once(async { 123 }))]
    #[case::async_stream(|| async { stream::once(async { 123 }) })]
    #[case::iter(|| iter::once(123))]
    #[case::async_iter(|| async { iter::once(123) })]
    #[case::stream_iter(|| stream::once(async { iter::once(123) }))]
    #[case::async_stream_iter(|| async { stream::once(async { iter::once(123) }) })]
    #[case::iter_iter(|| iter::once(iter::once(123)))]
    #[case::async_iter_iter(|| async { iter::once(iter::once(123)) })]
    #[case::stream_iter_iter(|| stream::once(async { iter::once(iter::once(123)) }))]
    #[case::async_stream_iter_iter(|| async { stream::once(async { iter::once(iter::once(123)) }) })]
    fn register_handler<
        MSig,
        MValue: marker::ResponseMarker,
        MReturn: marker::HandlerReturnMarker,
    >(
        #[case] handler: impl RegisterableHandler<(), MSig, MValue, MReturn, Ctx = ()>,
    ) {
        handler.register(&mut RpcModule::new(()), "handler".to_string());
    }

    /// Call some handlers, and assert the output.
    #[rstest]
    #[case(|| {}, json!([]), ())]
    #[case(|_ctx: ()| {}, json!([]), ())]
    #[case(|_ctx: (), param: u32| param, json!([123]), 123)]
    #[case(|_ctx: (), param_1: u32, param_2: String| -> (u32, String) { (param_1, param_2) }, json!([123, "hello"]), (123, "hello".to_string()))]
    fn call_handler<H, MSig>(#[case] handler: H, #[case] params: Value, #[case] expected: H::Return)
    where
        H: QubitHandler<(), MSig, Ctx = ()>,
        H::Return: Debug + PartialEq,
    {
        let output = handler.call(
            (),
            Params::new(Some(&serde_json::to_string(&params).unwrap())).into_owned(),
        );

        assert_eq!(output, expected);
    }

    /// Sample CTX.
    #[derive(Clone)]
    struct SampleCtx;

    /// Sample CTX that derives from [`SampleCtx`].
    #[derive(Clone)]
    struct DerivedCtx;
    impl FromRequestExtensions<SampleCtx> for DerivedCtx {
        async fn from_request_extensions(
            _ctx: SampleCtx,
            _extensions: http::Extensions,
        ) -> Result<Self, RpcError> {
            Ok(DerivedCtx)
        }
    }

    /// Ensure that a handler can be registered if the ctx can be derived from the module ctx.
    #[test]
    fn derived_ctx() {
        fn handler(_ctx: DerivedCtx) {}
        handler.register(&mut RpcModule::new(SampleCtx), "handler".to_string());
    }

    /// Assert that a handler implements [`RegisterableHandler`], and the reflected TS types are correct.
    #[rstest]
    #[case::unit_handler(|| {}, (), [], "null")]
    #[case::unit_handler_other_ctx(|| {}, SampleCtx, [], "null")]
    #[case::single_ctx_param(|_ctx: SampleCtx| {}, SampleCtx, [], "null")]
    #[case::only_return_ty(|| -> bool { todo!() }, (), [], "boolean")]
    #[case::ctx_and_param(|_ctx: SampleCtx, _a: u32| {}, SampleCtx, ["number"], "null")]
    #[case::ctx_and_param_and_return(|_ctx: SampleCtx, _a: u32| -> bool { todo!() }, SampleCtx, ["number"], "boolean")]
    #[case::ctx_and_multi_param(|_ctx: SampleCtx, _a: u32, _b: String, _c: bool| {}, SampleCtx, ["number", "string", "boolean"], "null")]
    #[case::ctx_and_multi_param_return(|_ctx: SampleCtx, _a: u32, _b: String, _c: bool| -> bool { todo!() }, SampleCtx, ["number", "string", "boolean"], "boolean")]
    #[case::produce_iter(|| { [1, 2, 3].into_iter() }, (), [], "Array<number>")]
    #[case::produce_stream(|| { stream::iter([1, 2, 3]) }, (), [], "number")]
    fn handler_ts_type<H, Ctx, MSig, MValue, MReturn>(
        #[case] _handler: H,
        #[case] _ctx: Ctx,
        #[case] expected_params: impl IntoIterator<Item = &'static str>,
        #[case] expected_return: &'static str,
    ) where
        MValue: marker::ResponseMarker,
        MReturn: marker::HandlerReturnMarker,
        H: RegisterableHandler<Ctx, MSig, MValue, MReturn>,
        Ctx: 'static + Clone + Send + Sync,
        H::Ctx: 'static + Send + Sync + FromRequestExtensions<Ctx>,
    {
        assert_eq!(
            TypeCollector::collect_names::<H::Params>(),
            expected_params.into_iter().collect::<Vec<_>>()
        );
        assert_eq!(
            <<H::Response as ResponseValue<_>>::Value as TS>::name(&ts_rs::Config::default()),
            expected_return
        );
    }

    mod handler_traits {
        //! Some random trait assertions for [`QubitHandler`].

        use super::*;

        use static_assertions::assert_impl_all;

        // Handler with no inputs/outputs.
        assert_impl_all!(
            fn () -> (): QubitHandler<(), ((), ()), Ctx = (), Params = (), Return = ()>
        );
        // Handler with single Ctx param.
        assert_impl_all!(
            fn (u32) -> (): QubitHandler<u32, ((u32,), ()), Ctx = u32, Params = (), Return = ()>
        );
        // Handler with Ctx param, and other parameters.
        assert_impl_all!(
            fn (u32, String, bool) -> (): QubitHandler<u32, ((u32, String, bool), ()), Ctx = u32, Params = (String, bool), Return = ()>
        );
        // Handler with primitive return type.
        assert_impl_all!(
            fn () -> u32: QubitHandler<(), ((), u32), Ctx = (), Params = (), Return = u32>
        );
        // Handler with iterator return type.
        assert_impl_all!(
            fn () -> std::vec::IntoIter<u32> : QubitHandler<(), ((), std::vec::IntoIter<u32>)>
        );
        // Handler with stream return type.
        assert_impl_all!(
            fn () -> futures::stream::Iter<std::vec::IntoIter<u32>> : QubitHandler<(), ((), futures::stream::Iter<std::vec::IntoIter<u32>>)>
        );
        // Handler returning a stream of iterators of iterators.
        assert_impl_all!(
            fn () -> futures::stream::Iter<std::vec::IntoIter<std::vec::IntoIter<u32>>> : QubitHandler<(), ((), futures::stream::Iter<std::vec::IntoIter<std::vec::IntoIter<u32>>>)>
        );
    }
}