rpc-io 0.5.0

JSON-RPC 2.0 engine for LSP, MCP, and service APIs: Content-Length and newline framings, a sans-IO core, blocking and async adapters, bounded non-blocking output, and hostile-input hardening.
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! [`Router`]: which handler answers which method, and what a handler gets:
//! [`Params`] and a [`Context`]. [`Event`]s report what the endpoint did on
//! its own.

use std::{
    collections::BTreeMap,
    fmt,
    sync::{Arc, OnceLock},
    time::Duration,
};

use jsonx_lib::{Parser, Value};

use crate::{
    error::Error,
    error_object::ErrorObject,
    framing::FrameError,
    handles::{Call, Peer, Responder},
    id::Id,
    link::{Link, Scope, Turn},
    raw::{Json, Raw},
};

/// A request handler. The `String` is a buffer the endpoint keeps for it to
/// encode a result into (and borrow it from) instead of allocating one.
pub(crate) type RequestFn<S> = Box<
    dyn for<'b> FnMut(
            &mut S,
            Params<'_>,
            &mut Context<'_>,
            &'b mut String,
        ) -> Result<Json<'b>, ErrorObject>
        + Send,
>;
pub(crate) type DeferredFn<S> =
    Box<dyn FnMut(&mut S, Params<'_>, &mut Context<'_>, Responder) + Send>;
/// A notification handler; `Err` when it could not read its params (a typed
/// handler), which is reported, not answered.
pub(crate) type NotificationFn<S> =
    Box<dyn FnMut(&mut S, Params<'_>, &mut Context<'_>) -> Result<(), ErrorObject> + Send>;
pub(crate) type EventFn<S> = Box<dyn FnMut(&mut S, &Event<'_>) + Send>;
pub(crate) type InterceptFn<S> =
    Box<dyn FnMut(&mut S, &str, Option<&Id<'_>>, &Params<'_>) -> Result<(), ErrorObject> + Send>;

/// How a request method is answered.
pub(crate) enum RequestHandler<S> {
    /// By the handler's return value.
    Now(RequestFn<S>),
    /// Through a [`Responder`], now or later.
    Deferred(DeferredFn<S>),
}

/// Which handler answers which method.
///
/// Handlers receive the endpoint's state `S` (`&mut`), the call's
/// [`Params`], and a [`Context`] for talking back. They run one at a time, in
/// the order messages arrive, on the endpoint's thread. A request handler's
/// `Ok` value (anything that converts into [`Json`]) becomes the result and
/// its `Err` the error; a [deferred](Router::deferred) handler answers through
/// a [`Responder`] instead.
///
/// A panic in any handler is contained: a request is answered with
/// `INTERNAL_ERROR` (the panic message goes to [`Event::Panicked`], never to
/// the peer), a notification gets nothing, and the session carries on. The
/// state may be left half-updated by the panicking handler; the endpoint does
/// not try to repair it. Containment needs `panic = "unwind"` (Rust's
/// default): under `panic = "abort"` a handler panic ends the process.
///
/// Requests for methods with no request handler are answered with
/// `METHOD_NOT_FOUND`. Notifications for methods with no notification handler
/// are ignored (reported as [`Event::UnknownNotification`]): requests and
/// notifications are registered separately, as LSP and MCP use them.
///
/// # Examples
///
/// ```
/// use rpc_io::jsonx::json;
/// use rpc_io::{ErrorObject, Router};
///
/// #[derive(Default)]
/// struct Counter { hits: u64 }
///
/// let router = Router::new()
///     .request("add", |_: &mut Counter, params, _| {
///         let p = params.parse()?;
///         match (p[0].as_i64(), p[1].as_i64()) {
///             (Some(a), Some(b)) => Ok(json!(a + b)),
///             _ => Err(ErrorObject::invalid_params("expected two integers")),
///         }
///     })
///     .notification("hit", |state, _, _| state.hits += 1);
/// assert!(router.has_request("add"));
/// assert!(router.has_notification("hit"));
/// ```
pub struct Router<S> {
    pub(crate) requests: BTreeMap<Box<str>, RequestHandler<S>>,
    pub(crate) notifications: BTreeMap<Box<str>, NotificationFn<S>>,
    pub(crate) on_event: Option<EventFn<S>>,
    pub(crate) intercept: Option<InterceptFn<S>>,
}

impl<S> Router<S> {
    /// A router with no handlers.
    ///
    /// # Examples
    ///
    /// ```
    /// let router: rpc_io::Router<()> = rpc_io::Router::new();
    /// assert!(!router.has_request("anything"));
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            requests: BTreeMap::new(),
            notifications: BTreeMap::new(),
            on_event: None,
            intercept: None,
        }
    }

    /// Answers requests for `method` with `handler`'s return value,
    /// replacing any request handler registered for it before.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Router};
    ///
    /// let router = Router::new().request("ping", |_, _, _| Ok("pong"));
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// Endpoint::new(tx, router, ()).handle(br#"{"jsonrpc":"2.0","id":1,"method":"ping"}"#)?;
    /// assert_eq!(rx.try_recv().ok().as_deref(), Some(r#"{"jsonrpc":"2.0","id":1,"result":"pong"}"#));
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn request<F, J>(self, method: &str, mut handler: F) -> Self
    where
        F: FnMut(&mut S, Params<'_>, &mut Context<'_>) -> Result<J, ErrorObject> + Send + 'static,
        J: Into<Json<'static>>,
    {
        self.request_into(method, move |state, params, cx, _out| {
            handler(state, params, cx).map(Into::into)
        })
    }

    /// Registers a request handler that may encode its result into the
    /// endpoint's result buffer.
    pub(crate) fn request_into<F>(mut self, method: &str, handler: F) -> Self
    where
        F: for<'b> FnMut(
                &mut S,
                Params<'_>,
                &mut Context<'_>,
                &'b mut String,
            ) -> Result<Json<'b>, ErrorObject>
            + Send
            + 'static,
    {
        let _replaced = self
            .requests
            .insert(method.into(), RequestHandler::Now(Box::new(handler)));
        self
    }

    /// Answers requests for `method` through a [`Responder`] the handler
    /// receives: now, from a later message, or from another thread. The
    /// request stays in flight (and cancellable by the peer) until answered;
    /// at most [`Config::max_in_flight`](crate::Config) can be, and more are
    /// refused with `SERVER_OVERLOADED`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::Router;
    ///
    /// let router: Router<()> = Router::new().deferred("index", |_, params, _, responder| {
    ///     let params = params.into_owned(); // keep it beyond this call
    ///     std::thread::spawn(move || {
    ///         let _ = params;
    ///         responder.ok("indexed").ok();
    ///     });
    /// });
    /// assert!(router.has_request("index"));
    /// ```
    #[must_use]
    pub fn deferred<F>(mut self, method: &str, handler: F) -> Self
    where
        F: FnMut(&mut S, Params<'_>, &mut Context<'_>, Responder) + Send + 'static,
    {
        let _replaced = self
            .requests
            .insert(method.into(), RequestHandler::Deferred(Box::new(handler)));
        self
    }

    /// Handles notifications for `method`, replacing any notification handler
    /// registered for it before.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Router};
    ///
    /// let router = Router::new().notification("log", |seen: &mut Vec<String>, params, _| {
    ///     seen.push(params.raw().map(|r| r.as_str().to_owned()).unwrap_or_default());
    /// });
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// let mut endpoint = Endpoint::new(tx, router, Vec::new());
    /// endpoint.handle(br#"{"jsonrpc":"2.0","method":"log","params":["hi"]}"#)?;
    /// assert_eq!(endpoint.state(), &["[\"hi\"]"]);
    /// assert!(rx.try_recv().is_err()); // notifications are never answered
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn notification<F>(self, method: &str, mut handler: F) -> Self
    where
        F: FnMut(&mut S, Params<'_>, &mut Context<'_>) + Send + 'static,
    {
        self.notification_with(method, move |state, params, cx| {
            handler(state, params, cx);
            Ok(())
        })
    }

    /// Registers a notification handler that may refuse its params.
    pub(crate) fn notification_with<F>(mut self, method: &str, handler: F) -> Self
    where
        F: FnMut(&mut S, Params<'_>, &mut Context<'_>) -> Result<(), ErrorObject> + Send + 'static,
    {
        let _replaced = self.notifications.insert(method.into(), Box::new(handler));
        self
    }

    /// Calls `hook` for every [`Event`]: things the endpoint handled on its
    /// own that an application may want to log. Runs on the endpoint's thread;
    /// a panic in it is contained and ignored.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Event, Router};
    ///
    /// let router = Router::new().on_event(|log: &mut Vec<String>, event| {
    ///     if let Event::UnknownNotification { method } = event {
    ///         log.push(method.to_string());
    ///     }
    /// });
    /// let (tx, _rx) = std::sync::mpsc::channel::<String>();
    /// let mut endpoint = Endpoint::new(tx, router, Vec::new());
    /// endpoint.handle(br#"{"jsonrpc":"2.0","method":"nobody/listens"}"#)?;
    /// assert_eq!(endpoint.state(), &["nobody/listens"]);
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn on_event<F>(mut self, hook: F) -> Self
    where
        F: FnMut(&mut S, &Event<'_>) + Send + 'static,
    {
        self.on_event = Some(Box::new(hook));
        self
    }

    /// Runs `hook` before every call the router would route: each request
    /// and each notification, after the gate and cancellation have had their
    /// say and before the handler is looked up. `Ok(())` lets the call
    /// through; `Err(error)` refuses it: a request is answered with `error`,
    /// a notification is dropped and reported as [`Event::Refused`]. The
    /// place for authorization, rate limits, or an audit log. The hook gets
    /// the state, the method, the id (`None` for a notification), and the
    /// params; a panic in it is contained as a handler's would be.
    /// Registering a hook again replaces it.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, ErrorCode, ErrorObject, Router};
    ///
    /// #[derive(Default)]
    /// struct Session { calls: u32, admin: bool }
    ///
    /// let router = Router::new()
    ///     .request("admin/reset", |_: &mut Session, _, _| Ok(()))
    ///     .request("ping", |_, _, _| Ok("pong"))
    ///     .intercept(|session, method, _id, _params| {
    ///         session.calls += 1;
    ///         if method.starts_with("admin/") && !session.admin {
    ///             return Err(ErrorObject::new(ErrorCode(1003), "forbidden"));
    ///         }
    ///         Ok(())
    ///     });
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// let mut endpoint = Endpoint::new(tx, router, Session::default());
    /// endpoint.handle(br#"{"jsonrpc":"2.0","id":1,"method":"admin/reset"}"#)?;
    /// endpoint.handle(br#"{"jsonrpc":"2.0","id":2,"method":"ping"}"#)?;
    /// let replies: Vec<String> = rx.try_iter().collect();
    /// assert_eq!(replies[0], r#"{"jsonrpc":"2.0","id":1,"error":{"code":1003,"message":"forbidden"}}"#);
    /// assert_eq!(replies[1], r#"{"jsonrpc":"2.0","id":2,"result":"pong"}"#);
    /// assert_eq!(endpoint.state().calls, 2);
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn intercept<F>(mut self, hook: F) -> Self
    where
        F: FnMut(&mut S, &str, Option<&Id<'_>>, &Params<'_>) -> Result<(), ErrorObject>
            + Send
            + 'static,
    {
        self.intercept = Some(Box::new(hook));
        self
    }

    /// Whether a request handler is registered for `method`.
    ///
    /// # Examples
    ///
    /// ```
    /// let router: rpc_io::Router<()> = rpc_io::Router::new().request("a", |_, _, _| Ok(()));
    /// assert!(router.has_request("a"));
    /// assert!(!router.has_notification("a"));
    /// ```
    #[must_use]
    pub fn has_request(&self, method: &str) -> bool {
        self.requests.contains_key(method)
    }

    /// Whether a notification handler is registered for `method`.
    ///
    /// # Examples
    ///
    /// ```
    /// let router: rpc_io::Router<()> = rpc_io::Router::new().notification("n", |_, _, _| {});
    /// assert!(router.has_notification("n"));
    /// ```
    #[must_use]
    pub fn has_notification(&self, method: &str) -> bool {
        self.notifications.contains_key(method)
    }
}

impl<S> Default for Router<S> {
    fn default() -> Self {
        Self::new()
    }
}

impl<S> fmt::Debug for Router<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Router")
            .field("requests", &self.requests.keys().collect::<Vec<_>>())
            .field(
                "notifications",
                &self.notifications.keys().collect::<Vec<_>>(),
            )
            .finish_non_exhaustive()
    }
}

/// A call's `params`: validated, kept as a slice of the message, parsed only
/// when asked.
///
/// # Examples
///
/// ```
/// use rpc_io::{Endpoint, Router};
///
/// let router = Router::new().request("pos", |_, params, _| {
///     let p = params.parse()?; // `INVALID_PARAMS` if over the parser's budgets
///     let line = p["line"].as_u64().ok_or_else(|| {
///         rpc_io::ErrorObject::invalid_params("`line` must be a non-negative integer")
///     })?;
///     Ok(line + 1)
/// });
/// let (tx, rx) = std::sync::mpsc::channel::<String>();
/// Endpoint::new(tx, router, ()).handle(br#"{"jsonrpc":"2.0","id":1,"method":"pos","params":{"line":9}}"#)?;
/// assert_eq!(rx.try_recv().ok().as_deref(), Some(r#"{"jsonrpc":"2.0","id":1,"result":10}"#));
/// # Ok::<(), rpc_io::Error>(())
/// ```
#[derive(Clone, Debug)]
pub struct Params<'a> {
    raw: Option<Raw<'a>>,
    parser: Parser,
}

impl<'a> Params<'a> {
    pub(crate) fn new(raw: Option<Raw<'a>>, parser: Parser) -> Self {
        Self { raw, parser }
    }

    /// The parser with the endpoint's budgets.
    pub(crate) fn parser(&self) -> Parser {
        self.parser
    }

    /// The params' JSON text, `None` when absent.
    pub(crate) fn text(&self) -> Option<&str> {
        self.raw.as_ref().map(Raw::as_str)
    }

    /// Parses the params (an array or an object; `null` when absent), with
    /// the endpoint's JSON budgets and duplicate keys refused. The value
    /// borrows from the message.
    ///
    /// # Errors
    ///
    /// An `INVALID_PARAMS` [`ErrorObject`] saying why, ready to return with
    /// `?`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Router};
    ///
    /// let router = Router::new().request("dup", |_, params, _| params.parse().map(|_| ()));
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// Endpoint::new(tx, router, ()).handle(br#"{"jsonrpc":"2.0","id":1,"method":"dup","params":{"a":1,"a":2}}"#)?;
    /// assert!(rx.try_recv().is_ok_and(|m| m.contains("-32602")));
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    pub fn parse(&self) -> Result<Value<'_>, ErrorObject> {
        match &self.raw {
            Some(raw) => raw.parse_with(&self.parser).map_err(ErrorObject::from),
            None => Ok(Value::Null),
        }
    }

    /// The params as raw JSON text, `None` when absent.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Router};
    ///
    /// let router = Router::new().request("echo", |_, params, _| {
    ///     Ok(params.raw().map(|r| r.clone().into_owned()).unwrap_or(rpc_io::Raw::NULL))
    /// });
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// Endpoint::new(tx, router, ()).handle(br#"{"jsonrpc":"2.0","id":1,"method":"echo","params":[1, 2]}"#)?;
    /// assert_eq!(rx.try_recv().ok().as_deref(), Some(r#"{"jsonrpc":"2.0","id":1,"result":[1, 2]}"#));
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn raw(&self) -> Option<&Raw<'a>> {
        self.raw.as_ref()
    }

    /// Whether params were sent.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Router};
    ///
    /// let router = Router::new().request("m", |_, params, _| Ok(params.is_present()));
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// Endpoint::new(tx, router, ()).handle(br#"{"jsonrpc":"2.0","id":1,"method":"m"}"#)?;
    /// assert!(rx.try_recv().is_ok_and(|m| m.ends_with(r#""result":false}"#)));
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn is_present(&self) -> bool {
        self.raw.is_some()
    }

    /// Copies the params so they can outlive the message, to move them to
    /// another thread.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Params, Router};
    ///
    /// let router = Router::<Vec<Params<'static>>>::new().notification("keep", |kept, params, _| {
    ///     kept.push(params.into_owned());
    /// });
    /// # let _ = router;
    /// ```
    #[must_use]
    pub fn into_owned(self) -> Params<'static> {
        Params {
            raw: self.raw.map(Raw::into_owned),
            parser: self.parser,
        }
    }

    /// The params as raw JSON, `None` when absent.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Router};
    ///
    /// // Forward a request's params to the peer unchanged: no parse, no re-serialization.
    /// let router = Router::new().notification("relay", |_, params, cx| {
    ///     let raw = params.into_raw().unwrap_or(rpc_io::Raw::NULL);
    ///     cx.notify("relayed", raw).ok();
    /// });
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// Endpoint::new(tx, router, ()).handle(br#"{"jsonrpc":"2.0","method":"relay","params":{"k": [1]}}"#)?;
    /// assert_eq!(rx.try_recv().ok().as_deref(), Some(r#"{"jsonrpc":"2.0","method":"relayed","params":{"k": [1]}}"#));
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn into_raw(self) -> Option<Raw<'a>> {
        self.raw
    }
}

/// What a handler can reach while it runs: the method and id it is handling,
/// and a channel back to the peer.
///
/// Messages sent through a context during a request go out before that
/// request's response, in the order they were sent.
///
/// # Examples
///
/// ```
/// use rpc_io::{Endpoint, Router};
///
/// let router = Router::new().request("work", |_, _, cx| {
///     cx.notify("progress", rpc_io::jsonx::json!({ "method": cx.method() }))?;
///     Ok(())
/// });
/// let (tx, rx) = std::sync::mpsc::channel::<String>();
/// Endpoint::new(tx, router, ()).handle(br#"{"jsonrpc":"2.0","id":1,"method":"work"}"#)?;
/// let sent: Vec<String> = rx.try_iter().collect();
/// assert_eq!(sent[0], r#"{"jsonrpc":"2.0","method":"progress","params":{"method":"work"}}"#);
/// assert_eq!(sent[1], r#"{"jsonrpc":"2.0","id":1,"result":null}"#);
/// # Ok::<(), rpc_io::Error>(())
/// ```
pub struct Context<'a> {
    link: &'a Arc<dyn Link>,
    stop: &'a mut bool,
    id: Option<&'a Id<'a>>,
    method: &'a str,
    /// For a deferred request (answered through a `Responder`), its serial.
    deferred: Option<u64>,
    /// Set when a request answered by the handler's return value has its
    /// response written; made only if a scoped progress handle asks for it.
    /// A `OnceLock` rather than a `OnceCell`, so `Context` stays `Sync`.
    turn: OnceLock<Arc<Turn>>,
}

impl<'a> Context<'a> {
    pub(crate) fn new(
        link: &'a Arc<dyn Link>,
        stop: &'a mut bool,
        id: Option<&'a Id<'a>>,
        method: &'a str,
        deferred: Option<u64>,
    ) -> Self {
        Self {
            link,
            stop,
            id,
            method,
            deferred,
            turn: OnceLock::new(),
        }
    }

    /// The request this call is, for a notification that must not outlive
    /// it; `None` for a notification.
    pub(crate) fn scope(&self) -> Option<Scope> {
        let id = self.id?;
        Some(match self.deferred {
            Some(serial) => Scope::Deferred {
                id: id.clone().into_owned(),
                serial,
            },
            None => Scope::Turn(Arc::clone(self.turn.get_or_init(|| Arc::new(Turn::new())))),
        })
    }

    /// A [`Notifier`](crate::Notifier) bound to the request being handled:
    /// it sends while the request is in flight and refuses once it is
    /// answered (for a request answered by the handler's return value, as
    /// soon as the handler returns) or cancelled. `None` for a notification.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Error, Notifier, Router};
    ///
    /// let router = Router::new().request("work", |kept: &mut Option<Notifier>, _, cx| {
    ///     let notifier = cx.notifier();
    ///     if let Some(n) = &notifier {
    ///         n.notify("working", ())?;
    ///     }
    ///     *kept = notifier;
    ///     Ok("done")
    /// });
    /// let (tx, rx) = std::sync::mpsc::channel::<String>();
    /// let mut endpoint = Endpoint::new(tx, router, None);
    /// endpoint.handle(br#"{"jsonrpc":"2.0","id":1,"method":"work"}"#)?;
    /// assert_eq!(rx.try_iter().count(), 2); // the notification, then the result
    /// let late = endpoint.state().as_ref().map(|n| n.notify("working", ()));
    /// assert!(matches!(late, Some(Err(Error::AlreadyAnswered))));
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    #[must_use]
    pub fn notifier(&self) -> Option<crate::Notifier> {
        self.scope()
            .map(|scope| crate::Notifier::new(self.peer(), scope))
    }

    /// The flag to set when the response is written, if a scoped handle
    /// took one.
    pub(crate) fn take_turn(&mut self) -> Option<Arc<Turn>> {
        self.turn.take()
    }

    /// The method being handled.
    ///
    /// # Examples
    ///
    /// ```
    /// let router: rpc_io::Router<()> = rpc_io::Router::new().request("m", |_, _, cx| Ok(cx.method().len()));
    /// # let _ = router;
    /// ```
    #[must_use]
    pub fn method(&self) -> &str {
        self.method
    }

    /// The id of the request being handled; `None` for a notification.
    ///
    /// # Examples
    ///
    /// ```
    /// let router: rpc_io::Router<()> =
    ///     rpc_io::Router::new().request("m", |_, _, cx| Ok(cx.id().and_then(|id| id.as_i64())));
    /// # let _ = router;
    /// ```
    #[must_use]
    pub fn id(&self) -> Option<&Id<'a>> {
        self.id
    }

    /// Sends a notification to the peer; see [`Peer::notify`].
    ///
    /// # Errors
    ///
    /// As [`Peer::notify`].
    ///
    /// # Examples
    ///
    /// ```
    /// let router: rpc_io::Router<()> = rpc_io::Router::new().notification("n", |_, _, cx| {
    ///     cx.notify("ack", ()).ok();
    /// });
    /// # let _ = router;
    /// ```
    pub fn notify<'j>(&self, method: &str, params: impl Into<Json<'j>>) -> Result<(), Error> {
        Peer::new(Arc::clone(self.link)).notify(method, params)
    }

    /// Sends a request to the peer; see [`Peer::request`]. Its [`Call`]
    /// cannot be waited on here (the answer arrives on this thread, after the
    /// handler returns): keep it and [`try_wait`](Call::try_wait) later, or
    /// wait on another thread.
    ///
    /// # Errors
    ///
    /// As [`Peer::request`].
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Call, Router};
    ///
    /// let router = Router::<Vec<Call>>::new().notification("initialized", |calls, _, cx| {
    ///     if let Ok(call) = cx.request("workspace/configuration", rpc_io::jsonx::json!({ "items": [] })) {
    ///         calls.push(call);
    ///     }
    /// });
    /// # let _ = router;
    /// ```
    pub fn request<'j>(&self, method: &str, params: impl Into<Json<'j>>) -> Result<Call, Error> {
        Peer::new(Arc::clone(self.link)).request(method, params)
    }

    /// Sends a request with a timeout; see [`Peer::request_with_timeout`].
    ///
    /// # Errors
    ///
    /// As [`Peer::request`].
    ///
    /// # Examples
    ///
    /// ```
    /// use std::time::Duration;
    ///
    /// let router: rpc_io::Router<()> = rpc_io::Router::new().notification("n", |_, _, cx| {
    ///     let _call = cx.request_with_timeout("ask", (), Duration::from_secs(1));
    /// });
    /// # let _ = router;
    /// ```
    pub fn request_with_timeout<'j>(
        &self,
        method: &str,
        params: impl Into<Json<'j>>,
        timeout: Duration,
    ) -> Result<Call, Error> {
        Peer::new(Arc::clone(self.link)).request_with_timeout(method, params, timeout)
    }

    /// A [`Peer`] for this connection, to keep or move to another thread.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Peer, Router};
    ///
    /// let router: Router<Option<Peer>> = Router::new().notification("hello", |slot, _, cx| {
    ///     *slot = Some(cx.peer());
    /// });
    /// # let _ = router;
    /// ```
    #[must_use]
    pub fn peer(&self) -> Peer {
        Peer::new(Arc::clone(self.link))
    }

    /// Stops the endpoint after this message: [`Endpoint::run`](crate::Endpoint::run)
    /// returns [`End::Stopped`](crate::End) and
    /// [`Endpoint::handle`](crate::Endpoint::handle) returns
    /// [`Flow::Stop`](crate::Flow). A response to the current request is still
    /// sent.
    ///
    /// # Examples
    ///
    /// ```
    /// use rpc_io::{Endpoint, Flow, Router};
    ///
    /// let router = Router::new().notification("exit", |_, _, cx| cx.stop());
    /// let (tx, _rx) = std::sync::mpsc::channel::<String>();
    /// let mut endpoint = Endpoint::new(tx, router, ());
    /// assert_eq!(endpoint.handle(br#"{"jsonrpc":"2.0","method":"exit"}"#)?, Flow::Stop);
    /// # Ok::<(), rpc_io::Error>(())
    /// ```
    pub fn stop(&mut self) {
        *self.stop = true;
    }
}

impl fmt::Debug for Context<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Context")
            .field("method", &self.method)
            .field("id", &self.id)
            .finish_non_exhaustive()
    }
}

/// Something the endpoint handled on its own, reported to
/// [`Router::on_event`] so an application can log it.
///
/// # Examples
///
/// ```
/// use rpc_io::Event;
///
/// fn describe(event: &Event<'_>) -> String {
///     match event {
///         Event::Panicked { method, message } => format!("`{method}` panicked: {message}"),
///         other => format!("{other:?}"),
///     }
/// }
/// assert_eq!(describe(&Event::Panicked { method: "m", message: "boom" }), "`m` panicked: boom");
/// ```
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Event<'a> {
    /// A handler panicked. A request was answered with `INTERNAL_ERROR`
    /// (without the panic message, which may hold details the peer should not
    /// see); the message is here.
    Panicked {
        /// The method whose handler panicked.
        method: &'a str,
        /// The panic message, clipped, or a placeholder.
        message: &'a str,
    },
    /// A malformed message arrived: answered with `error` if it was
    /// request-like, dropped if it was a malformed response to one of our
    /// requests (which then ends with `CallError::InvalidResponse`).
    Invalid {
        /// What was wrong.
        error: &'a ErrorObject,
        /// Whether it was answered.
        answered: bool,
    },
    /// A notification for a method with no notification handler.
    UnknownNotification {
        /// The method.
        method: &'a str,
    },
    /// A notification dropped because it arrived before the
    /// [`Gate`](crate::Gate) opened.
    Dropped {
        /// The method.
        method: &'a str,
    },
    /// A response whose id matches none of our outstanding requests (late,
    /// repeated, or forged).
    Unmatched {
        /// The id it carried.
        id: &'a Id<'a>,
    },
    /// An error response with a `null` id: the peer could not read something
    /// we sent.
    PeerError {
        /// The peer's error.
        error: &'a ErrorObject,
    },
    /// A message over [`Limits::max_message_bytes`](crate::Limits) was skipped
    /// unread, and answered with `INVALID_REQUEST` under a `null` id.
    Oversized {
        /// The framing error with the sizes.
        error: FrameError,
    },
    /// The peer cancelled one of its requests that was in flight.
    Cancelled {
        /// The cancelled request's id.
        id: &'a Id<'a>,
    },
    /// A notification refused by the router's
    /// [interceptor](Router::intercept); it was not handled.
    Refused {
        /// The method.
        method: &'a str,
        /// The interceptor's error.
        error: &'a ErrorObject,
    },
    /// A notification's params did not fit what its handler takes (a typed
    /// handler: [`Router::notification_json`], or `notification_typed` with
    /// the `serde` feature); the handler was not called.
    InvalidParams {
        /// The method.
        method: &'a str,
        /// What did not fit, as an `INVALID_PARAMS` error.
        error: &'a ErrorObject,
    },
}