diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-15 10:45:55 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-15 10:45:55 +0100 |
commit | 257a15b93942a1e38e561576af471a6a1e63bcff (patch) | |
tree | 3586eda54dab85be6d24325e597a1d8f060e36b7 /crates/gen_lsp_server | |
parent | 9dbf985df5515f4b9b40a7dcf74f916fa8d57ee3 (diff) | |
parent | 408e173bb9737f9484ca773ee57cc791f5c57e16 (diff) |
Merge #1404
1404: Fight down failures! r=matklad a=mominul
issue #1400
Now only `ra_tools` crate depends on `failure`, should I also fight those? :grin:
Co-authored-by: Muhammad Mominul Huque <[email protected]>
Diffstat (limited to 'crates/gen_lsp_server')
-rw-r--r-- | crates/gen_lsp_server/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/gen_lsp_server/examples/01_gen_lsp_server.rs | 6 | ||||
-rw-r--r-- | crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs | 6 | ||||
-rw-r--r-- | crates/gen_lsp_server/src/lib.rs | 22 | ||||
-rw-r--r-- | crates/gen_lsp_server/src/msg.rs | 8 | ||||
-rw-r--r-- | crates/gen_lsp_server/src/stdio.rs | 5 |
6 files changed, 24 insertions, 24 deletions
diff --git a/crates/gen_lsp_server/Cargo.toml b/crates/gen_lsp_server/Cargo.toml index fa2fefea5..d375606d0 100644 --- a/crates/gen_lsp_server/Cargo.toml +++ b/crates/gen_lsp_server/Cargo.toml | |||
@@ -10,7 +10,6 @@ description = "Generic LSP server scaffold." | |||
10 | [dependencies] | 10 | [dependencies] |
11 | lsp-types = "0.57.0" | 11 | lsp-types = "0.57.0" |
12 | log = "0.4.3" | 12 | log = "0.4.3" |
13 | failure = "0.1.4" | ||
14 | serde_json = "1.0.34" | 13 | serde_json = "1.0.34" |
15 | serde = { version = "1.0.83", features = ["derive"] } | 14 | serde = { version = "1.0.83", features = ["derive"] } |
16 | crossbeam-channel = "0.3.5" | 15 | crossbeam-channel = "0.3.5" |
diff --git a/crates/gen_lsp_server/examples/01_gen_lsp_server.rs b/crates/gen_lsp_server/examples/01_gen_lsp_server.rs index 60c581075..bc497f74a 100644 --- a/crates/gen_lsp_server/examples/01_gen_lsp_server.rs +++ b/crates/gen_lsp_server/examples/01_gen_lsp_server.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::error::Error; | ||
2 | |||
1 | use crossbeam_channel::{Sender, Receiver}; | 3 | use crossbeam_channel::{Sender, Receiver}; |
2 | use lsp_types::{ | 4 | use lsp_types::{ |
3 | ServerCapabilities, InitializeParams, | 5 | ServerCapabilities, InitializeParams, |
@@ -5,7 +7,7 @@ use lsp_types::{ | |||
5 | }; | 7 | }; |
6 | use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse}; | 8 | use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse}; |
7 | 9 | ||
8 | fn main() -> Result<(), failure::Error> { | 10 | fn main() -> Result<(), Box<dyn Error + Sync + Send>> { |
9 | let (receiver, sender, io_threads) = stdio_transport(); | 11 | let (receiver, sender, io_threads) = stdio_transport(); |
10 | run_server(ServerCapabilities::default(), receiver, sender, main_loop)?; | 12 | run_server(ServerCapabilities::default(), receiver, sender, main_loop)?; |
11 | io_threads.join()?; | 13 | io_threads.join()?; |
@@ -16,7 +18,7 @@ fn main_loop( | |||
16 | _params: InitializeParams, | 18 | _params: InitializeParams, |
17 | receiver: &Receiver<RawMessage>, | 19 | receiver: &Receiver<RawMessage>, |
18 | sender: &Sender<RawMessage>, | 20 | sender: &Sender<RawMessage>, |
19 | ) -> Result<(), failure::Error> { | 21 | ) -> Result<(), Box<dyn Error + Sync + Send>> { |
20 | for msg in receiver { | 22 | for msg in receiver { |
21 | match msg { | 23 | match msg { |
22 | RawMessage::Request(req) => { | 24 | RawMessage::Request(req) => { |
diff --git a/crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs b/crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs index 27e4f1cbc..1a6174462 100644 --- a/crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs +++ b/crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs | |||
@@ -42,6 +42,8 @@ | |||
42 | //! {"jsonrpc": "2.0", "method": "exit", "params": null} | 42 | //! {"jsonrpc": "2.0", "method": "exit", "params": null} |
43 | //! ``` | 43 | //! ``` |
44 | 44 | ||
45 | use std::error::Error; | ||
46 | |||
45 | use crossbeam_channel::{Sender, Receiver}; | 47 | use crossbeam_channel::{Sender, Receiver}; |
46 | use lsp_types::{ | 48 | use lsp_types::{ |
47 | ServerCapabilities, InitializeParams, | 49 | ServerCapabilities, InitializeParams, |
@@ -52,7 +54,7 @@ use gen_lsp_server::{ | |||
52 | run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse, RawRequest, | 54 | run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse, RawRequest, |
53 | }; | 55 | }; |
54 | 56 | ||
55 | fn main() -> Result<(), failure::Error> { | 57 | fn main() -> Result<(), Box<dyn Error + Sync + Send>> { |
56 | // Set up logging. Because `stdio_transport` gets a lock on stdout and stdin, we must have | 58 | // Set up logging. Because `stdio_transport` gets a lock on stdout and stdin, we must have |
57 | // our logging only write out to stderr. | 59 | // our logging only write out to stderr. |
58 | flexi_logger::Logger::with_str("info").start().unwrap(); | 60 | flexi_logger::Logger::with_str("info").start().unwrap(); |
@@ -75,7 +77,7 @@ fn main_loop( | |||
75 | _params: InitializeParams, | 77 | _params: InitializeParams, |
76 | receiver: &Receiver<RawMessage>, | 78 | receiver: &Receiver<RawMessage>, |
77 | sender: &Sender<RawMessage>, | 79 | sender: &Sender<RawMessage>, |
78 | ) -> Result<(), failure::Error> { | 80 | ) -> Result<(), Box<dyn Error + Sync + Send>> { |
79 | info!("starting example main loop"); | 81 | info!("starting example main loop"); |
80 | for msg in receiver { | 82 | for msg in receiver { |
81 | info!("got msg: {:?}", msg); | 83 | info!("got msg: {:?}", msg); |
diff --git a/crates/gen_lsp_server/src/lib.rs b/crates/gen_lsp_server/src/lib.rs index 1cd5a3a7c..7ecef83cb 100644 --- a/crates/gen_lsp_server/src/lib.rs +++ b/crates/gen_lsp_server/src/lib.rs | |||
@@ -5,11 +5,12 @@ | |||
5 | //! Run with `RUST_LOG=gen_lsp_server=debug` to see all the messages. | 5 | //! Run with `RUST_LOG=gen_lsp_server=debug` to see all the messages. |
6 | //! | 6 | //! |
7 | //! ```no_run | 7 | //! ```no_run |
8 | //! use std::error::Error; | ||
8 | //! use crossbeam_channel::{Sender, Receiver}; | 9 | //! use crossbeam_channel::{Sender, Receiver}; |
9 | //! use lsp_types::{ServerCapabilities, InitializeParams, request::{GotoDefinition, GotoDefinitionResponse}}; | 10 | //! use lsp_types::{ServerCapabilities, InitializeParams, request::{GotoDefinition, GotoDefinitionResponse}}; |
10 | //! use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse}; | 11 | //! use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse}; |
11 | //! | 12 | //! |
12 | //! fn main() -> Result<(), failure::Error> { | 13 | //! fn main() -> Result<(), Box<dyn Error + Send + Sync>> { |
13 | //! let (receiver, sender, io_threads) = stdio_transport(); | 14 | //! let (receiver, sender, io_threads) = stdio_transport(); |
14 | //! run_server( | 15 | //! run_server( |
15 | //! ServerCapabilities::default(), | 16 | //! ServerCapabilities::default(), |
@@ -25,7 +26,7 @@ | |||
25 | //! _params: InitializeParams, | 26 | //! _params: InitializeParams, |
26 | //! receiver: &Receiver<RawMessage>, | 27 | //! receiver: &Receiver<RawMessage>, |
27 | //! sender: &Sender<RawMessage>, | 28 | //! sender: &Sender<RawMessage>, |
28 | //! ) -> Result<(), failure::Error> { | 29 | //! ) -> Result<(), Box<dyn Error + Send + Sync>> { |
29 | //! for msg in receiver { | 30 | //! for msg in receiver { |
30 | //! match msg { | 31 | //! match msg { |
31 | //! RawMessage::Request(req) => { | 32 | //! RawMessage::Request(req) => { |
@@ -54,7 +55,7 @@ | |||
54 | //! } | 55 | //! } |
55 | //! ``` | 56 | //! ``` |
56 | 57 | ||
57 | use failure::{bail, format_err}; | 58 | use std::error::Error; |
58 | 59 | ||
59 | mod msg; | 60 | mod msg; |
60 | mod stdio; | 61 | mod stdio; |
@@ -66,7 +67,7 @@ use lsp_types::{ | |||
66 | InitializeParams, InitializeResult, ServerCapabilities, | 67 | InitializeParams, InitializeResult, ServerCapabilities, |
67 | }; | 68 | }; |
68 | 69 | ||
69 | pub type Result<T> = ::std::result::Result<T, failure::Error>; | 70 | pub type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>; |
70 | pub use crate::{ | 71 | pub use crate::{ |
71 | msg::{ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, RawResponseError}, | 72 | msg::{ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, RawResponseError}, |
72 | stdio::{stdio_transport, Threads}, | 73 | stdio::{stdio_transport, Threads}, |
@@ -92,8 +93,8 @@ pub fn run_server( | |||
92 | match receiver.recv() { | 93 | match receiver.recv() { |
93 | Ok(RawMessage::Notification(n)) => n | 94 | Ok(RawMessage::Notification(n)) => n |
94 | .cast::<Exit>() | 95 | .cast::<Exit>() |
95 | .map_err(|n| format_err!("unexpected notification during shutdown: {:?}", n))?, | 96 | .map_err(|n| format!("unexpected notification during shutdown: {:?}", n))?, |
96 | m => bail!("unexpected message during shutdown: {:?}", m), | 97 | m => Err(format!("unexpected message during shutdown: {:?}", m))?, |
97 | } | 98 | } |
98 | log::info!("lsp server shutdown complete"); | 99 | log::info!("lsp server shutdown complete"); |
99 | Ok(()) | 100 | Ok(()) |
@@ -118,19 +119,18 @@ fn initialize( | |||
118 | ) -> Result<InitializeParams> { | 119 | ) -> Result<InitializeParams> { |
119 | let (id, params) = match receiver.recv() { | 120 | let (id, params) = match receiver.recv() { |
120 | Ok(RawMessage::Request(req)) => match req.cast::<Initialize>() { | 121 | Ok(RawMessage::Request(req)) => match req.cast::<Initialize>() { |
121 | Err(req) => bail!("expected initialize request, got {:?}", req), | 122 | Err(req) => Err(format!("expected initialize request, got {:?}", req))?, |
122 | Ok(req) => req, | 123 | Ok(req) => req, |
123 | }, | 124 | }, |
124 | msg => bail!("expected initialize request, got {:?}", msg), | 125 | msg => Err(format!("expected initialize request, got {:?}", msg))?, |
125 | }; | 126 | }; |
126 | let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps }); | 127 | let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps }); |
127 | sender.send(RawMessage::Response(resp)).unwrap(); | 128 | sender.send(RawMessage::Response(resp)).unwrap(); |
128 | match receiver.recv() { | 129 | match receiver.recv() { |
129 | Ok(RawMessage::Notification(n)) => { | 130 | Ok(RawMessage::Notification(n)) => { |
130 | n.cast::<Initialized>() | 131 | n.cast::<Initialized>().map_err(|_| "expected initialized notification")?; |
131 | .map_err(|_| format_err!("expected initialized notification"))?; | ||
132 | } | 132 | } |
133 | _ => bail!("expected initialized notification"), | 133 | _ => Err(format!("expected initialized notification"))?, |
134 | } | 134 | } |
135 | Ok(params) | 135 | Ok(params) |
136 | } | 136 | } |
diff --git a/crates/gen_lsp_server/src/msg.rs b/crates/gen_lsp_server/src/msg.rs index 1d39ba4bc..2928e4f8b 100644 --- a/crates/gen_lsp_server/src/msg.rs +++ b/crates/gen_lsp_server/src/msg.rs | |||
@@ -3,7 +3,6 @@ use std::io::{BufRead, Write}; | |||
3 | use lsp_types::{notification::Notification, request::Request}; | 3 | use lsp_types::{notification::Notification, request::Request}; |
4 | use serde::{Deserialize, Serialize}; | 4 | use serde::{Deserialize, Serialize}; |
5 | use serde_json::{from_str, from_value, to_string, to_value, Value}; | 5 | use serde_json::{from_str, from_value, to_string, to_value, Value}; |
6 | use failure::{bail, format_err}; | ||
7 | 6 | ||
8 | use crate::Result; | 7 | use crate::Result; |
9 | 8 | ||
@@ -175,7 +174,7 @@ fn read_msg_text(inp: &mut impl BufRead) -> Result<Option<String>> { | |||
175 | return Ok(None); | 174 | return Ok(None); |
176 | } | 175 | } |
177 | if !buf.ends_with("\r\n") { | 176 | if !buf.ends_with("\r\n") { |
178 | bail!("malformed header: {:?}", buf); | 177 | Err(format!("malformed header: {:?}", buf))?; |
179 | } | 178 | } |
180 | let buf = &buf[..buf.len() - 2]; | 179 | let buf = &buf[..buf.len() - 2]; |
181 | if buf.is_empty() { | 180 | if buf.is_empty() { |
@@ -183,13 +182,12 @@ fn read_msg_text(inp: &mut impl BufRead) -> Result<Option<String>> { | |||
183 | } | 182 | } |
184 | let mut parts = buf.splitn(2, ": "); | 183 | let mut parts = buf.splitn(2, ": "); |
185 | let header_name = parts.next().unwrap(); | 184 | let header_name = parts.next().unwrap(); |
186 | let header_value = | 185 | let header_value = parts.next().ok_or_else(|| format!("malformed header: {:?}", buf))?; |
187 | parts.next().ok_or_else(|| format_err!("malformed header: {:?}", buf))?; | ||
188 | if header_name == "Content-Length" { | 186 | if header_name == "Content-Length" { |
189 | size = Some(header_value.parse::<usize>()?); | 187 | size = Some(header_value.parse::<usize>()?); |
190 | } | 188 | } |
191 | } | 189 | } |
192 | let size = size.ok_or_else(|| format_err!("no Content-Length"))?; | 190 | let size = size.ok_or("no Content-Length")?; |
193 | let mut buf = buf.into_bytes(); | 191 | let mut buf = buf.into_bytes(); |
194 | buf.resize(size, 0); | 192 | buf.resize(size, 0); |
195 | inp.read_exact(&mut buf)?; | 193 | inp.read_exact(&mut buf)?; |
diff --git a/crates/gen_lsp_server/src/stdio.rs b/crates/gen_lsp_server/src/stdio.rs index 2d6418400..f8931f2dc 100644 --- a/crates/gen_lsp_server/src/stdio.rs +++ b/crates/gen_lsp_server/src/stdio.rs | |||
@@ -4,7 +4,6 @@ use std::{ | |||
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crossbeam_channel::{bounded, Receiver, Sender}; | 6 | use crossbeam_channel::{bounded, Receiver, Sender}; |
7 | use failure::bail; | ||
8 | use lsp_types::notification::Exit; | 7 | use lsp_types::notification::Exit; |
9 | 8 | ||
10 | use crate::{RawMessage, Result}; | 9 | use crate::{RawMessage, Result}; |
@@ -48,11 +47,11 @@ impl Threads { | |||
48 | pub fn join(self) -> Result<()> { | 47 | pub fn join(self) -> Result<()> { |
49 | match self.reader.join() { | 48 | match self.reader.join() { |
50 | Ok(r) => r?, | 49 | Ok(r) => r?, |
51 | Err(_) => bail!("reader panicked"), | 50 | Err(_) => Err("reader panicked")?, |
52 | } | 51 | } |
53 | match self.writer.join() { | 52 | match self.writer.join() { |
54 | Ok(r) => r, | 53 | Ok(r) => r, |
55 | Err(_) => bail!("writer panicked"), | 54 | Err(_) => Err("writer panicked")?, |
56 | } | 55 | } |
57 | } | 56 | } |
58 | } | 57 | } |