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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::borrow::Cow;
use serde::{
de::{DeserializeOwned, Deserializer},
Deserialize, Serialize,
};
use serde_json::Value;
use tracing::error;
use super::{Error, JSON_RPC_VERSION};
/// A JSON-RPC response.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields, untagged)]
pub enum Response {
/// A successful RPC execution.
Success {
/// The JSON-RPC version field.
#[serde(deserialize_with = "set_jsonrpc_field")]
jsonrpc: Cow<'static, str>,
/// The same ID as was passed in the corresponding request.
id: Value,
/// The successful result of executing the RPC.
result: Value,
},
/// An RPC execution which failed.
Failure {
/// The JSON-RPC version field.
#[serde(deserialize_with = "set_jsonrpc_field")]
jsonrpc: Cow<'static, str>,
/// The same ID as was passed in the corresponding request.
id: Value,
/// The error encountered while executing the RPC.
error: Error,
},
}
impl Response {
/// Returns a new `Response::Success`.
pub fn new_success(id: Value, result: Value) -> Self {
Response::Success {
jsonrpc: Cow::Borrowed(JSON_RPC_VERSION),
id,
result,
}
}
/// Returns a new `Response::Failure`.
pub fn new_failure(id: Value, error: Error) -> Self {
Response::Failure {
jsonrpc: Cow::Borrowed(JSON_RPC_VERSION),
id,
error,
}
}
/// Returns `true` is this is a `Response::Success`.
pub fn is_success(&self) -> bool {
matches!(self, Response::Success { .. })
}
/// Returns `true` is this is a `Response::Failure`.
pub fn is_failure(&self) -> bool {
matches!(self, Response::Failure { .. })
}
/// Returns the "result" field, or `None` if this is a `Response::Failure`.
pub fn raw_result(&self) -> Option<&Value> {
match &self {
Response::Success { result, .. } => Some(result),
Response::Failure { .. } => None,
}
}
/// Returns the "result" field parsed as `T`, or `None` if this is a `Response::Failure` or if
/// parsing fails.
pub fn result<T: DeserializeOwned>(&self) -> Option<T> {
match &self {
Response::Success { result, .. } => serde_json::from_value(result.clone())
.map_err(|error| {
error!("failed to parse: {}", error);
})
.ok(),
Response::Failure { .. } => None,
}
}
/// Returns the "error" field or `None` if this is a `Response::Success`.
pub fn error(&self) -> Option<&Error> {
match &self {
Response::Success { .. } => None,
Response::Failure { error, .. } => Some(error),
}
}
/// Returns the "id" field.
pub fn id(&self) -> &Value {
match &self {
Response::Success { id, .. } | Response::Failure { id, .. } => id,
}
}
}
fn set_jsonrpc_field<'de, D: Deserializer<'de>>(
_deserializer: D,
) -> Result<Cow<'static, str>, D::Error> {
Ok(Cow::Borrowed(JSON_RPC_VERSION))
}