rust/crates/gen_lsp_server/src/msg.rs

205 lines
5.2 KiB
Rust
Raw Normal View History

2018-09-01 15:18:02 +02:00
use std::io::{BufRead, Write};
use languageserver_types::{notification::Notification, request::Request};
use serde::{de::DeserializeOwned, Serialize};
use serde_json::{from_str, from_value, to_string, to_value, Value};
2018-09-01 15:18:02 +02:00
use Result;
2018-09-02 11:34:06 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2018-09-01 15:18:02 +02:00
#[serde(untagged)]
pub enum RawMessage {
Request(RawRequest),
Notification(RawNotification),
Response(RawResponse),
}
2018-09-02 11:34:06 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2018-09-01 15:18:02 +02:00
pub struct RawRequest {
pub id: u64,
pub method: String,
pub params: Value,
}
2018-09-02 11:34:06 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2018-09-01 15:18:02 +02:00
pub struct RawResponse {
// JSON RPC allows this to be null if it was impossible
// to decode the request's id. Ignore this special case
// and just die horribly.
pub id: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<RawResponseError>,
}
2018-09-02 11:34:06 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2018-09-01 15:18:02 +02:00
pub struct RawResponseError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
2018-09-02 11:34:06 +02:00
#[derive(Clone, Copy, Debug)]
2018-09-01 15:18:02 +02:00
#[allow(unused)]
pub enum ErrorCode {
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603,
ServerErrorStart = -32099,
ServerErrorEnd = -32000,
ServerNotInitialized = -32002,
UnknownErrorCode = -32001,
RequestCancelled = -32800,
}
2018-09-02 11:34:06 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2018-09-01 15:18:02 +02:00
pub struct RawNotification {
pub method: String,
pub params: Value,
}
impl RawMessage {
pub fn read(r: &mut impl BufRead) -> Result<Option<RawMessage>> {
let text = match read_msg_text(r)? {
None => return Ok(None),
Some(text) => text,
};
let msg = from_str(&text)?;
Ok(Some(msg))
}
pub fn write(self, w: &mut impl Write) -> Result<()> {
#[derive(Serialize)]
struct JsonRpc {
jsonrpc: &'static str,
#[serde(flatten)]
msg: RawMessage,
}
let text = to_string(&JsonRpc {
jsonrpc: "2.0",
msg: self,
})?;
2018-09-01 15:18:02 +02:00
write_msg_text(w, &text)?;
Ok(())
}
}
impl RawRequest {
2018-09-02 14:18:43 +02:00
pub fn new<R>(id: u64, params: &R::Params) -> RawRequest
2018-09-01 19:21:11 +02:00
where
R: Request,
R::Params: Serialize,
{
RawRequest {
2018-11-29 21:30:49 +01:00
id,
2018-09-01 19:21:11 +02:00
method: R::METHOD.to_string(),
2018-09-02 14:18:43 +02:00
params: to_value(params).unwrap(),
2018-09-01 19:21:11 +02:00
}
}
2018-09-01 15:18:02 +02:00
pub fn cast<R>(self) -> ::std::result::Result<(u64, R::Params), RawRequest>
where
R: Request,
R::Params: DeserializeOwned,
{
if self.method != R::METHOD {
return Err(self);
}
let id = self.id;
let params: R::Params = from_value(self.params).unwrap();
Ok((id, params))
}
}
impl RawResponse {
2018-09-02 14:18:43 +02:00
pub fn ok<R>(id: u64, result: &R::Result) -> RawResponse
where
R: Request,
R::Result: Serialize,
2018-09-01 19:21:11 +02:00
{
2018-09-01 15:18:02 +02:00
RawResponse {
id,
result: Some(to_value(&result).unwrap()),
error: None,
}
}
pub fn err(id: u64, code: i32, message: String) -> RawResponse {
let error = RawResponseError {
code,
message,
data: None,
};
2018-09-01 15:18:02 +02:00
RawResponse {
id,
result: None,
error: Some(error),
}
}
}
impl RawNotification {
2018-09-02 14:18:43 +02:00
pub fn new<N>(params: &N::Params) -> RawNotification
2018-09-01 16:40:45 +02:00
where
N: Notification,
N::Params: Serialize,
{
RawNotification {
method: N::METHOD.to_string(),
2018-09-02 14:18:43 +02:00
params: to_value(params).unwrap(),
2018-09-01 16:40:45 +02:00
}
}
2018-09-01 15:18:02 +02:00
pub fn cast<N>(self) -> ::std::result::Result<N::Params, RawNotification>
where
N: Notification,
N::Params: DeserializeOwned,
{
if self.method != N::METHOD {
return Err(self);
}
Ok(from_value(self.params).unwrap())
}
}
fn read_msg_text(inp: &mut impl BufRead) -> Result<Option<String>> {
let mut size = None;
let mut buf = String::new();
loop {
buf.clear();
if inp.read_line(&mut buf)? == 0 {
return Ok(None);
}
if !buf.ends_with("\r\n") {
bail!("malformed header: {:?}", buf);
}
let buf = &buf[..buf.len() - 2];
if buf.is_empty() {
break;
}
let mut parts = buf.splitn(2, ": ");
let header_name = parts.next().unwrap();
let header_value = parts
.next()
.ok_or_else(|| format_err!("malformed header: {:?}", buf))?;
2018-09-01 15:18:02 +02:00
if header_name == "Content-Length" {
size = Some(header_value.parse::<usize>()?);
}
}
let size = size.ok_or_else(|| format_err!("no Content-Length"))?;
let mut buf = buf.into_bytes();
buf.resize(size, 0);
inp.read_exact(&mut buf)?;
let buf = String::from_utf8(buf)?;
debug!("< {}", buf);
Ok(Some(buf))
}
fn write_msg_text(out: &mut impl Write, msg: &str) -> Result<()> {
debug!("> {}", msg);
write!(out, "Content-Length: {}\r\n\r\n", msg.len())?;
out.write_all(msg.as_bytes())?;
out.flush()?;
Ok(())
}