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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use base64::Engine as _;
use serde::de::DeserializeOwned;
use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize};

use super::error::map_json_rpc_error_code_to_str;

/// Generate a random json_rpc_id string that follows the requirements of LSPS0
///
/// - Should be a String
/// - Should be at generated using at least 80 bits of randomness
pub fn generate_random_rpc_id() -> String {
    // The specification requires an id using least 80 random bits of randomness
    let seed: [u8; 10] = rand::random();
    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(seed)
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JsonRpcMethod<I, O, E>
where
    E: MapErrorCode,
{
    pub method: &'static str,
    #[serde(skip_serializing)]
    request: std::marker::PhantomData<I>,
    #[serde(skip_serializing)]
    return_type: std::marker::PhantomData<O>,
    #[serde(skip_serializing)]
    error_type: std::marker::PhantomData<E>,
}

impl<I, O, E> JsonRpcMethod<I, O, E>
where
    E: MapErrorCode,
{
    pub const fn new(method: &'static str) -> Self {
        Self {
						method,
            request: std::marker::PhantomData,
            return_type: std::marker::PhantomData,
            error_type: std::marker::PhantomData,
        }
    }

    pub const fn name(&self) -> &'static str {
        self.method
    }

    pub fn create_request(&self, params: I, json_rpc_id: String) -> JsonRpcRequest<I> {
        JsonRpcRequest::<I> {
            jsonrpc: String::from("2.0"),
            id: json_rpc_id,
            method: self.method.into(),
            params,
        }
    }
}

impl<O, E> JsonRpcMethod<NoParams, O, E>
where
    E: MapErrorCode,
{
    pub fn create_request_no_params(&self, json_rpc_id: String) -> JsonRpcRequest<NoParams> {
        self.create_request(NoParams::default(), json_rpc_id)
    }
}

impl<I, O, E> std::convert::From<&JsonRpcMethod<I, O, E>> for String 
where
    E: MapErrorCode
		{
    fn from(value: &JsonRpcMethod<I, O, E>) -> Self {
        value.method.into()
    }
}

impl<'de, I, O, E> JsonRpcMethod<I, O, E>
where
    O: Deserialize<'de>,
    E: Deserialize<'de> + MapErrorCode,
{
    pub fn parse_json_response_str(
        &self,
        json_str: &'de str,
    ) -> Result<JsonRpcResponse<O, E>, serde_json::Error> {
        serde_json::from_str(json_str)
    }
}

impl<I, O, E> JsonRpcMethod<I, O, E>
where
    O: DeserializeOwned,
    E: DeserializeOwned + MapErrorCode,
{
    pub fn parse_json_response_value(
        &self,
        json_value: serde_json::Value,
    ) -> Result<JsonRpcResponse<O, E>, serde_json::Error> {
        serde_json::from_value(json_value)
    }
}

// We only intend to implement to implement an LSP-client and only intend on sending requests
// Therefore, we only implement the serialization of requests
//
// D is the data-type of the request-data
// R is the data-type of the result if the query is successful
#[derive(Serialize, Deserialize, Debug)]
pub struct JsonRpcRequest<I> {
    pub jsonrpc: String,
    pub id: String,
    pub method: String,
    pub params: I,
}

// LSPS0 specifies that the RPC-request must use a parameter-by-name structure.
//
// A JSONRpcRequest<(),()> will be serialized to a json where "params" : null
// A JsonRpcRequest<NoParams, ()> will be serialized to "params" : {} which is compliant
#[derive(Debug, Default, Clone, Deserialize, PartialEq)]
pub struct NoParams {}

// Serde serializes NoParams to null by default
// LSPS0 requires an empty dictionary in this situation
impl Serialize for NoParams {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_map(Some(0))?.end()
    }
}

impl<I> JsonRpcRequest<I> {
    pub fn new<O, E>(method: JsonRpcMethod<I, O, E>, params: I) -> Self
    where
        E: MapErrorCode,
    {
        return Self {
            jsonrpc: String::from("2.0"),
            id: generate_random_rpc_id(),
            method: method.method.into(),
            params,
        }
    }
}

impl JsonRpcRequest<NoParams> {
    pub fn new_no_params<O, E>(method: JsonRpcMethod<NoParams, O, E>) -> Self
    where
        E: MapErrorCode,
    {
        return Self {
            jsonrpc: String::from("2.0"),
            id: generate_random_rpc_id(),
            method: method.method.into(),
            params: NoParams::default(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JsonRpcResponseSuccess<O> {
    pub id: String,
    pub result: O,
    pub jsonrpc: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JsonRpcResponseFailure<E> {
    pub id: String,
    pub error: ErrorData<E>,
    pub jsonrpc: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsonRpcResponse<O, E> {
    Error(JsonRpcResponseFailure<E>),
    Ok(JsonRpcResponseSuccess<O>),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorData<E> {
    pub code: i64,
    pub message: String,
    pub data: Option<E>,
}

impl<E> ErrorData<E>
where
    E: MapErrorCode,
{
    pub fn code_str(&self) -> &str {
        return E::get_code_str(self.code);
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DefaultError;

pub trait MapErrorCode {
    fn get_code_str(code: i64) -> &'static str;
}

impl MapErrorCode for DefaultError {
    fn get_code_str(code: i64) -> &'static str {
        map_json_rpc_error_code_to_str(code)
    }
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn serialize_no_params() {
        let no_params = NoParams::default();
        let json_str = serde_json::to_string(&no_params).unwrap();

        assert_eq!(json_str, "{}")
    }

    #[test]
    fn deserialize_no_params() {
        let _: NoParams = serde_json::from_str("{}").unwrap();
    }

    #[test]
    fn serialize_json_rpc_request() {
        let rpc_request = JsonRpcRequest {
            id: "abcefg".into(),
            jsonrpc: "2.0".into(),
            params: NoParams::default(),
            method: "test.method".into(),
        };

        let json_str = serde_json::to_string(&rpc_request).unwrap();

        let value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(value.get("jsonrpc").unwrap(), "2.0");
        assert_eq!(value.get("id").unwrap(), &rpc_request.id);
        assert_eq!(value.get("method").unwrap(), "test.method");
        assert!(value.get("params").unwrap().as_object().unwrap().is_empty())
    }

    #[test]
    fn serialize_json_rpc_response_success() {
        let rpc_response_ok: JsonRpcResponseSuccess<String> = JsonRpcResponseSuccess {
            id: String::from("abc"),
            result: String::from("result_data"),
            jsonrpc: String::from("2.0"),
        };

        let rpc_response: JsonRpcResponse<String, ()> = JsonRpcResponse::Ok(rpc_response_ok);

        let json_str: String = serde_json::to_string(&rpc_response).unwrap();

        let value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(value.get("jsonrpc").unwrap(), "2.0");
        assert_eq!(value.get("id").unwrap(), "abc");
        assert_eq!(value.get("result").unwrap(), "result_data")
    }

    #[test]
    fn serialize_json_rpc_response_error() {
        let rpc_response: JsonRpcResponse<String, ()> =
            JsonRpcResponse::Error(JsonRpcResponseFailure {
                jsonrpc: String::from("2.0"),
                id: String::from("abc"),
                error: ErrorData {
                    code: -32700,
                    message: String::from("Failed to parse data"),
                    data: None,
                },
            });

        let json_str: String = serde_json::to_string(&rpc_response).unwrap();

        let value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(value.get("jsonrpc").unwrap(), "2.0");
        assert_eq!(value.get("id").unwrap(), "abc");
        assert_eq!(value.get("error").unwrap().get("code").unwrap(), -32700);
        assert_eq!(
            value.get("error").unwrap().get("message").unwrap(),
            "Failed to parse data"
        );
    }

    #[test]
    fn create_rpc_request_from_call() {
        let rpc_method = JsonRpcMethod::<NoParams, (), DefaultError>::new("test.method");
        let json_rpc_id = generate_random_rpc_id();
        let rpc_request = rpc_method.create_request_no_params(json_rpc_id);

        assert_eq!(rpc_request.method, "test.method");
        assert_eq!(rpc_request.jsonrpc, "2.0");
        assert_eq!(rpc_request.params, NoParams::default());
    }

    #[test]
    fn parse_rpc_response_success_from_call() {
        let rpc_method = JsonRpcMethod::<NoParams, String, DefaultError>::new("test.return_string");

        let json_value = serde_json::json!({
            "jsonrpc" : "2.0",
            "result" : "result_data",
            "id" : "request_id"
        });

        let json_str = serde_json::to_string(&json_value).unwrap();

        let result = rpc_method.parse_json_response_str(&json_str).unwrap();

        match result {
            JsonRpcResponse::Error(_) => panic!("Deserialized a good response but got panic"),
            JsonRpcResponse::Ok(ok) => {
                assert_eq!(ok.jsonrpc, "2.0");
                assert_eq!(ok.id, "request_id");
                assert_eq!(ok.result, "result_data")
            }
        }
    }

    #[test]
    fn parse_rpc_response_failure_from_call() {
        let rpc_method = JsonRpcMethod::<NoParams, String, DefaultError>::new("test.return_string");

        let json_value = serde_json::json!({
            "jsonrpc" : "2.0",
            "error" : { "code" : -32700, "message" : "Failed to parse response"},
            "id" : "request_id"
        });

        let json_str = serde_json::to_string(&json_value).unwrap();

        let result = rpc_method.parse_json_response_str(&json_str).unwrap();

        match result {
            JsonRpcResponse::Error(err) => {
                assert_eq!(err.jsonrpc, "2.0");

                assert_eq!(err.error.code, -32700);
                assert_eq!(err.error.code_str(), "parsing_error");

                assert_eq!(err.error.message, "Failed to parse response");
                assert_eq!(err.id, "request_id");
            }
            JsonRpcResponse::Ok(_ok) => {
                panic!("Failure deserialized as Ok")
            }
        }
    }
}