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
#![crate_type = "lib"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![crate_name = "jsonrpc"]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![warn(missing_docs)]
extern crate hyper;
extern crate serde;
extern crate serde_json as json;
#[macro_use] mod macros;
pub mod client;
pub mod error;
#[derive(Clone, Debug, PartialEq)]
pub struct Request {
pub method: String,
pub params: Vec<json::Value>,
pub id: json::Value
}
#[derive(Clone, Debug, PartialEq)]
pub struct Response {
pub result: Option<json::Value>,
pub error: Option<error::RpcError>,
pub id: json::Value
}
serde_struct_serialize!(
Request,
RequestMapVisitor,
method => 0,
params => 1,
id => 2
);
serde_struct_deserialize!(
Request,
RequestVisitor,
RequestField,
RequestFieldVisitor,
method => Method,
params => Params,
id => Id
);
serde_struct_serialize!(
Response,
ResponseMapVisitor,
result => 0,
error => 1,
id => 2
);
serde_struct_deserialize!(
Response,
ResponseVisitor,
ResponseField,
ResponseFieldVisitor,
result => Result,
error => Error,
id => Id
);
#[cfg(test)]
mod tests {
use super::{Request, Response};
use super::error::RpcError;
use json;
use json::value::Value as JsonValue;
#[test]
fn request_serialize_round_trip() {
let original = Request {
method: "test".to_owned(),
params: vec![JsonValue::Null,
JsonValue::Bool(false),
JsonValue::Bool(true),
JsonValue::String("test2".to_owned())],
id: JsonValue::U64(69)
};
let ser = json::to_string(&original).unwrap();
let des = json::from_str(&ser).unwrap();
assert_eq!(original, des);
}
#[test]
fn response_serialize_round_trip() {
let original_err = RpcError {
code: -77,
message: "test4".to_string(),
data: Some(JsonValue::Bool(true))
};
let original = Response {
result: Some(JsonValue::Array(vec![JsonValue::Null,
JsonValue::Bool(false),
JsonValue::Bool(true),
JsonValue::String("test2".to_owned())])),
error: Some(original_err),
id: JsonValue::U64(101)
};
let ser = json::to_string(&original).unwrap();
let des = json::from_str(&ser).unwrap();
assert_eq!(original, des);
}
}