Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Upgrade serde to 1.0.
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Pierre Dudey <[email protected]>
  • Loading branch information
jeandudey committed Jul 30, 2018
1 parent 8a50735 commit 2d10b1e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 368 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ rust:
- stable
- beta
- nightly

- 1.17.0
26 changes: 13 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

[package]
name = "jsonrpc"
version = "0.8.0"
authors = ["Andrew Poelstra <[email protected]>"]
license = "CC0-1.0"
homepage = "https://github.com/apoelstra/rust-jsonrpc/"
repository = "https://github.com/apoelstra/rust-jsonrpc/"
documentation = "https://www.wpsoftware.net/rustdoc/jsonrpc/"
description = "Rust support for the JSON-RPC 2.0 protocol"
keywords = [ "protocol", "json", "http", "jsonrpc" ]
readme = "README.md"
name = "jsonrpc"
version = "0.9.0"
authors = ["Andrew Poelstra <[email protected]>"]
license = "CC0-1.0"
homepage = "https://github.com/apoelstra/rust-jsonrpc/"
repository = "https://github.com/apoelstra/rust-jsonrpc/"
documentation = "https://www.wpsoftware.net/rustdoc/jsonrpc/"
description = "Rust support for the JSON-RPC 2.0 protocol"
keywords = [ "protocol", "json", "http", "jsonrpc" ]
readme = "README.md"

[lib]
name = "jsonrpc"
path = "src/lib.rs"

[dependencies]
serde = "0.6"
strason = "0.3"
serde = "1"
serde_derive = "1"
strason = "0.4"

[dependencies.hyper]
version = "0.9"
Expand Down
8 changes: 3 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::sync::{Arc, Mutex};
use hyper::client::Client as HyperClient;
use hyper::header::{Headers, Authorization, Basic};
use hyper;
use strason::{self, Json};
use strason::Json;

use super::{Request, Response};
use error::Error;
Expand Down Expand Up @@ -57,8 +57,7 @@ impl Client {
/// Sends a request to a client
pub fn send_request(&self, request: &Request) -> Result<Response, Error> {
// Build request
let request_json = try!(strason::from_serialize(request));
let request_raw = request_json.to_bytes();
let request_raw = Json::from_serialize(request)?.to_bytes();

// Setup connection
let mut headers = Headers::new();
Expand Down Expand Up @@ -94,9 +93,8 @@ impl Client {

// nb we ignore stream.status since we expect the body
// to contain information about any error
let response_json = try!(Json::from_reader(&mut stream));
let response: Response = Json::from_reader(&mut stream)?.into_deserialize()?;
stream.bytes().count(); // Drain the stream so it can be reused
let response: Response = try!(response_json.into_deserialize());
if response.jsonrpc != None &&
response.jsonrpc != Some(From::from("2.0")) {
return Err(Error::VersionMismatch);
Expand Down
10 changes: 1 addition & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use std::{error, fmt};

use hyper;
use serde;
use strason::{self, Json};

use Response;
Expand Down Expand Up @@ -123,7 +122,7 @@ pub enum StandardError {
InternalError
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
/// A JSONRPC error object
pub struct RpcError {
/// The integer identifier of the error
Expand Down Expand Up @@ -173,13 +172,6 @@ pub fn result_to_response(result: Result<Json, RpcError>, id: Json) -> Response
}
}

serde_struct_impl!(
RpcError,
code,
message,
data
);

#[cfg(test)]
mod tests {
use super::StandardError::{ParseError, InvalidRequest, MethodNotFound, InvalidParams, InternalError};
Expand Down
39 changes: 13 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@
#![warn(missing_docs)]

extern crate hyper;

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

#[macro_use] mod macros;
pub mod client;
pub mod error;

use strason::Json;
// Re-export error type
pub use error::Error;

#[derive(Clone, Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
/// A JSONRPC request object
pub struct Request {
/// The name of the RPC call
Expand All @@ -54,7 +56,7 @@ pub struct Request {
pub jsonrpc: Option<String>
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
/// A JSONRPC response object
pub struct Response {
/// A result if there is one, or null
Expand All @@ -69,7 +71,7 @@ pub struct Response {

impl Response {
/// Extract the result from a response
pub fn result<T: serde::Deserialize>(&self) -> Result<T, Error> {
pub fn result<T: serde::de::DeserializeOwned>(&self) -> Result<T, Error> {
if let Some(ref e) = self.error {
return Err(Error::Rpc(e.clone()));
}
Expand All @@ -80,12 +82,13 @@ impl Response {
}

/// Extract the result from a response, consuming the response
pub fn into_result<T: serde::Deserialize>(self) -> Result<T, Error> {
pub fn into_result<T: serde::de::DeserializeOwned>(self) -> Result<T, Error> {
if let Some(e) = self.error {
return Err(Error::Rpc(e));
}

match self.result {
Some(res) => res.into_deserialize().map_err(Error::Json),
Some(ref res) => res.clone().into_deserialize().map_err(Error::Json),
None => Err(Error::NoErrorOrResult)
}
}
Expand All @@ -103,27 +106,11 @@ impl Response {
pub fn is_none(&self) -> bool { self.result.is_none() }
}

serde_struct_impl!(
Request,
method,
params,
id,
jsonrpc
);

serde_struct_impl!(
Response,
result,
error,
id,
jsonrpc
);

#[cfg(test)]
mod tests {
use super::{Request, Response};
use super::error::RpcError;
use strason::{self, Json};
use strason::Json;

#[test]
fn request_serialize_round_trip() {
Expand All @@ -137,7 +124,7 @@ mod tests {
jsonrpc: Some(String::from("2.0"))
};

let ser = strason::from_serialize(&original).unwrap();
let ser = Json::from_serialize(&original).unwrap();
let des = ser.into_deserialize().unwrap();

assert_eq!(original, des);
Expand All @@ -161,7 +148,7 @@ mod tests {
jsonrpc: Some(String::from("2.0"))
};

let ser = strason::from_serialize(&original).unwrap();
let ser = Json::from_serialize(&original).unwrap();
let des = ser.into_deserialize().unwrap();

assert_eq!(original, des);
Expand Down Expand Up @@ -191,7 +178,7 @@ mod tests {
fn response_extract() {
let obj = vec!["Mary", "had", "a", "little", "lamb"];
let response = Response {
result: Some(strason::from_serialize(&obj).unwrap()),
result: Some(Json::from_serialize(&obj).unwrap()),
error: None,
id: From::from(()),
jsonrpc: Some(String::from("2.0"))
Expand Down
Loading

0 comments on commit 2d10b1e

Please sign in to comment.