rpc_json_client/
types.rs

1use crate::error::{Error, RpcError};
2use serde_derive::Deserialize;
3use serde_derive::Serialize;
4
5//TODO figure out if we can wrap this around the Request struct in a main lib (similar to the error
6//type, and then just add on serialization in this specific repo.
7#[derive(Debug, Clone, PartialEq, Serialize)]
8pub struct RpcRequest {
9    pub method: String,
10    pub params: Vec<serde_json::Value>,
11    pub id: serde_json::Value,
12    pub jsonrpc: Option<String>,
13}
14
15//TODO see above
16#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
17pub struct RpcResponse {
18    pub result: Option<serde_json::Value>,
19    pub error: Option<RpcError>,
20    pub id: serde_json::Value,
21    pub jsonrpc: Option<String>,
22}
23
24impl RpcResponse {
25    // pub fn result<T: serde::de::DeserializeOwned>(&self) -> Result<T, RpcError> {
26    //     if let Some(ref e) = self.error {
27    //         return Err(Error::Rpc(e.clone()));
28    //     }
29
30    //     serde_json::from_value(self.result.clone().unwrap_or(serde_json::Value::Null))
31    //         .map_err(Error::Json)
32    // }
33
34    pub fn into_result<T: serde::de::DeserializeOwned>(self) -> Result<T, Error> {
35        if let Some(e) = self.error {
36            return Err(Error::Rpc(e));
37        }
38
39        serde_json::from_value(self.result.unwrap_or(serde_json::Value::Null)).map_err(Error::Json)
40    }
41}