lsp-server-tokio 0.4.0

An async-first LSP server infrastructure crate using Tokio
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
//! Request queue types for tracking pending LSP requests.
//!
//! This module provides types for tracking request-response correlation in both
//! directions of LSP communication:
//!
//! - [`IncomingRequests`] - Tracks requests received from clients (server needs to send responses)
//! - [`OutgoingRequests`] - Tracks requests sent to clients (server is waiting for responses)
//! - [`RequestQueue`] - Combines both for complete request lifecycle management
//!
//! # Usage Pattern
//!
//! ```
//! use lsp_server_tokio::{RequestQueue, RequestId};
//! use tokio_util::sync::CancellationToken;
//!
//! // Create a queue with custom metadata types
//! let mut queue: RequestQueue<String> = RequestQueue::new();
//!
//! // Track an incoming request (from client) with a cancellation token
//! let request_id: RequestId = 1.into();
//! let token = CancellationToken::new();
//! queue.incoming.register(request_id.clone(), "handler_context".to_string(), token);
//! assert!(queue.incoming.is_pending(&request_id));
//!
//! // When ready to respond, complete the request
//! let metadata = queue.incoming.complete(&request_id);
//! assert_eq!(metadata, Some("handler_context".to_string()));
//! ```
//!
//! # Server-Initiated Requests
//!
//! ```
//! use lsp_server_tokio::{RequestQueue, RequestId, Response};
//! use serde_json::json;
//!
//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
//! let mut queue: RequestQueue<()> = RequestQueue::new();
//!
//! // Register an outgoing request (to client) and get a receiver
//! let request_id: RequestId = 42.into();
//! let rx = queue.outgoing.register(request_id.clone());
//!
//! // When response arrives, complete the request
//! let response = Response::ok(42, json!({"result": "ok"}));
//! let sent = queue.outgoing.complete(&request_id, response);
//! assert!(sent);
//!
//! // The receiver gets the response
//! let received = rx.await.unwrap();
//! assert_eq!(received.id, Some(42.into()));
//! # });
//! ```

use std::collections::HashMap;
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;

use crate::request_id::RequestId;
use crate::Response;

/// The method name for cancel request notifications per LSP specification.
pub const CANCEL_REQUEST_METHOD: &str = "$/cancelRequest";

/// Parses the request ID from $/cancelRequest notification params.
///
/// According to the LSP specification, $/cancelRequest has params:
/// ```json
/// { "id": number | string }
/// ```
///
/// Returns `None` if params are missing, malformed, or the ID is not
/// a valid integer or string.
///
/// # Example
///
/// ```
/// use lsp_server_tokio::parse_cancel_params;
/// use serde_json::json;
///
/// // Integer ID
/// let params = Some(json!({"id": 42}));
/// let id = parse_cancel_params(&params);
/// assert_eq!(id, Some(42.into()));
///
/// // String ID
/// let params = Some(json!({"id": "request-abc"}));
/// let id = parse_cancel_params(&params);
/// assert_eq!(id, Some("request-abc".into()));
///
/// // Missing params
/// let id = parse_cancel_params(&None);
/// assert!(id.is_none());
/// ```
#[must_use]
pub fn parse_cancel_params(params: &Option<serde_json::Value>) -> Option<RequestId> {
    let params = params.as_ref()?;
    let id_value = params.get("id")?;

    match id_value {
        serde_json::Value::Number(n) => n
            .as_i64()
            .and_then(|i| i32::try_from(i).ok())
            .map(RequestId::Integer),
        serde_json::Value::String(s) => Some(RequestId::String(s.clone())),
        _ => None,
    }
}

/// Tracks requests received from clients (incoming to the server).
///
/// When the server receives a request, it registers the request ID along with
/// any metadata needed to process the response. When the server is ready to
/// send a response, it completes the request to retrieve the metadata.
///
/// Each request is also associated with a [`CancellationToken`] that can be
/// used to signal cancellation (e.g., when receiving `$/cancelRequest`).
///
/// The generic parameter `I` represents user-defined metadata associated with
/// each incoming request (e.g., handler context, timing info, request origin).
///
/// # Example
///
/// ```
/// use lsp_server_tokio::{IncomingRequests, RequestId};
/// use tokio_util::sync::CancellationToken;
///
/// let mut incoming: IncomingRequests<String> = IncomingRequests::new();
///
/// // Register a request with metadata and cancellation token
/// let token1 = CancellationToken::new();
/// let token2 = CancellationToken::new();
/// incoming.register(1.into(), "textDocument/hover".to_string(), token1);
/// incoming.register(2.into(), "textDocument/completion".to_string(), token2);
///
/// assert_eq!(incoming.pending_count(), 2);
/// assert!(incoming.is_pending(&1.into()));
///
/// // Cancel a request
/// incoming.cancel(&2.into());
///
/// // Complete request and get metadata back
/// let method = incoming.complete(&1.into());
/// assert_eq!(method, Some("textDocument/hover".to_string()));
/// assert_eq!(incoming.pending_count(), 1);
/// ```
#[derive(Debug)]
pub struct IncomingRequests<I> {
    pending: HashMap<RequestId, (I, CancellationToken)>,
}

impl<I> IncomingRequests<I> {
    /// Creates a new empty incoming request tracker.
    #[must_use]
    pub fn new() -> Self {
        Self {
            pending: HashMap::new(),
        }
    }

    /// Registers an incoming request with associated metadata and cancellation token.
    ///
    /// The metadata can be any user-defined type that you want to associate
    /// with this request until it's completed. The cancellation token can be
    /// used to signal request cancellation to async handlers.
    pub fn register(&mut self, id: RequestId, data: I, token: CancellationToken) {
        self.pending.insert(id, (data, token));
    }

    /// Completes an incoming request, removing it from tracking and returning the metadata.
    ///
    /// Returns `Some(metadata)` if the request was pending, `None` otherwise.
    /// The cancellation token is dropped when the request is completed.
    pub fn complete(&mut self, id: &RequestId) -> Option<I> {
        self.pending.remove(id).map(|(data, _)| data)
    }

    /// Returns `true` if the request is currently pending.
    #[must_use]
    pub fn is_pending(&self, id: &RequestId) -> bool {
        self.pending.contains_key(id)
    }

    /// Cancels a pending request by triggering its cancellation token.
    ///
    /// Returns `true` if the request was found and cancelled, `false` if the
    /// request ID was not pending. Note that cancelling an already-cancelled
    /// token is a no-op.
    #[must_use]
    pub fn cancel(&self, id: &RequestId) -> bool {
        if let Some((_, token)) = self.pending.get(id) {
            token.cancel();
            true
        } else {
            false
        }
    }

    /// Returns a clone of the cancellation token for a pending request.
    ///
    /// Returns `None` if the request is not pending. The returned token
    /// can be passed to async handlers for cooperative cancellation.
    #[must_use]
    pub fn get_token(&self, id: &RequestId) -> Option<CancellationToken> {
        self.pending.get(id).map(|(_, token)| token.clone())
    }

    /// Returns the number of currently pending requests.
    #[must_use]
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }
}

impl<I> Default for IncomingRequests<I> {
    fn default() -> Self {
        Self::new()
    }
}

/// Tracks requests sent to clients (outgoing from the server).
///
/// When the server sends a request to the client, it registers the request ID
/// and receives a oneshot receiver. When the client's response arrives, the
/// server completes the request, sending the response to the waiting receiver.
///
/// Responses are always [`Response`](crate::Response) — the standard JSON-RPC response type.
///
/// # Example
///
/// ```
/// use lsp_server_tokio::{OutgoingRequests, RequestId, Response};
/// use serde_json::json;
///
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// let mut outgoing = OutgoingRequests::new();
///
/// // Register an outgoing request
/// let rx = outgoing.register(1.into());
/// assert!(outgoing.is_pending(&1.into()));
///
/// // Simulate receiving a response
/// let response = Response::ok(1, json!({"result": "success"}));
/// let completed = outgoing.complete(&1.into(), response);
/// assert!(completed);
///
/// // Receiver gets the response
/// let result = rx.await.unwrap();
/// assert_eq!(result.id, Some(1.into()));
/// # });
/// ```
#[derive(Debug)]
pub struct OutgoingRequests {
    pending: HashMap<RequestId, oneshot::Sender<Response>>,
}

impl OutgoingRequests {
    /// Creates a new empty outgoing request tracker.
    #[must_use]
    pub fn new() -> Self {
        Self {
            pending: HashMap::new(),
        }
    }

    /// Registers an outgoing request and returns a receiver for the response.
    ///
    /// The returned receiver will receive the response value when [`complete`](Self::complete)
    /// is called with a matching ID. If the request is cancelled via [`cancel`](Self::cancel),
    /// the receiver will return a `RecvError`.
    pub fn register(&mut self, id: RequestId) -> oneshot::Receiver<Response> {
        let (tx, rx) = oneshot::channel();
        self.pending.insert(id, tx);
        rx
    }

    /// Completes an outgoing request by sending the response to the waiting receiver.
    ///
    /// Returns `true` if the request was pending and the response was sent,
    /// `false` if the request was not found.
    ///
    /// Note: This returns `true` even if the receiver was dropped (the response is
    /// still considered "completed" from the queue's perspective).
    pub fn complete(&mut self, id: &RequestId, response: Response) -> bool {
        if let Some(tx) = self.pending.remove(id) {
            // Ignore send error - receiver may have been dropped
            let _ = tx.send(response);
            true
        } else {
            false
        }
    }

    /// Cancels an outgoing request without sending a response.
    ///
    /// The sender is dropped, causing the receiver to return `RecvError`.
    ///
    /// Returns `true` if the request was pending, `false` otherwise.
    pub fn cancel(&mut self, id: &RequestId) -> bool {
        self.pending.remove(id).is_some()
    }

    /// Returns `true` if the request is currently pending.
    #[must_use]
    pub fn is_pending(&self, id: &RequestId) -> bool {
        self.pending.contains_key(id)
    }

    /// Returns the number of currently pending requests.
    #[must_use]
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }
}

impl Default for OutgoingRequests {
    fn default() -> Self {
        Self::new()
    }
}

/// Combined request queue tracking both incoming and outgoing requests.
///
/// This is the primary type for managing LSP request-response correlation.
/// It provides separate tracking for:
///
/// - `incoming`: Requests received from clients that need responses
/// - `outgoing`: Requests sent to clients that are awaiting responses
///
/// # Type Parameters
///
/// - `I`: Metadata type for incoming requests (e.g., handler context)
///
/// # Example
///
/// ```
/// use lsp_server_tokio::{RequestQueue, RequestId};
/// use tokio_util::sync::CancellationToken;
///
/// // Create a queue for a server that tracks method names for incoming
/// // requests and expects JSON responses for outgoing requests
/// let mut queue: RequestQueue<String> = RequestQueue::new();
///
/// // Track incoming request with cancellation token
/// let token = CancellationToken::new();
/// queue.incoming.register(1.into(), "textDocument/hover".to_string(), token);
///
/// // Operations on incoming don't affect outgoing
/// assert_eq!(queue.incoming.pending_count(), 1);
/// assert_eq!(queue.outgoing.pending_count(), 0);
/// ```
#[derive(Debug)]
pub struct RequestQueue<I> {
    /// Tracker for requests received from clients.
    pub incoming: IncomingRequests<I>,
    /// Tracker for requests sent to clients.
    pub outgoing: OutgoingRequests,
}

impl<I> RequestQueue<I> {
    /// Creates a new empty request queue.
    #[must_use]
    pub fn new() -> Self {
        Self {
            incoming: IncomingRequests::new(),
            outgoing: OutgoingRequests::new(),
        }
    }
}

impl<I> Default for RequestQueue<I> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio_util::sync::CancellationToken;

    // ============== IncomingRequests Tests ==============

    #[test]
    fn incoming_register_and_complete() {
        let mut incoming: IncomingRequests<String> = IncomingRequests::new();
        let token = CancellationToken::new();

        incoming.register(1.into(), "metadata".to_string(), token);
        let data = incoming.complete(&1.into());

        assert_eq!(data, Some("metadata".to_string()));
        assert!(!incoming.is_pending(&1.into()));
    }

    #[test]
    fn incoming_complete_unknown_returns_none() {
        let mut incoming: IncomingRequests<String> = IncomingRequests::new();

        let data = incoming.complete(&999.into());
        assert_eq!(data, None);
    }

    #[test]
    fn incoming_is_pending() {
        let mut incoming: IncomingRequests<()> = IncomingRequests::new();

        assert!(!incoming.is_pending(&1.into()));

        let token = CancellationToken::new();
        incoming.register(1.into(), (), token);
        assert!(incoming.is_pending(&1.into()));

        incoming.complete(&1.into());
        assert!(!incoming.is_pending(&1.into()));
    }

    #[test]
    fn incoming_pending_count() {
        let mut incoming: IncomingRequests<i32> = IncomingRequests::new();

        assert_eq!(incoming.pending_count(), 0);

        let token1 = CancellationToken::new();
        incoming.register(1.into(), 100, token1);
        assert_eq!(incoming.pending_count(), 1);

        let token2 = CancellationToken::new();
        incoming.register(2.into(), 200, token2);
        assert_eq!(incoming.pending_count(), 2);

        incoming.complete(&1.into());
        assert_eq!(incoming.pending_count(), 1);

        incoming.complete(&2.into());
        assert_eq!(incoming.pending_count(), 0);
    }

    #[test]
    fn incoming_default() {
        let incoming: IncomingRequests<()> = IncomingRequests::default();
        assert_eq!(incoming.pending_count(), 0);
    }

    #[test]
    fn incoming_cancel_triggers_token() {
        let mut incoming: IncomingRequests<String> = IncomingRequests::new();
        let token = CancellationToken::new();
        let token_clone = token.clone();

        incoming.register(1.into(), "data".to_string(), token);

        // Cancel the request
        assert!(incoming.cancel(&1.into()));

        // Token should be cancelled
        assert!(token_clone.is_cancelled());
    }

    #[test]
    fn incoming_cancel_unknown_returns_false() {
        let incoming: IncomingRequests<()> = IncomingRequests::new();
        assert!(!incoming.cancel(&999.into()));
    }

    #[test]
    fn incoming_cancel_idempotent() {
        let mut incoming: IncomingRequests<()> = IncomingRequests::new();
        let token = CancellationToken::new();

        incoming.register(1.into(), (), token);

        // Cancel twice - both should succeed
        assert!(incoming.cancel(&1.into()));
        assert!(incoming.cancel(&1.into())); // Still returns true, request still pending
    }

    #[test]
    fn incoming_get_token_returns_clone() {
        let mut incoming: IncomingRequests<String> = IncomingRequests::new();
        let original_token = CancellationToken::new();

        incoming.register(1.into(), "data".to_string(), original_token.clone());

        // Get the token
        let retrieved = incoming.get_token(&1.into());
        assert!(retrieved.is_some());

        // Cancel via retrieved token
        retrieved.unwrap().cancel();

        // Original should also be cancelled (they're the same underlying token)
        assert!(original_token.is_cancelled());
    }

    #[test]
    fn incoming_get_token_unknown_returns_none() {
        let incoming: IncomingRequests<()> = IncomingRequests::new();
        assert!(incoming.get_token(&999.into()).is_none());
    }

    #[test]
    fn incoming_complete_after_cancel_returns_data() {
        let mut incoming: IncomingRequests<String> = IncomingRequests::new();
        let token = CancellationToken::new();

        incoming.register(1.into(), "cancelled_data".to_string(), token);

        // Cancel first
        let _ = incoming.cancel(&1.into());

        // Complete should still return the data
        let data = incoming.complete(&1.into());
        assert_eq!(data, Some("cancelled_data".to_string()));
    }

    // ============== OutgoingRequests Tests ==============

    #[tokio::test]
    async fn outgoing_register_and_complete() {
        let mut outgoing = OutgoingRequests::new();
        let rx = outgoing.register(1.into());

        let response = Response::ok(1, serde_json::json!("response"));
        assert!(outgoing.complete(&1.into(), response.clone()));
        assert_eq!(rx.await.unwrap().id, response.id);
    }

    #[test]
    fn outgoing_complete_unknown_returns_false() {
        let mut outgoing = OutgoingRequests::new();

        let result = outgoing.complete(&999.into(), Response::ok(999, serde_json::json!(null)));
        assert!(!result);
    }

    #[tokio::test]
    async fn outgoing_cancel_drops_sender() {
        let mut outgoing = OutgoingRequests::new();
        let rx = outgoing.register(1.into());

        assert!(outgoing.cancel(&1.into()));
        assert!(!outgoing.is_pending(&1.into()));

        // Receiver should get an error since sender was dropped
        assert!(rx.await.is_err());
    }

    #[test]
    fn outgoing_cancel_unknown_returns_false() {
        let mut outgoing = OutgoingRequests::new();

        assert!(!outgoing.cancel(&999.into()));
    }

    #[test]
    fn outgoing_is_pending() {
        let mut outgoing = OutgoingRequests::new();

        assert!(!outgoing.is_pending(&1.into()));

        let _rx = outgoing.register(1.into());
        assert!(outgoing.is_pending(&1.into()));

        outgoing.complete(&1.into(), Response::ok(1, serde_json::json!(null)));
        assert!(!outgoing.is_pending(&1.into()));
    }

    #[test]
    fn outgoing_pending_count() {
        let mut outgoing = OutgoingRequests::new();

        assert_eq!(outgoing.pending_count(), 0);

        let _rx1 = outgoing.register(1.into());
        assert_eq!(outgoing.pending_count(), 1);

        let _rx2 = outgoing.register(2.into());
        assert_eq!(outgoing.pending_count(), 2);

        outgoing.complete(&1.into(), Response::ok(1, serde_json::json!(null)));
        assert_eq!(outgoing.pending_count(), 1);

        outgoing.cancel(&2.into());
        assert_eq!(outgoing.pending_count(), 0);
    }

    #[test]
    fn outgoing_default() {
        let outgoing = OutgoingRequests::default();
        assert_eq!(outgoing.pending_count(), 0);
    }

    // ============== RequestQueue Tests ==============

    #[test]
    fn queue_new_creates_empty() {
        let queue: RequestQueue<()> = RequestQueue::new();

        assert_eq!(queue.incoming.pending_count(), 0);
        assert_eq!(queue.outgoing.pending_count(), 0);
    }

    #[test]
    fn queue_incoming_outgoing_independent() {
        let mut queue: RequestQueue<String> = RequestQueue::new();

        // Register on incoming
        let token = CancellationToken::new();
        queue
            .incoming
            .register(1.into(), "incoming".to_string(), token);
        assert_eq!(queue.incoming.pending_count(), 1);
        assert_eq!(queue.outgoing.pending_count(), 0);

        // Register on outgoing
        let _rx = queue.outgoing.register(2.into());
        assert_eq!(queue.incoming.pending_count(), 1);
        assert_eq!(queue.outgoing.pending_count(), 1);

        // Complete incoming doesn't affect outgoing
        queue.incoming.complete(&1.into());
        assert_eq!(queue.incoming.pending_count(), 0);
        assert_eq!(queue.outgoing.pending_count(), 1);
    }

    #[test]
    fn queue_default() {
        let queue: RequestQueue<()> = RequestQueue::default();
        assert_eq!(queue.incoming.pending_count(), 0);
        assert_eq!(queue.outgoing.pending_count(), 0);
    }

    #[test]
    fn queue_with_string_request_id() {
        let mut queue: RequestQueue<i32> = RequestQueue::new();

        let str_id: RequestId = "abc-123".into();
        let token = CancellationToken::new();
        queue.incoming.register(str_id.clone(), 42, token);

        assert!(queue.incoming.is_pending(&str_id));
        assert_eq!(queue.incoming.complete(&str_id), Some(42));
    }

    // ============== parse_cancel_params Tests ==============

    use super::parse_cancel_params;

    #[test]
    fn parse_cancel_params_integer_id() {
        let params = Some(serde_json::json!({"id": 42}));
        let id = parse_cancel_params(&params);
        assert_eq!(id, Some(RequestId::Integer(42)));
    }

    #[test]
    fn parse_cancel_params_string_id() {
        let params = Some(serde_json::json!({"id": "request-123"}));
        let id = parse_cancel_params(&params);
        assert_eq!(id, Some(RequestId::String("request-123".to_string())));
    }

    #[test]
    fn parse_cancel_params_missing_params() {
        let id = parse_cancel_params(&None);
        assert!(id.is_none());
    }

    #[test]
    fn parse_cancel_params_missing_id_field() {
        let params = Some(serde_json::json!({"other": "field"}));
        let id = parse_cancel_params(&params);
        assert!(id.is_none());
    }

    #[test]
    fn parse_cancel_params_invalid_id_type() {
        let params = Some(serde_json::json!({"id": true}));
        let id = parse_cancel_params(&params);
        assert!(id.is_none());
    }

    #[test]
    fn parse_cancel_params_null_id() {
        let params = Some(serde_json::json!({"id": null}));
        let id = parse_cancel_params(&params);
        assert!(id.is_none());
    }
}