forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallback.rs
More file actions
80 lines (71 loc) · 2.33 KB
/
Copy pathfallback.rs
File metadata and controls
80 lines (71 loc) · 2.33 KB
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
use std::future::Future;
use std::ops::ControlFlow;
use std::pin::Pin;
use std::task::{Context, Poll};
use async_lsp::{AnyEvent, AnyNotification, AnyRequest, LspService, ResponseError};
use serde_json::Value;
use tower::Service;
use crate::lsp_actor::service::CanHandle;
pub struct WithFallbackService<A, B> {
primary: A,
fallback: B,
}
impl<A, B> WithFallbackService<A, B> {
pub fn new(primary: A, fallback: B) -> Self {
Self { primary, fallback }
}
}
impl<A, B, F> Service<AnyRequest> for WithFallbackService<A, B>
where
A: Service<AnyRequest, Response = Value, Error = ResponseError, Future = F>
+ CanHandle<AnyRequest>,
B: Service<AnyRequest, Response = Value, Error = ResponseError, Future = F>,
F: Future<Output = Result<Value, ResponseError>> + Send + 'static,
{
type Response = serde_json::Value;
type Error = ResponseError;
type Future = F;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.primary.poll_ready(cx) {
Poll::Ready(Ok(())) => self.fallback.poll_ready(cx),
other => other,
}
}
fn call(&mut self, req: AnyRequest) -> Self::Future {
if self.primary.can_handle(&req) {
self.primary.call(req)
} else {
self.fallback.call(req)
}
}
}
impl<A, B> LspService for WithFallbackService<A, B>
where
A: LspService<
Response = Value,
Error = ResponseError,
Future = Pin<Box<dyn Future<Output = Result<Value, ResponseError>> + Send + 'static>>,
> + CanHandle<AnyRequest>
+ CanHandle<AnyNotification>
+ CanHandle<AnyEvent>,
B: LspService<
Response = Value,
Error = ResponseError,
Future = Pin<Box<dyn Future<Output = Result<Value, ResponseError>> + Send + 'static>>,
>,
{
fn notify(&mut self, notif: AnyNotification) -> ControlFlow<async_lsp::Result<()>> {
if self.primary.can_handle(¬if) {
self.primary.notify(notif)
} else {
self.fallback.notify(notif)
}
}
fn emit(&mut self, event: AnyEvent) -> ControlFlow<async_lsp::Result<()>> {
if self.primary.can_handle(&event) {
self.primary.emit(event)
} else {
self.fallback.emit(event)
}
}
}