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
// Rust JSON-RPC Library
// Written in 2015 by
//   Andrew Poelstra <apoelstra@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

//! # Rust JSON-RPC Library
//!
//! Rust support for the JSON-RPC 2.0 protocol.
//!

#![crate_type = "lib"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![crate_name = "jsonrpc"]

// Coding conventions
#![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)]
/// A JSONRPC request object
pub struct Request {
    /// The name of the RPC call
    pub method: String,
    /// Parameters to the RPC call
    pub params: Vec<json::Value>,
    /// Identifier for this Request, which should appear in the response
    pub id: json::Value
}

#[derive(Clone, Debug, PartialEq)]
/// A JSONRPC response object
pub struct Response {
    /// A result if there is one, or null
    pub result: Option<json::Value>,
    /// An error if there is one, or null
    pub error: Option<error::RpcError>,
    /// Identifier from the request
    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);
    }
}