aboutsummaryrefslogtreecommitdiff
path: root/crates/gen_lsp_server
diff options
context:
space:
mode:
Diffstat (limited to 'crates/gen_lsp_server')
-rw-r--r--crates/gen_lsp_server/Cargo.toml1
-rw-r--r--crates/gen_lsp_server/examples/01_gen_lsp_server.rs6
-rw-r--r--crates/gen_lsp_server/examples/02_gen_lsp_server_with_logging.rs6
-rw-r--r--crates/gen_lsp_server/src/lib.rs16
-rw-r--r--crates/gen_lsp_server/src/msg.rs7
-rw-r--r--crates/gen_lsp_server/src/stdio.rs5
6 files changed, 21 insertions, 20 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]
11lsp-types = "0.57.0" 11lsp-types = "0.57.0"
12log = "0.4.3" 12log = "0.4.3"
13failure = "0.1.4"
14serde_json = "1.0.34" 13serde_json = "1.0.34"
15serde = { version = "1.0.83", features = ["derive"] } 14serde = { version = "1.0.83", features = ["derive"] }
16crossbeam-channel = "0.3.5" 15crossbeam-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 @@
1use std::error::Error;
2
1use crossbeam_channel::{Sender, Receiver}; 3use crossbeam_channel::{Sender, Receiver};
2use lsp_types::{ 4use lsp_types::{
3 ServerCapabilities, InitializeParams, 5 ServerCapabilities, InitializeParams,
@@ -5,7 +7,7 @@ use lsp_types::{
5}; 7};
6use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse}; 8use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse};
7 9
8fn main() -> Result<(), failure::Error> { 10fn 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
45use std::error::Error;
46
45use crossbeam_channel::{Sender, Receiver}; 47use crossbeam_channel::{Sender, Receiver};
46use lsp_types::{ 48use 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
55fn main() -> Result<(), failure::Error> { 57fn 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..7643dcacc 100644
--- a/crates/gen_lsp_server/src/lib.rs
+++ b/crates/gen_lsp_server/src/lib.rs
@@ -54,7 +54,7 @@
54//! } 54//! }
55//! ``` 55//! ```
56 56
57use failure::{bail, format_err}; 57use std::error::Error;
58 58
59mod msg; 59mod msg;
60mod stdio; 60mod stdio;
@@ -66,7 +66,7 @@ use lsp_types::{
66 InitializeParams, InitializeResult, ServerCapabilities, 66 InitializeParams, InitializeResult, ServerCapabilities,
67}; 67};
68 68
69pub type Result<T> = ::std::result::Result<T, failure::Error>; 69pub type Result<T> = ::std::result::Result<T, Box<dyn Error + Send + Sync>>;
70pub use crate::{ 70pub use crate::{
71 msg::{ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, RawResponseError}, 71 msg::{ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, RawResponseError},
72 stdio::{stdio_transport, Threads}, 72 stdio::{stdio_transport, Threads},
@@ -92,8 +92,8 @@ pub fn run_server(
92 match receiver.recv() { 92 match receiver.recv() {
93 Ok(RawMessage::Notification(n)) => n 93 Ok(RawMessage::Notification(n)) => n
94 .cast::<Exit>() 94 .cast::<Exit>()
95 .map_err(|n| format_err!("unexpected notification during shutdown: {:?}", n))?, 95 .map_err(|n| format!("unexpected notification during shutdown: {:?}", n))?,
96 m => bail!("unexpected message during shutdown: {:?}", m), 96 m => Err(format!("unexpected message during shutdown: {:?}", m))?,
97 } 97 }
98 log::info!("lsp server shutdown complete"); 98 log::info!("lsp server shutdown complete");
99 Ok(()) 99 Ok(())
@@ -118,19 +118,19 @@ fn initialize(
118) -> Result<InitializeParams> { 118) -> Result<InitializeParams> {
119 let (id, params) = match receiver.recv() { 119 let (id, params) = match receiver.recv() {
120 Ok(RawMessage::Request(req)) => match req.cast::<Initialize>() { 120 Ok(RawMessage::Request(req)) => match req.cast::<Initialize>() {
121 Err(req) => bail!("expected initialize request, got {:?}", req), 121 Err(req) => Err(format!("expected initialize request, got {:?}", req))?,
122 Ok(req) => req, 122 Ok(req) => req,
123 }, 123 },
124 msg => bail!("expected initialize request, got {:?}", msg), 124 msg => Err(format!("expected initialize request, got {:?}", msg))?,
125 }; 125 };
126 let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps }); 126 let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps });
127 sender.send(RawMessage::Response(resp)).unwrap(); 127 sender.send(RawMessage::Response(resp)).unwrap();
128 match receiver.recv() { 128 match receiver.recv() {
129 Ok(RawMessage::Notification(n)) => { 129 Ok(RawMessage::Notification(n)) => {
130 n.cast::<Initialized>() 130 n.cast::<Initialized>()
131 .map_err(|_| format_err!("expected initialized notification"))?; 131 .map_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..8138b84eb 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};
3use lsp_types::{notification::Notification, request::Request}; 3use lsp_types::{notification::Notification, request::Request};
4use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
5use serde_json::{from_str, from_value, to_string, to_value, Value}; 5use serde_json::{from_str, from_value, to_string, to_value, Value};
6use failure::{bail, format_err};
7 6
8use crate::Result; 7use 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() {
@@ -184,12 +183,12 @@ fn read_msg_text(inp: &mut impl BufRead) -> Result<Option<String>> {
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 =
187 parts.next().ok_or_else(|| format_err!("malformed header: {:?}", buf))?; 186 parts.next().ok_or_else(|| format!("malformed header: {:?}", buf))?;
188 if header_name == "Content-Length" { 187 if header_name == "Content-Length" {
189 size = Some(header_value.parse::<usize>()?); 188 size = Some(header_value.parse::<usize>()?);
190 } 189 }
191 } 190 }
192 let size = size.ok_or_else(|| format_err!("no Content-Length"))?; 191 let size = size.ok_or("no Content-Length")?;
193 let mut buf = buf.into_bytes(); 192 let mut buf = buf.into_bytes();
194 buf.resize(size, 0); 193 buf.resize(size, 0);
195 inp.read_exact(&mut buf)?; 194 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
6use crossbeam_channel::{bounded, Receiver, Sender}; 6use crossbeam_channel::{bounded, Receiver, Sender};
7use failure::bail;
8use lsp_types::notification::Exit; 7use lsp_types::notification::Exit;
9 8
10use crate::{RawMessage, Result}; 9use 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}