diff --git a/CHANGELOG.md b/CHANGELOG.md index 03ad9f82..bd16a260 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.17.0 - 2023-06-29 + +* `params` field in `Request` changed to a generic `RawValue` instead of an array. + [#108](https://github.com/apoelstra/rust-jsonrpc/pull/108) + # 0.16.0 - 2023-06-29 * Re-export the `minreq` crate when the feature is set diff --git a/README.md b/README.md index ece3c413..1b6ebe98 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ fn client(url: &str, user: &str, pass: &str) -> Result(&self, method: &'a str, params: &'a [Box]) -> Request<'a> { + 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, @@ -115,7 +115,7 @@ impl Client { pub fn call serde::de::Deserialize<'a>>( &self, method: &str, - args: &[Box], + args: Option<&RawValue>, ) -> Result { let request = self.build_request(method, args); let id = request.id.clone(); @@ -224,9 +224,9 @@ mod tests { fn sanity() { let client = Client::with_transport(DummyTransport); assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 1); - let req1 = client.build_request("test", &[]); + let req1 = client.build_request("test", None); assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 2); - let req2 = client.build_request("test", &[]); + let req2 = client.build_request("test", None); assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 3); assert!(req1.id != req2.id); } diff --git a/src/http/simple_http.rs b/src/http/simple_http.rs index 5b216710..e143a62f 100644 --- a/src/http/simple_http.rs +++ b/src/http/simple_http.rs @@ -818,11 +818,11 @@ mod tests { let port = rx.recv().unwrap(); let client = Client::simple_http(format!("localhost:{}", port).as_str(), None, None).unwrap(); - let request = client.build_request("test_request", &[]); + let request = client.build_request("test_request", None); let result = client.send_request(request).unwrap(); assert_eq!(result.id, Value::Number(Number::from(0))); thread::sleep(Duration::from_secs(1)); - let request = client.build_request("test_request2", &[]); + let request = client.build_request("test_request2", None); let result2 = client.send_request(request) .expect("This second request should not be an Err like `Err(Transport(HttpResponseTooShort { actual: 0, needed: 12 }))`"); assert_eq!(result2.id, Value::Number(Number::from(1))); diff --git a/src/lib.rs b/src/lib.rs index 73d19726..4d942adf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,7 @@ pub struct Request<'a> { /// The name of the RPC call. pub method: &'a str, /// Parameters to the RPC call. - pub params: &'a [Box], + pub params: Option<&'a RawValue>, /// Identifier for this request, which should appear in the response. pub id: serde_json::Value, /// jsonrpc field, MUST be "2.0". @@ -123,7 +123,10 @@ impl Response { mod tests { use super::*; - use serde_json::value::RawValue; + use serde_json::{ + json, + value::{to_raw_value, RawValue}, + }; #[test] fn response_is_none() { @@ -219,4 +222,38 @@ mod tests { Test ); } + + #[test] + fn test_request_list() { + let list = json!([0]); + let raw_value = Some(to_raw_value(&list).unwrap()); + + let request = Request { + method: "list", + params: raw_value.as_deref(), + id: serde_json::json!(2), + jsonrpc: Some("2.0"), + }; + assert_eq!( + serde_json::to_string(&request).unwrap(), + r#"{"method":"list","params":[0],"id":2,"jsonrpc":"2.0"}"# + ); + } + + #[test] + fn test_request_object() { + let object = json!({ "height": 0 }); + let raw_value = Some(to_raw_value(&object).unwrap()); + + let request = Request { + method: "object", + params: raw_value.as_deref(), + id: serde_json::json!(2), + jsonrpc: Some("2.0"), + }; + assert_eq!( + serde_json::to_string(&request).unwrap(), + r#"{"method":"object","params":{"height":0},"id":2,"jsonrpc":"2.0"}"# + ); + } } diff --git a/src/simple_tcp.rs b/src/simple_tcp.rs index 27130e4e..f36600b0 100644 --- a/src/simple_tcp.rs +++ b/src/simple_tcp.rs @@ -134,7 +134,7 @@ mod tests { let addr = server.local_addr().unwrap(); let dummy_req = Request { method: "arandommethod", - params: &[], + params: None, id: serde_json::Value::Number(4242242.into()), jsonrpc: Some("2.0"), }; diff --git a/src/simple_uds.rs b/src/simple_uds.rs index a1afdf58..d05c48fd 100644 --- a/src/simple_uds.rs +++ b/src/simple_uds.rs @@ -137,7 +137,7 @@ mod tests { let server = UnixListener::bind(&socket_path).unwrap(); let dummy_req = Request { method: "getinfo", - params: &[], + params: None, id: serde_json::Value::Number(111.into()), jsonrpc: Some("2.0"), };