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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use serde::{Deserialize, Serialize};
#[repr(i32)]
pub enum ErrorCode {
ServerNotInitialized = -32002,
InvalidRequest = -32600,
MethodNotFound = -32601,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(untagged)]
pub enum ClientMessage {
Request(Request),
Notification(Notification),
}
impl From<Request> for ClientMessage {
fn from(request: Request) -> ClientMessage {
ClientMessage::Request(request)
}
}
impl From<Notification> for ClientMessage {
fn from(notification: Notification) -> ClientMessage {
ClientMessage::Notification(notification)
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(untagged)]
pub enum ServerMessage {
Response(Response),
Notification(Notification),
}
impl From<Response> for ServerMessage {
fn from(response: Response) -> ServerMessage {
ServerMessage::Response(response)
}
}
impl From<Notification> for ServerMessage {
fn from(notification: Notification) -> ServerMessage {
ServerMessage::Notification(notification)
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct Notification {
pub method: String,
pub params: serde_json::Value,
}
impl Notification {
pub fn new(method: impl Into<String>, params: impl Serialize) -> Self {
Notification {
method: method.into(),
params: serde_json::to_value(params).expect("Could not serialise notification"),
}
}
pub fn new_lsp<N>(params: N::Params) -> Self
where
N: lsp_types::notification::Notification,
N::Params: Serialize,
{
Self::new(N::METHOD, params)
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(untagged)]
enum IdRepr {
U64(u64),
String(String),
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(transparent)]
pub struct RequestId(IdRepr);
impl From<u64> for RequestId {
fn from(id: u64) -> RequestId {
RequestId(IdRepr::U64(id))
}
}
impl From<String> for RequestId {
fn from(id: String) -> RequestId {
RequestId(IdRepr::String(id))
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct Request {
pub id: RequestId,
pub method: String,
pub params: serde_json::Value,
}
impl Request {
#[cfg(test)]
pub fn new(id: RequestId, method: impl Into<String>, params: impl Serialize) -> Self {
Request {
id,
method: method.into(),
params: serde_json::to_value(params).expect("Could not serialise request"),
}
}
#[cfg(test)]
pub fn new_lsp<N>(id: RequestId, params: N::Params) -> Self
where
N: lsp_types::request::Request,
N::Params: Serialize,
{
Self::new(id, N::METHOD, params)
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct Response {
pub id: RequestId,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<ResponseError>,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct ResponseError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
}
impl Response {
pub fn new_ok(id: RequestId, result: impl Serialize) -> Response {
Response {
id,
result: Some(serde_json::to_value(result).expect("Could not serialise result")),
error: None,
}
}
pub fn new_err(id: RequestId, code: ErrorCode, message: impl Into<String>) -> Response {
let error = ResponseError {
code: code as i32,
message: message.into(),
data: None,
};
Response {
id,
result: None,
error: Some(error),
}
}
}