From 177e2329a5cb67009f28999d667f315311b4514d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Wed, 7 Mar 2018 13:35:47 +0100 Subject: [PATCH] Inline check_response --- core/src/response.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/core/src/response.rs b/core/src/response.rs index b85eebf..58ee026 100644 --- a/core/src/response.rs +++ b/core/src/response.rs @@ -19,29 +19,22 @@ where { let response: Output = serde_json::from_slice(response_raw) .chain_err(|| ErrorKind::ResponseError("Not valid json"))?; + ensure!( + response.version() == Some(Version::V2), + ErrorKind::ResponseError("Not JSON-RPC 2.0 compatible") + ); + ensure!( + response.id() == &expected_id, + ErrorKind::ResponseError("Response id not equal to request id") + ); match response { Output::Success(success) => { - check_response(success.jsonrpc, success.id, expected_id)?; trace!("Received json result: {}", success.result); serde_json::from_value::(success.result) .chain_err(|| ErrorKind::ResponseError("Not valid for target type")) } Output::Failure(failure) => { - check_response(failure.jsonrpc, failure.id, expected_id)?; Err(ErrorKind::JsonRpcError(failure.error).into()) } } } - -/// Validate if response is a valid JSON-RPC 2.0 response object with the correct Id. -fn check_response(version: Option, id: Id, expected_id: Id) -> Result<()> { - ensure!( - version == Some(Version::V2), - ErrorKind::ResponseError("Not JSON-RPC 2.0 compatible") - ); - ensure!( - id == expected_id, - ErrorKind::ResponseError("Response id not equal to request id") - ); - Ok(()) -}