use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::atomic;
use serde_json::value::RawValue;
use serde_json::Value;
use crate::error::Error;
use crate::{Request, Response};
pub trait Transport: Send + Sync + 'static {
fn send_request(&self, _: Request) -> Result<Response, Error>;
fn send_batch(&self, _: &[Request]) -> Result<Vec<Response>, Error>;
fn fmt_target(&self, f: &mut fmt::Formatter) -> fmt::Result;
}
pub struct Client {
pub(crate) transport: Box<dyn Transport>,
nonce: atomic::AtomicUsize,
}
impl Client {
pub fn with_transport<T: Transport>(transport: T) -> Client {
Client {
transport: Box::new(transport),
nonce: atomic::AtomicUsize::new(1),
}
}
pub fn build_request<'a>(&self, method: &'a str, params: Option<&'a RawValue>) -> Request<'a> {
let nonce = self.nonce.fetch_add(1, atomic::Ordering::Relaxed);
Request {
method,
params,
id: serde_json::Value::from(nonce),
jsonrpc: Some("2.0"),
}
}
pub fn send_request(&self, request: Request) -> Result<Response, Error> {
self.transport.send_request(request)
}
pub fn send_batch(&self, requests: &[Request]) -> Result<Vec<Option<Response>>, Error> {
if requests.is_empty() {
return Err(Error::EmptyBatch);
}
let responses = self.transport.send_batch(requests)?;
if responses.len() > requests.len() {
return Err(Error::WrongBatchResponseSize);
}
let mut by_id = HashMap::with_capacity(requests.len());
for resp in responses.into_iter() {
let id = HashableValue(Cow::Owned(resp.id.clone()));
if let Some(dup) = by_id.insert(id, resp) {
return Err(Error::BatchDuplicateResponseId(dup.id));
}
}
let results =
requests.iter().map(|r| by_id.remove(&HashableValue(Cow::Borrowed(&r.id)))).collect();
if let Some(id) = by_id.keys().next() {
return Err(Error::WrongBatchResponseId((*id.0).clone()));
}
Ok(results)
}
pub fn call<R: for<'a> serde::de::Deserialize<'a>>(
&self,
method: &str,
args: Option<&RawValue>,
) -> Result<R, Error> {
let request = self.build_request(method, args);
let id = request.id.clone();
let response = self.send_request(request)?;
if response.jsonrpc.is_some() && response.jsonrpc != Some(From::from("2.0")) {
return Err(Error::VersionMismatch);
}
if response.id != id {
return Err(Error::NonceMismatch);
}
response.result()
}
}
impl fmt::Debug for crate::Client {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "jsonrpc::Client(")?;
self.transport.fmt_target(f)?;
write!(f, ")")
}
}
impl<T: Transport> From<T> for Client {
fn from(t: T) -> Client {
Client::with_transport(t)
}
}
#[derive(Clone, PartialEq, Debug)]
struct HashableValue<'a>(pub Cow<'a, Value>);
impl<'a> Eq for HashableValue<'a> {}
impl<'a> Hash for HashableValue<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
match *self.0.as_ref() {
Value::Null => "null".hash(state),
Value::Bool(false) => "false".hash(state),
Value::Bool(true) => "true".hash(state),
Value::Number(ref n) => {
"number".hash(state);
if let Some(n) = n.as_i64() {
n.hash(state);
} else if let Some(n) = n.as_u64() {
n.hash(state);
} else {
n.to_string().hash(state);
}
}
Value::String(ref s) => {
"string".hash(state);
s.hash(state);
}
Value::Array(ref v) => {
"array".hash(state);
v.len().hash(state);
for obj in v {
HashableValue(Cow::Borrowed(obj)).hash(state);
}
}
Value::Object(ref m) => {
"object".hash(state);
m.len().hash(state);
for (key, val) in m {
key.hash(state);
HashableValue(Cow::Borrowed(val)).hash(state);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::borrow::Cow;
use std::collections::HashSet;
use std::str::FromStr;
use std::sync;
struct DummyTransport;
impl Transport for DummyTransport {
fn send_request(&self, _: Request) -> Result<Response, Error> {
Err(Error::NonceMismatch)
}
fn send_batch(&self, _: &[Request]) -> Result<Vec<Response>, Error> {
Ok(vec![])
}
fn fmt_target(&self, _: &mut fmt::Formatter) -> fmt::Result {
Ok(())
}
}
#[test]
fn sanity() {
let client = Client::with_transport(DummyTransport);
assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 1);
let req1 = client.build_request("test", None);
assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 2);
let req2 = client.build_request("test", None);
assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 3);
assert!(req1.id != req2.id);
}
#[test]
fn hash_value() {
let val = HashableValue(Cow::Owned(Value::from_str("null").unwrap()));
let t = HashableValue(Cow::Owned(Value::from_str("true").unwrap()));
let f = HashableValue(Cow::Owned(Value::from_str("false").unwrap()));
let ns =
HashableValue(Cow::Owned(Value::from_str("[0, -0, 123.4567, -100000000]").unwrap()));
let m =
HashableValue(Cow::Owned(Value::from_str("{ \"field\": 0, \"field\": -0 }").unwrap()));
let mut coll = HashSet::new();
assert!(!coll.contains(&val));
coll.insert(val.clone());
assert!(coll.contains(&val));
assert!(!coll.contains(&t));
assert!(!coll.contains(&f));
coll.insert(t.clone());
assert!(coll.contains(&t));
assert!(!coll.contains(&f));
coll.insert(f.clone());
assert!(coll.contains(&t));
assert!(coll.contains(&f));
assert!(!coll.contains(&ns));
coll.insert(ns.clone());
assert!(coll.contains(&ns));
assert!(!coll.contains(&m));
coll.insert(m.clone());
assert!(coll.contains(&m));
}
}