diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 21:56:19 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 21:56:19 +0100 |
commit | 52de08330f5052a1f10c5d46e1352ca86b622d71 (patch) | |
tree | 9f1adb749b2e4e344192e7060fcde8c748feede1 | |
parent | bf42a75f1e9a86247067611fadbed48ea9d379b0 (diff) |
dedupe
-rw-r--r-- | crates/server/src/dispatch.rs | 27 | ||||
-rw-r--r-- | crates/server/src/main.rs | 8 | ||||
-rw-r--r-- | crates/server/src/req.rs | 21 |
3 files changed, 29 insertions, 27 deletions
diff --git a/crates/server/src/dispatch.rs b/crates/server/src/dispatch.rs index d9681db40..6a609f10e 100644 --- a/crates/server/src/dispatch.rs +++ b/crates/server/src/dispatch.rs | |||
@@ -9,20 +9,17 @@ use drop_bomb::DropBomb; | |||
9 | 9 | ||
10 | use ::{ | 10 | use ::{ |
11 | Result, | 11 | Result, |
12 | req::{Request, Notification}, | 12 | req::{ClientRequest, Notification}, |
13 | io::{Io, RawMsg, RawResponse, RawRequest, RawNotification}, | 13 | io::{Io, RawMsg, RawResponse, RawRequest, RawNotification}, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | pub struct Responder<R: Request> { | 16 | pub struct Responder<R: ClientRequest> { |
17 | id: u64, | 17 | id: u64, |
18 | bomb: DropBomb, | 18 | bomb: DropBomb, |
19 | ph: PhantomData<R>, | 19 | ph: PhantomData<R>, |
20 | } | 20 | } |
21 | 21 | ||
22 | impl<R: Request> Responder<R> | 22 | impl<R: ClientRequest> Responder<R> |
23 | where | ||
24 | R::Params: DeserializeOwned, | ||
25 | R::Result: Serialize, | ||
26 | { | 23 | { |
27 | pub fn response(self, io: &mut Io, resp: Result<R::Result>) -> Result<()> { | 24 | pub fn response(self, io: &mut Io, resp: Result<R::Result>) -> Result<()> { |
28 | match resp { | 25 | match resp { |
@@ -52,11 +49,8 @@ impl<R: Request> Responder<R> | |||
52 | } | 49 | } |
53 | 50 | ||
54 | 51 | ||
55 | fn parse_request_as<R>(raw: RawRequest) -> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>> | 52 | fn parse_request_as<R: ClientRequest>(raw: RawRequest) |
56 | where | 53 | -> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>> |
57 | R: Request, | ||
58 | R::Params: DeserializeOwned, | ||
59 | R::Result: Serialize, | ||
60 | { | 54 | { |
61 | if raw.method != R::METHOD { | 55 | if raw.method != R::METHOD { |
62 | return Ok(Err(raw)); | 56 | return Ok(Err(raw)); |
@@ -73,9 +67,7 @@ fn parse_request_as<R>(raw: RawRequest) -> Result<::std::result::Result<(R::Para | |||
73 | 67 | ||
74 | pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()> | 68 | pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()> |
75 | where | 69 | where |
76 | R: Request, | 70 | R: ClientRequest, |
77 | R::Params: DeserializeOwned, | ||
78 | R::Result: Serialize, | ||
79 | F: FnOnce(R::Params, Responder<R>) -> Result<()> | 71 | F: FnOnce(R::Params, Responder<R>) -> Result<()> |
80 | { | 72 | { |
81 | match req.take() { | 73 | match req.take() { |
@@ -90,11 +82,8 @@ pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()> | |||
90 | } | 82 | } |
91 | } | 83 | } |
92 | 84 | ||
93 | pub fn expect_request<R>(io: &mut Io, raw: RawRequest) -> Result<Option<(R::Params, Responder<R>)>> | 85 | pub fn expect_request<R: ClientRequest>(io: &mut Io, raw: RawRequest) |
94 | where | 86 | -> Result<Option<(R::Params, Responder<R>)>> |
95 | R: Request, | ||
96 | R::Params: DeserializeOwned, | ||
97 | R::Result: Serialize, | ||
98 | { | 87 | { |
99 | let ret = match parse_request_as::<R>(raw)? { | 88 | let ret = match parse_request_as::<R>(raw)? { |
100 | Ok(x) => Some(x), | 89 | Ok(x) => Some(x), |
diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index f521271fb..dfde8afb1 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs | |||
@@ -28,12 +28,10 @@ use threadpool::ThreadPool; | |||
28 | use crossbeam_channel::{bounded, Sender, Receiver}; | 28 | use crossbeam_channel::{bounded, Sender, Receiver}; |
29 | use flexi_logger::Logger; | 29 | use flexi_logger::Logger; |
30 | use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier}; | 30 | use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier}; |
31 | use serde::{ser::Serialize, de::DeserializeOwned}; | ||
32 | use libanalysis::{WorldState, World}; | 31 | use libanalysis::{WorldState, World}; |
33 | 32 | ||
34 | use ::{ | 33 | use ::{ |
35 | io::{Io, RawMsg, RawRequest}, | 34 | io::{Io, RawMsg, RawRequest}, |
36 | req::Request, | ||
37 | handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, | 35 | handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, |
38 | }; | 36 | }; |
39 | 37 | ||
@@ -261,17 +259,13 @@ fn main_loop( | |||
261 | } | 259 | } |
262 | } | 260 | } |
263 | 261 | ||
264 | fn handle_request_on_threadpool<R>( | 262 | fn handle_request_on_threadpool<R: req::ClientRequest>( |
265 | req: &mut Option<RawRequest>, | 263 | req: &mut Option<RawRequest>, |
266 | pool: &ThreadPool, | 264 | pool: &ThreadPool, |
267 | world: &WorldState, | 265 | world: &WorldState, |
268 | sender: &Sender<Thunk>, | 266 | sender: &Sender<Thunk>, |
269 | f: fn(World, R::Params) -> Result<R::Result>, | 267 | f: fn(World, R::Params) -> Result<R::Result>, |
270 | ) -> Result<()> | 268 | ) -> Result<()> |
271 | where | ||
272 | R: Request + Send + 'static, | ||
273 | R::Params: DeserializeOwned + Send + 'static, | ||
274 | R::Result: Serialize + Send + 'static, | ||
275 | { | 269 | { |
276 | dispatch::handle_request::<R, _>(req, |params, resp| { | 270 | dispatch::handle_request::<R, _>(req, |params, resp| { |
277 | let world = world.snapshot(); | 271 | let world = world.snapshot(); |
diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index 64e7ef4ae..10f0b2919 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs | |||
@@ -1,10 +1,29 @@ | |||
1 | use serde::{ser::Serialize, de::DeserializeOwned}; | ||
1 | use languageserver_types::{TextDocumentIdentifier, Range}; | 2 | use languageserver_types::{TextDocumentIdentifier, Range}; |
2 | 3 | ||
3 | pub use languageserver_types::{ | 4 | pub use languageserver_types::{ |
4 | request::*, notification::*, | 5 | request::*, notification::*, |
5 | InitializeResult, PublishDiagnosticsParams | 6 | InitializeResult, PublishDiagnosticsParams, |
6 | }; | 7 | }; |
7 | 8 | ||
9 | |||
10 | pub trait ClientRequest: Send + 'static { | ||
11 | type Params: DeserializeOwned + Send + 'static; | ||
12 | type Result: Serialize + Send + 'static; | ||
13 | const METHOD: &'static str; | ||
14 | } | ||
15 | |||
16 | impl<T> ClientRequest for T | ||
17 | where T: Request + Send + 'static, | ||
18 | T::Params: DeserializeOwned + Send + 'static, | ||
19 | T::Result: Serialize + Send + 'static, | ||
20 | { | ||
21 | type Params = <T as Request>::Params; | ||
22 | type Result = <T as Request>::Result; | ||
23 | const METHOD: &'static str = <T as Request>::METHOD; | ||
24 | } | ||
25 | |||
26 | |||
8 | pub enum SyntaxTree {} | 27 | pub enum SyntaxTree {} |
9 | 28 | ||
10 | impl Request for SyntaxTree { | 29 | impl Request for SyntaxTree { |