@@ -9,6 +9,18 @@ use std::collections::HashMap;
9
9
use std:: convert:: TryFrom ;
10
10
use std:: fmt;
11
11
12
+ const LSPS_MESSAGE_SERIALIZED_STRUCT_NAME : & str = "LSPSMessage" ;
13
+ const JSONRPC_FIELD_KEY : & str = "jsonrpc" ;
14
+ const JSONRPC_FIELD_VALUE : & str = "2.0" ;
15
+ const JSONRPC_METHOD_FIELD_KEY : & str = "method" ;
16
+ const JSONRPC_ID_FIELD_KEY : & str = "id" ;
17
+ const JSONRPC_PARAMS_FIELD_KEY : & str = "params" ;
18
+ const JSONRPC_RESULT_FIELD_KEY : & str = "result" ;
19
+ const JSONRPC_ERROR_FIELD_KEY : & str = "error" ;
20
+ const JSONRPC_INVALID_MESSAGE_ERROR_CODE : i32 = -32700 ;
21
+ const JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE : & str = "parse error" ;
22
+ const LSPS0_LISTPROTOCOLS_METHOD_NAME : & str = "lsps0.listprotocols" ;
23
+
12
24
pub const LSPS_MESSAGE_TYPE : u16 = 37913 ;
13
25
14
26
#[ derive( Clone , Debug , PartialEq , Eq , Serialize , Deserialize ) ]
@@ -118,39 +130,43 @@ impl Serialize for LSPSMessage {
118
130
where
119
131
S : serde:: Serializer ,
120
132
{
121
- let mut jsonrpc_object = serializer. serialize_struct ( "LSPSMessage" , 3 ) ?;
133
+ let mut jsonrpc_object =
134
+ serializer. serialize_struct ( LSPS_MESSAGE_SERIALIZED_STRUCT_NAME , 3 ) ?;
122
135
123
- jsonrpc_object. serialize_field ( "jsonrpc" , "2.0" ) ?;
136
+ jsonrpc_object. serialize_field ( JSONRPC_FIELD_KEY , JSONRPC_FIELD_VALUE ) ?;
124
137
125
138
match self {
126
139
LSPSMessage :: LSPS0 ( LSPS0Message :: Request ( request_id, request) ) => {
127
- jsonrpc_object. serialize_field ( "method" , request. method ( ) ) ?;
128
- jsonrpc_object. serialize_field ( "id" , & request_id. 0 ) ?;
140
+ jsonrpc_object. serialize_field ( JSONRPC_METHOD_FIELD_KEY , request. method ( ) ) ?;
141
+ jsonrpc_object. serialize_field ( JSONRPC_ID_FIELD_KEY , & request_id. 0 ) ?;
129
142
130
143
match request {
131
144
LSPS0Request :: ListProtocols ( params) => {
132
- jsonrpc_object. serialize_field ( "params" , params) ?
145
+ jsonrpc_object. serialize_field ( JSONRPC_PARAMS_FIELD_KEY , params) ?
133
146
}
134
147
} ;
135
148
}
136
149
LSPSMessage :: LSPS0 ( LSPS0Message :: Response ( request_id, response) ) => {
137
- jsonrpc_object. serialize_field ( "id" , & request_id. 0 ) ?;
150
+ jsonrpc_object. serialize_field ( JSONRPC_ID_FIELD_KEY , & request_id. 0 ) ?;
138
151
139
152
match response {
140
153
LSPS0Response :: ListProtocols ( result) => {
141
- jsonrpc_object. serialize_field ( "result" , result) ?;
154
+ jsonrpc_object. serialize_field ( JSONRPC_RESULT_FIELD_KEY , result) ?;
142
155
}
143
156
LSPS0Response :: ListProtocolsError ( error) => {
144
- jsonrpc_object. serialize_field ( "error" , error) ?;
157
+ jsonrpc_object. serialize_field ( JSONRPC_ERROR_FIELD_KEY , error) ?;
145
158
}
146
159
}
147
160
}
148
161
LSPSMessage :: Invalid => {
149
- let error =
150
- ResponseError { code : -32700 , message : "parse error" . to_string ( ) , data : None } ;
162
+ let error = ResponseError {
163
+ code : JSONRPC_INVALID_MESSAGE_ERROR_CODE ,
164
+ message : JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE . to_string ( ) ,
165
+ data : None ,
166
+ } ;
151
167
152
- jsonrpc_object. serialize_field ( "id" , & serde_json:: Value :: Null ) ?;
153
- jsonrpc_object. serialize_field ( "error" , & error) ?;
168
+ jsonrpc_object. serialize_field ( JSONRPC_ID_FIELD_KEY , & serde_json:: Value :: Null ) ?;
169
+ jsonrpc_object. serialize_field ( JSONRPC_ERROR_FIELD_KEY , & error) ?;
154
170
}
155
171
}
156
172
@@ -202,59 +218,63 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
202
218
}
203
219
}
204
220
205
- if let Some ( method) = method {
206
- if let Some ( id) = id {
207
- match method {
208
- "lsps0.listprotocols" => {
209
- let list_protocols_request =
210
- serde_json:: from_value ( params. unwrap_or ( json ! ( { } ) ) )
211
- . map_err ( de:: Error :: custom) ?;
221
+ match ( id, method) {
222
+ ( Some ( id) , Some ( method) ) => match method {
223
+ LSPS0_LISTPROTOCOLS_METHOD_NAME => {
224
+ let list_protocols_request =
225
+ serde_json:: from_value ( params. unwrap_or ( json ! ( { } ) ) )
226
+ . map_err ( de:: Error :: custom) ?;
212
227
213
- self . request_id_to_method . insert ( id. clone ( ) , method. to_string ( ) ) ;
228
+ self . request_id_to_method . insert ( id. clone ( ) , method. to_string ( ) ) ;
214
229
215
- Ok ( LSPSMessage :: LSPS0 ( LSPS0Message :: Request (
216
- RequestId ( id) ,
217
- LSPS0Request :: ListProtocols ( list_protocols_request) ,
218
- ) ) )
219
- }
220
- _ => Err ( de:: Error :: custom ( format ! (
221
- "Received request with unknown method: {}" ,
222
- method
223
- ) ) ) ,
230
+ Ok ( LSPSMessage :: LSPS0 ( LSPS0Message :: Request (
231
+ RequestId ( id) ,
232
+ LSPS0Request :: ListProtocols ( list_protocols_request) ,
233
+ ) ) )
224
234
}
225
- } else {
226
- Err ( de:: Error :: custom ( format ! ( "Received unknown notification: {}" , method) ) )
227
- }
228
- } else if let Some ( id) = id {
229
- if let Some ( method) = self . request_id_to_method . get ( & id) {
230
- match method. as_str ( ) {
231
- "lsps0.listprotocols" => {
232
- if let Some ( error) = error {
233
- Ok ( LSPSMessage :: LSPS0 ( LSPS0Message :: Response (
234
- RequestId ( id) ,
235
- LSPS0Response :: ListProtocolsError ( error) ,
236
- ) ) )
237
- } else if let Some ( result) = result {
238
- let list_protocols_response =
239
- serde_json:: from_value ( result) . map_err ( de:: Error :: custom) ?;
240
- Ok ( LSPSMessage :: LSPS0 ( LSPS0Message :: Response (
241
- RequestId ( id) ,
242
- LSPS0Response :: ListProtocols ( list_protocols_response) ,
243
- ) ) )
244
- } else {
245
- Err ( de:: Error :: custom ( "Received invalid JSON-RPC object: one of method, result, or error required" ) )
235
+ _ => Err ( de:: Error :: custom ( format ! (
236
+ "Received request with unknown method: {}" ,
237
+ method
238
+ ) ) ) ,
239
+ } ,
240
+ ( Some ( id) , None ) => {
241
+ if let Some ( method) = self . request_id_to_method . get ( & id) {
242
+ match method. as_str ( ) {
243
+ LSPS0_LISTPROTOCOLS_METHOD_NAME => {
244
+ if let Some ( error) = error {
245
+ Ok ( LSPSMessage :: LSPS0 ( LSPS0Message :: Response (
246
+ RequestId ( id) ,
247
+ LSPS0Response :: ListProtocolsError ( error) ,
248
+ ) ) )
249
+ } else if let Some ( result) = result {
250
+ let list_protocols_response =
251
+ serde_json:: from_value ( result) . map_err ( de:: Error :: custom) ?;
252
+ Ok ( LSPSMessage :: LSPS0 ( LSPS0Message :: Response (
253
+ RequestId ( id) ,
254
+ LSPS0Response :: ListProtocols ( list_protocols_response) ,
255
+ ) ) )
256
+ } else {
257
+ Err ( de:: Error :: custom ( "Received invalid JSON-RPC object: one of method, result, or error required" ) )
258
+ }
246
259
}
260
+ _ => Err ( de:: Error :: custom ( format ! (
261
+ "Received response for an unknown request method: {}" ,
262
+ method
263
+ ) ) ) ,
247
264
}
248
- _ => Err ( de:: Error :: custom ( format ! (
249
- "Received response for an unknown request method: {}" ,
250
- method
251
- ) ) ) ,
265
+ } else {
266
+ Err ( de:: Error :: custom ( format ! (
267
+ "Received response for unknown request id: {}" ,
268
+ id
269
+ ) ) )
252
270
}
253
- } else {
254
- Err ( de:: Error :: custom ( format ! ( "Received response for unknown request id: {}" , id) ) )
255
271
}
256
- } else {
257
- Err ( de:: Error :: custom ( "Received invalid JSON-RPC object: one of method or id required" ) )
272
+ ( None , Some ( method) ) => {
273
+ Err ( de:: Error :: custom ( format ! ( "Received unknown notification: {}" , method) ) )
274
+ }
275
+ ( None , None ) => Err ( de:: Error :: custom (
276
+ "Received invalid JSON-RPC object: one of method or id required" ,
277
+ ) ) ,
258
278
}
259
279
}
260
280
}
0 commit comments