forked from informalsystems/tendermint-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.rs
80 lines (65 loc) · 2.11 KB
/
response.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! JSON-RPC response types
use std::io::Read;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{response_error::ResponseError, Error, Id, Version};
/// JSON-RPC responses
pub trait Response: DeserializeOwned + Sized {
/// Parse a JSON-RPC response from a JSON string
fn from_string(response: impl AsRef<[u8]>) -> Result<Self, Error> {
let wrapper: Wrapper<Self> =
serde_json::from_slice(response.as_ref()).map_err(Error::serde)?;
wrapper.into_result()
}
/// Parse a JSON-RPC response from an `io::Reader`
fn from_reader(reader: impl Read) -> Result<Self, Error> {
let wrapper: Wrapper<Self> = serde_json::from_reader(reader).map_err(Error::serde)?;
wrapper.into_result()
}
}
/// JSON-RPC response wrapper (i.e. message envelope)
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Wrapper<R> {
/// JSON-RPC version
jsonrpc: Version,
/// Identifier included in request
id: Id,
/// Results of request (if successful)
result: Option<R>,
/// Error message if unsuccessful
error: Option<ResponseError>,
}
impl<R> Wrapper<R> {
/// Get JSON-RPC version
pub fn version(&self) -> &Version {
&self.jsonrpc
}
/// Get JSON-RPC ID
#[allow(dead_code)]
pub fn id(&self) -> &Id {
&self.id
}
/// Convert this wrapper into the underlying error, if any
pub fn into_error(self) -> Option<Error> {
self.error.map(Error::response)
}
/// Convert this wrapper into a result type
pub fn into_result(self) -> Result<R, Error> {
// Ensure we're using a supported RPC version
self.version().ensure_supported()?;
if let Some(e) = self.error {
Err(Error::response(e))
} else if let Some(result) = self.result {
Ok(result)
} else {
Err(Error::malformed_json())
}
}
pub fn new_with_id(id: Id, result: Option<R>, error: Option<ResponseError>) -> Self {
Self {
jsonrpc: Version::current(),
id,
result,
error,
}
}
}