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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::fmt::{Display, Formatter};

use completeq_rs::error::CompleteQError;
use futures::channel::mpsc::SendError;
use serde::*;

/// A rpc call is represented by sending a Request object to a Server.  
///
/// visit [`here`](https://www.jsonrpc.org/specification) for details
#[derive(Debug, Serialize, Deserialize, Default, PartialEq)]
pub struct Request<S, P>
where
    S: AsRef<str>,
{
    /// An identifier established by the Client that MUST contain a String, Number,
    /// or NULL value if included. If it is not included it is assumed to be a notification.
    /// The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<usize>,
    /// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
    pub jsonrpc: Version,
    /// A String containing the name of the method to be invoked. Method names
    /// that begin with the word rpc followed by a period character (U+002E or ASCII 46)
    /// are reserved for rpc-internal methods and extensions and MUST NOT be used for anything else
    pub method: S,
    /// A Structured value that holds the parameter values to be used during the invocation of the method. This member MAY be omitted.
    pub params: P,
}

/// JSONRPC version type.
///
/// When [`Serialize`]/[`Deserialize`] JSONRPC object, automatic fill or check version string "2.0"
#[derive(Debug, Default, PartialEq)]
pub struct Version;

impl Serialize for Version {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str("2.0")
    }
}

impl<'de> Deserialize<'de> for Version {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(visitor::VersionVisitor)
    }
}

/// When a rpc call is made, the Server MUST reply with a Response,
/// except for in the case of Notifications.
///
/// visit [`here`](https://www.jsonrpc.org/specification) for details
#[derive(Debug, Serialize, Deserialize, Default, PartialEq)]
pub struct Response<S, R, D>
where
    S: AsRef<str>,
{
    /// This member is REQUIRED on error.
    /// This member MUST NOT exist if there was no error triggered during invocation.
    pub id: usize,
    /// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
    pub jsonrpc: Version,
    /// This member is REQUIRED on success.
    /// This member MUST NOT exist if there was an error invoking the method.
    /// The value of this member is determined by the method invoked on the Server.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<R>,

    ///This member is REQUIRED on error.
    /// This member MUST NOT exist if there was no error triggered during invocation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<Error<S, D>>,
}

/// JSONRPC type compatible with both [`Request`] and [`Response`] data structures
#[derive(Debug, Serialize, Deserialize, Default, PartialEq)]
struct JSONRPC<S, P, R, D> {
    /// An identifier established by the Client that MUST contain a String, Number,
    /// or NULL value if included. If it is not included it is assumed to be a notification.
    /// The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<usize>,
    /// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
    pub jsonrpc: Version,
    /// A String containing the name of the method to be invoked. Method names
    /// that begin with the word rpc followed by a period character (U+002E or ASCII 46)
    /// are reserved for rpc-internal methods and extensions and MUST NOT be used for anything else
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<S>,
    /// A Structured value that holds the parameter values to be used during the invocation of the method. This member MAY be omitted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<P>,
    /// This member is REQUIRED on success.
    /// This member MUST NOT exist if there was an error invoking the method.
    /// The value of this member is determined by the method invoked on the Server.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<R>,

    ///This member is REQUIRED on error.
    /// This member MUST NOT exist if there was no error triggered during invocation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<Error<S, D>>,
}

/// When a rpc call encounters an error,
/// the Response Object MUST contain the error member with a value that is a Object.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, thiserror::Error)]
pub struct Error<S, D> {
    /// A Number that indicates the error type that occurred.
    pub code: ErrorCode,
    /// A String providing a short description of the error.
    /// The message SHOULD be limited to a concise single sentence
    pub message: S,
    ///A Primitive or Structured value that contains additional information about the error.
    /// This may be omitted.
    /// The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
    ///
    pub data: Option<D>,
}

impl Display for Error<String, serde_json::Value> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "RPCError({}) {}", self.code, self.message)
    }
}

impl From<serde_json::Error> for Error<String, serde_json::Value> {
    fn from(err: serde_json::Error) -> Self {
        Self {
            code: ErrorCode::ParseError,
            message: format!("Serialize/Deserialize json data error: {}", err),
            data: None,
        }
    }
}

impl From<CompleteQError> for Error<String, serde_json::Value> {
    fn from(err: CompleteQError) -> Self {
        Self {
            code: ErrorCode::InternalError,
            message: format!("RPC call channel broken: {}", err),
            data: None,
        }
    }
}

impl From<SendError> for Error<String, serde_json::Value> {
    fn from(err: SendError) -> Self {
        Self {
            code: ErrorCode::InternalError,
            message: format!("RPC send channel broken: {}", err),
            data: None,
        }
    }
}

impl Error<String, serde_json::Value> {
    pub fn from_std_error<E>(e: E) -> Self
    where
        E: Display,
    {
        Self {
            code: ErrorCode::InternalError,
            message: format!("Unknown error: {}", e),
            data: None,
        }
    }
}

/// Maping other error type to JSONRPC [`Error`]
pub fn map_error<E>(err: E) -> Error<String, serde_json::Value>
where
    E: Display,
{
    Error::<String, serde_json::Value>::from_std_error(err)
}

/// The error codes from and including -32768 to -32000 are reserved for pre-defined errors.
/// Any code within this range, but not defined explicitly below is reserved for future use.
/// The error codes are nearly the same as those suggested for XML-RPC at the following url:
/// <http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php>
#[derive(thiserror::Error, Debug, PartialEq, Clone)]
pub enum ErrorCode {
    /// An error occurred on the server while parsing the JSON text.
    #[error("Invalid JSON was received by the server.")]
    ParseError,
    #[error("The JSON sent is not a valid Request object.")]
    InvalidRequest,
    #[error("The method does not exist / is not available.")]
    MethodNotFound,
    #[error("Invalid method parameter(s).")]
    InvalidParams,
    #[error("Internal JSON-RPC error.")]
    InternalError,
    /// Reserved for implementation-defined server-errors.
    #[error("Server error({0}),{1}")]
    ServerError(i64, String),
}

impl serde::Serialize for ErrorCode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::ParseError => serializer.serialize_i64(-32700),
            Self::InvalidRequest => serializer.serialize_i64(-32600),
            Self::MethodNotFound => serializer.serialize_i64(-32601),
            Self::InvalidParams => serializer.serialize_i64(-32602),
            Self::InternalError => serializer.serialize_i64(-32603),
            Self::ServerError(code, _) => serializer.serialize_i64(*code),
        }
    }
}

impl<'de> serde::Deserialize<'de> for ErrorCode {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let code = deserializer.deserialize_i64(visitor::ErrorCodeVisitor)?;

        match code {
            -32700 => Ok(ErrorCode::ParseError),
            -32600 => Ok(ErrorCode::InvalidRequest),
            -32601 => Ok(ErrorCode::MethodNotFound),
            -32602 => Ok(ErrorCode::InvalidParams),
            -32603 => Ok(ErrorCode::InternalError),
            _ => {
                // Check reserved implementation-defined server-errors range.
                if code <= -32000 && code >= -32099 {
                    Ok(ErrorCode::ServerError(code, "".to_owned()))
                } else {
                    Err(anyhow::format_err!("Invalid JSONRPC error code {}", code))
                        .map_err(serde::de::Error::custom)
                }
            }
        }
    }
}

mod visitor {
    use serde::de;
    use std::fmt;

    use crate::Version;

    pub struct ErrorCodeVisitor;

    impl<'de> de::Visitor<'de> for ErrorCodeVisitor {
        type Value = i64;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("Version string MUST be exactly 2.0")
        }

        fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(i64::from(value))
        }

        fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(i64::from(value))
        }

        fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(value)
        }
    }

    pub struct VersionVisitor;

    impl<'de> de::Visitor<'de> for VersionVisitor {
        type Value = Version;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("an integer between -2^63 and 2^63")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            if v != "2.0" {
                return Err(anyhow::format_err!(
                    "Version string MUST be exactly 2.0, but got `{}`",
                    v
                ))
                .map_err(serde::de::Error::custom);
            }

            Ok(Version {})
        }

        fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            if v.as_str() != "2.0" {
                return Err(anyhow::format_err!(
                    "Version string MUST be exactly 2.0, but got `{}`",
                    v
                ))
                .map_err(serde::de::Error::custom);
            }

            Ok(Version {})
        }
    }
}

#[cfg(test)]
mod tests {

    use serde::{Deserialize, Serialize};
    use serde_json::json;

    use crate::Request;

    #[test]
    fn test_array_params() {
        _ = pretty_env_logger::try_init();
        #[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
        struct Params<S>(i32, S);

        let request = Request {
            method: "hello",
            params: Params(10, "world"),
            ..Default::default()
        };

        let json = serde_json::to_string(&request).unwrap();

        assert_eq!(
            json!({ "jsonrpc":"2.0", "method":"hello","params":[10, "world"]}).to_string(),
            json
        );

        let request = serde_json::from_value::<Request<String, Params<String>>>(
            json!({ "jsonrpc":"2.0", "method":"hello","params":[20, "hello"]}),
        )
        .expect("deserialize json");

        assert_eq!(request.params.0, 20);
        assert_eq!(request.params.1, "hello");
    }

    #[test]
    fn test_version_check() {
        #[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
        struct Params<S>(i32, S);

        let request = serde_json::from_value::<Request<String, Params<String>>>(
            json!({"jsonrpc":"3.0", "method":"hello","params":[10, "world"]}),
        );

        assert_eq!(
            format!("{}", request.unwrap_err()),
            "Version string MUST be exactly 2.0, but got `3.0`",
        );
    }

    #[test]
    fn test_tuple_params() {
        let request = serde_json::from_value::<Request<String, (i32, String)>>(
            json!({ "jsonrpc":"2.0", "method":"hello","params":[10, "world"]}),
        )
        .expect("parse tuple params");

        assert_eq!(request.params.0, 10);
        assert_eq!(request.params.1, "world");
    }

    #[test]
    fn test_object_params() {
        _ = pretty_env_logger::try_init();

        // JSONRPC Request params is object.
        #[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
        struct Params<S> {
            id: i32,
            name: S,
        }

        let request = Request {
            method: "hello",
            params: Params {
                id: 10,
                name: "world",
            },
            ..Default::default()
        };

        let json = serde_json::to_string(&request).unwrap();

        // check object params serialize
        assert_eq!(
            json!({"jsonrpc":"2.0", "method":"hello","params":{"id":10, "name":"world"}})
                .to_string(),
            json
        );

        let request = serde_json::from_value::<Request<String, Params<String>>>(
            json!({"jsonrpc":"2.0", "method":"hello","params":{"id": 20, "name":"hello"}}),
        )
        .expect("deserialize json");

        assert_eq!(request.params.id, 20);
        assert_eq!(request.params.name, "hello");
    }
}