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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*!
# Easy jsonrpc

Generates rpc handlers based on a trait definition.

```
use easy_jsonrpc::{self, JSONRPCServer};

// the jsonrpc_server generates a JSONRPCServer for &dyn Adder
#[easy_jsonrpc::jsonrpc_server]
pub trait Adder {
    fn checked_add(&self, a: isize, b: isize) -> Option<isize>;
    fn wrapping_add(&self, a: isize, b: isize) -> isize;
    fn is_some(&self, a: Option<usize>) -> bool;
    fn takes_ref(&self, rf: &isize);
}

struct AdderImpl;

impl Adder for AdderImpl {
    fn checked_add(&self, a: isize, b: isize) -> Option<isize> {
        a.checked_add(b)
    }
    fn wrapping_add(&self, a: isize, b: isize) -> isize {
        a.wrapping_add(b)
    }
    fn is_some(&self, a: Option<usize>) -> bool {
        a.is_some()
    }
    fn takes_ref(&self, rf: &isize) {}
}

// create an rpc handler
let adder = (&AdderImpl {} as &dyn Adder);

assert_eq!(
    adder.handle_raw(
        r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": [1, 2], "id": 1}"#
    ),
    Some(r#"{"jsonrpc":"2.0","result":3,"id":1}"#.into())
);

// Named arguments are handled automatically
assert_eq!(
    adder.handle_raw(
        r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": {"a": 1, "b":2}, "id": 1}"#
    ),
    Some(r#"{"jsonrpc":"2.0","result":3,"id":1}"#.into())
);

// Calls with no id are treated as notifications
assert_eq!(
    adder.handle_raw(r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": [1, 1]}"#),
    None
);

// Calls with no return value return unit, aka `()` in rust, aka `null` in json
assert_eq!(
    adder.handle_raw(r#"{"jsonrpc": "2.0", "method": "takes_ref", "params": [1], "id": 1}"#),
    Some(r#"{"jsonrpc":"2.0","result":null,"id":1}"#.into())
);
```

Multilple types may implement the trait

```
# use easy_jsonrpc::{self, JSONRPCServer};

#[easy_jsonrpc::jsonrpc_server]
pub trait Useless {}

struct ImplOne;

impl Useless for ImplOne {}

enum ImplTwo {}

impl Useless for ImplTwo {}
```

This library contains a server generator. No client generator has been implemented yet.
*/

// The JSONRPCClient generator design is still WIP, but ideally clients will satisfy this
// property:
//   if T implements                  fn f(&self, args..) -> R
//   then JSONRPCClient<T> implements fn f(&self, args..) -> Future<Result<R, E>>

pub use easy_jsonrpc_proc_macro::jsonrpc_server;
use serde::ser::Serialize;

// used from generated code
#[doc(hidden)]
pub use jsonrpc_core::types::{
    Call, Error, ErrorCode, Failure, Id, MethodCall, Notification, Output, Params, Request,
    Response, Success, Value, Version,
};
#[doc(hidden)]
pub use serde_json;

/// Handles jsonrpc calls.
pub trait JSONRPCServer {
    /// type-check params and call method if method exists
    fn handle(&self, method: &str, params: Params) -> Result<Value, Error>;

    /// extract method name and parameters from call
    /// if call is a normal method call, call `handle` and return result
    /// if call is a notification, call `handle` and return None
    /// if call is invalid return a jsonrpc failure
    fn handle_call(&self, call: Call) -> Option<Output> {
        match call {
            Call::Notification(Notification { method, params, .. }) => {
                let _ = self.handle(&method, params);
                None
            }
            Call::MethodCall(MethodCall {
                method,
                params,
                id,
                jsonrpc,
            }) => {
                let output = match self.handle(&method, params) {
                    Ok(ok) => Output::Success(Success {
                        jsonrpc,
                        result: ok,
                        id,
                    }),
                    Err(err) => Output::Failure(Failure {
                        jsonrpc,
                        error: err,
                        id,
                    }),
                };
                Some(output)
            }
            Call::Invalid { id } => Some(Output::Failure(Failure {
                jsonrpc: Some(Version::V2),
                error: Error::invalid_request(),
                id,
            })),
        }
    }

    /// Handle a structured jsonrpc request. If the request is a batch, handle the entire batch.
    /// return the singe result or a batch of results
    /// If the request consists of only notifications, return nothing as per jsonrpc 2.0 spec
    fn handle_parsed(&self, request: Request) -> Option<Response> {
        match request {
            Request::Single(call) => self.handle_call(call).map(Response::Single),
            Request::Batch(mut calls) => {
                let outputs = calls
                    .drain(..)
                    .filter_map(|call| self.handle_call(call))
                    .collect::<Vec<_>>();
                if outputs.is_empty() {
                    None
                } else {
                    Some(Response::Batch(outputs))
                }
            }
        }
    }

    /// Accept request as a jsonrpc formatted string. Call handler.
    /// Return result as a jsonrpc formatted string.
    fn handle_raw(&self, request: &str) -> Option<String> {
        let request: Request = serde_json::from_str(request)
            .unwrap_or(Request::Single(Call::Invalid { id: Id::Null }));
        self.handle_parsed(request).map(|response: Response| {
            // Here we assume that serializing a Response will not return an error.
            // we know the type of response, it doesn't contain mutexes or invalid utf strings so
            // serialization should succeed. If it does not, we respond with invalid json.
            serde_json::to_string(&response)
                .unwrap_or_else(|_| "unexpected serialization error, this is a bug".into())
        })
    }
}

// Verify and convert jsonrpc Params into owned argument list.
// Verifies:
//    - Number of args in positional parameter list is correct
//    - No missing args in named parameter object
//    - No extra args in named parameter object
// Absent parameter objects are interpreted as empty positional parameter lists
//
// this function needs to be public because it is used the code genterated by jsonrpc::server
// the function is not a stable part of the api and should not be used by client crates
#[doc(hidden)]
pub fn get_rpc_args(names: &[&'static str], params: Params) -> Result<Vec<Value>, InvalidArgs> {
    let ar: Vec<Value> = match params {
        Params::Array(ar) => ar,
        Params::Map(mut ma) => {
            let mut ar: Vec<Value> = Vec::with_capacity(names.len());
            for name in names.iter() {
                ar.push(
                    ma.remove(*name)
                        .ok_or(InvalidArgs::MissingNamedParameter { name })?,
                );
            }
            debug_assert_eq!(ar.len(), names.len());
            match ma.keys().next() {
                Some(key) => return Err(InvalidArgs::ExtraNamedParameter { name: key.clone() }),
                None => ar,
            }
        }
        Params::None => vec![],
    };
    if ar.len() != names.len() {
        Err(InvalidArgs::WrongNumberOfArgs {
            expected: ar.len(),
            actual: names.len(),
        })
    } else {
        Ok(ar)
    }
}

#[doc(hidden)]
pub enum InvalidArgs {
    WrongNumberOfArgs { expected: usize, actual: usize },
    ExtraNamedParameter { name: String },
    MissingNamedParameter { name: &'static str },
    InvalidArgStructure { name: &'static str, index: usize },
}

impl Into<Error> for InvalidArgs {
    fn into(self) -> Error {
        match self {
            InvalidArgs::WrongNumberOfArgs { expected, actual } => Error::invalid_params(format!(
                "WrongNumberOfArgs. Expected {}. Actual {}",
                expected, actual
            )),
            InvalidArgs::ExtraNamedParameter { name } => {
                Error::invalid_params(format!("ExtraNamedParameter {}", name))
            }
            InvalidArgs::MissingNamedParameter { name } => {
                Error::invalid_params(format!("MissingNamedParameter {}", name))
            }
            InvalidArgs::InvalidArgStructure { name, index } => Error::invalid_params(format!(
                "InvalidArgStructure {} at position {}.",
                name, index
            )),
        }
    }
}

/// used from generated code
#[doc(hidden)]
pub fn try_serialize<T: Serialize>(t: &T) -> Result<Value, Error> {
    // Serde serde_json::to_value does not perform io. It's still not safe to unwrap the result. For
    // example, the implementation of Serialize for Mutex returns an error if the mutex is poisined.
    // Another example, serialize(&std::Path) returns an error when it encounters invalid utf-8.
    serde_json::to_value(t).map_err(|e| Error {
        code: ErrorCode::ServerError(8),
        // serde::error::Error doesn't implement Serialize so we a human readable message instead of
        // a structured error.
        message: format!("Error serializing response. {}", e),
        data: None,
    })
}

#[cfg(test)]
mod test {
    mod easy_jsonrpc {
        pub use crate::*;
    }
    use super::{jsonrpc_server, JSONRPCServer};
    use assert_matches::assert_matches;
    use jsonrpc_core::types::*;

    #[jsonrpc_server]
    pub trait Adder {
        fn checked_add(&self, a: isize, b: isize) -> Option<isize>;
        fn wrapping_add(&self, a: isize, b: isize) -> isize;
        fn greet(&self) -> String;
        fn swallow(&self);
        fn repeat_list(&self, lst: Vec<usize>) -> Vec<usize>;
        fn fail(&self) -> Result<isize, String>;
        fn succeed(&self) -> Result<isize, String>;
    }

    struct AdderImpl;
    impl Adder for AdderImpl {
        fn checked_add(&self, a: isize, b: isize) -> Option<isize> {
            a.checked_add(b)
        }

        fn wrapping_add(&self, a: isize, b: isize) -> isize {
            a.wrapping_add(b)
        }

        fn greet(&self) -> String {
            "hello".into()
        }

        fn swallow(&self) {}

        fn repeat_list(&self, lst: Vec<usize>) -> Vec<usize> {
            let mut ret = lst.clone();
            ret.extend(lst);
            ret
        }

        fn fail(&self) -> Result<isize, String> {
            Err("tada!".into())
        }

        fn succeed(&self) -> Result<isize, String> {
            Ok(1)
        }
    }

    fn assert_adder_response(request: &str, response: &str) {
        assert_eq!(
            &(&AdderImpl {} as &dyn Adder).handle_raw(request).unwrap(),
            response
        );
    }

    fn handle_single(request: &str) -> Output {
        let a: Option<Response> =
            (&AdderImpl {} as &dyn Adder).handle_parsed(serde_json::from_str(&request).unwrap());
        match a {
            Some(Response::Single(a)) => a,
            _ => panic!(),
        }
    }

    #[test]
    fn positional_args() {
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": [1, 1], "id": 1}"#,
            r#"{"jsonrpc":"2.0","result":2,"id":1}"#,
        );
    }

    #[test]
    fn named_args() {
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": {"a": 1, "b":1}, "id": 1}"#,
            r#"{"jsonrpc":"2.0","result":2,"id":1}"#,
        );
    }

    #[test]
    fn null_args() {
        let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "greet", "params": {}, "id": 1}"#,
            response,
        );
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "greet", "params": [], "id": 1}"#,
            response,
        );
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "greet", "params": null, "id": 1}"#,
            response,
        );
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "greet", "id": 1}"#,
            response,
        );
    }

    #[test]
    fn null_return() {
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "swallow", "params": [], "id": 1}"#,
            r#"{"jsonrpc":"2.0","result":null,"id":1}"#,
        );
    }

    #[test]
    fn incorrect_method_name() {
        assert_matches!(
            handle_single(r#"{"jsonrpc": "2.0", "method": "nonexist", "params": [], "id": 1}"#),
            Output::Failure(Failure {
                error:
                    Error {
                        code: ErrorCode::MethodNotFound,
                        ..
                    },
                ..
            })
        );
    }

    #[test]
    fn incorrect_args() {
        assert_matches!(
            handle_single(r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": [], "id": 1}"#),
            Output::Failure(Failure {
                error:
                    Error {
                        code: ErrorCode::InvalidParams,
                        ..
                    },
                ..
            })
        );
        assert_matches!(
            handle_single(
                r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": {
	                "notanarg": 1, "notarg": 1}, "id": 1}"#
            ),
            Output::Failure(Failure {
                error:
                    Error {
                        code: ErrorCode::InvalidParams,
                        ..
                    },
                ..
            })
        );
        assert_matches!(
            handle_single(
                r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": [[], []], "id": 1}"#
            ),
            Output::Failure(Failure {
                error:
                    Error {
                        code: ErrorCode::InvalidParams,
                        ..
                    },
                ..
            })
        );
    }

    #[test]
    fn complex_type() {
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "repeat_list", "params": [[1, 2, 3]], "id": 1}"#,
            r#"{"jsonrpc":"2.0","result":[1,2,3,1,2,3],"id":1}"#,
        );
        assert_matches!(
            handle_single(
                r#"{"jsonrpc": "2.0", "method": "repeat_list", "params": [[1], [12]], "id": 1}"#,
            ),
            Output::Failure(Failure {
                error:
                    Error {
                        code: ErrorCode::InvalidParams,
                        ..
                    },
                ..
            })
        );
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "fail", "params": [], "id": 1}"#,
            r#"{"jsonrpc":"2.0","result":{"Err":"tada!"},"id":1}"#,
        );
        assert_adder_response(
            r#"{"jsonrpc": "2.0", "method": "succeed", "params": [], "id": 1}"#,
            r#"{"jsonrpc":"2.0","result":{"Ok":1},"id":1}"#,
        );
    }

    #[test]
    fn notification() {
        let request =
            serde_json::from_str(r#"{"jsonrpc": "2.0", "method": "succeed", "params": []}"#)
                .unwrap();
        assert_eq!((&AdderImpl {} as &dyn Adder).handle_parsed(request), None);
    }
}