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
// Rust JSON-RPC 1.0 Library
// Written in 2015 by
//     Andrew Poelstra <apoelstra@wpsoftware.net>
//
// Modified in 2016 by
//     Jean Pierre De Jesus Dudey Diaz <jeandudey@hotmail.com>
//
// Modified in 2017 by
//     Aleksey Sidorov <aleksei.sidorov@xdev.re>
//
// 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 1.0 protocol.
//!

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

// Coding conventions
#![deny(missing_docs,
        trivial_casts, trivial_numeric_casts,
        unused_import_braces, unused_qualifications)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]

extern crate hyper;

#[macro_use]
extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate serde_json;

pub mod client;
pub mod error;

use serde_json::Value;
use serde_json::value::from_value;
// Re-export error type
pub use error::Error;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// A JSONRPC request object
pub struct Request {
    /// A String containing the name of the method to be invoked
    pub method: String,
    /// An Array of objects to pass as arguments to the method
    pub params: Vec<Value>,
    /// The request id. This can be of any type. It is used to match the
    /// response with the request that it is replying to
    pub id: Value,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// A JSONRPC response object
pub struct Response {
    /// The Object that was returned by the invoked method. This must be null
    /// in case there was a error invoking the method
    pub result: Option<Value>,
    /// An Error object if there was an error invoking the method. It must be
    /// null if there was no error
    pub error: Option<Value>,
    /// This must be the same id as the request it is responding to
    pub id: Value,
}

impl Response {
    /// Extract the result from a response
    pub fn result<T>(&self) -> Result<T, Error>
        where for<'de> T: serde::Deserialize<'de>
    {
        if let Some(ref e) = self.error {
            return Err(Error::Rpc(e.clone()));
        }
        match self.result {
            Some(ref res) => from_value(res.clone()).map_err(Error::Json),
            None => Err(Error::NoErrorOrResult),
        }
    }

    /// Extract the result from a response, consuming the response
    pub fn into_result<T>(self) -> Result<T, Error>
        where for<'de> T: serde::Deserialize<'de>
    {
        if let Some(e) = self.error {
            return Err(Error::Rpc(e));
        }
        match self.result {
            Some(res) => from_value(res).map_err(Error::Json),
            None => Err(Error::NoErrorOrResult),
        }
    }

    /// Return the RPC error, if there was one, but do not check the result
    pub fn check_error(self) -> Result<(), Error> {
        if let Some(e) = self.error {
            Err(Error::Rpc(e))
        } else {
            Ok(())
        }
    }

    /// Returns whether or not the `result` field is empty
    pub fn is_none(&self) -> bool {
        self.result.is_none()
    }
}

#[cfg(test)]
mod tests {
    use super::{Request, Response};
    use super::serde_json::ser;
    use super::serde_json::de;
    use super::serde_json::Value;

    #[test]
    fn request_serialize_round_trip() {
        let original = Request {
            method: "test".to_owned(),
            params: json!([(), false, true, "test2"]).as_array().cloned().unwrap(),
            id: json!("69"),
        };

        let s = ser::to_vec(&original).unwrap();
        let d = de::from_slice(s.as_slice()).unwrap();

        assert_eq!(original, d);
    }

    #[test]
    fn response_is_none() {
        let joanna = Response {
            result: Some(json!(true)),
            error: None,
            id: json!(81),
        };

        let bill = Response {
            result: None,
            error: None,
            id: json!(66),
        };

        assert!(!joanna.is_none());
        assert!(bill.is_none());
    }

    #[test]
    fn response_extract() {
        let obj = vec!["Mary", "had", "a", "little", "lamb"];
        let response = Response {
            result: Some(json!(obj)),
            error: None,
            id: Value::Null,
        };
        let recovered1: Vec<String> = response.result().unwrap();
        assert!(response.clone().check_error().is_ok());
        let recovered2: Vec<String> = response.into_result().unwrap();
        assert_eq!(obj, recovered1);
        assert_eq!(obj, recovered2);
    }
}