forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonrpc.rs
88 lines (83 loc) · 2.66 KB
/
jsonrpc.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
81
82
83
84
85
86
87
88
//! Common structs to handle JSON-RPC decoding and encoding. They are
//! generic over the Notification and Request types.
use serde::ser::{SerializeStruct, Serializer};
use serde::de::{self, Deserializer};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Debug;
#[derive(Debug)]
pub enum JsonRpc<N, R> {
Request(serde_json::Value, R),
Notification(N),
}
/// This function disentangles the various cases:
///
/// 1) If we have an `id` then it is a request
///
/// 2) Otherwise it's a notification that doesn't require a
/// response.
///
/// Furthermore we distinguish between the built-in types and the
/// custom user notifications/methods:
///
/// 1) We either match a built-in type above,
///
/// 2) Or it's a custom one, so we pass it around just as a
/// `serde_json::Value`
impl<'de, N, R> Deserialize<'de> for JsonRpc<N, R>
where
N: Deserialize<'de> + Debug,
R: Deserialize<'de> + Debug,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize, Debug)]
struct IdHelper {
id: Option<serde_json::Value>,
}
let v = Value::deserialize(deserializer)?;
let helper = IdHelper::deserialize(&v).map_err(de::Error::custom)?;
match helper.id {
Some(id) => {
let r = R::deserialize(v).map_err(de::Error::custom)?;
Ok(JsonRpc::Request(id, r))
}
None => {
let n = N::deserialize(v).map_err(de::Error::custom)?;
Ok(JsonRpc::Notification(n))
}
}
}
}
impl<N, R> Serialize for JsonRpc<N, R>
where
N: Serialize + Debug,
R: Serialize + Debug,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
JsonRpc::Notification(r) => {
let r = serde_json::to_value(r).unwrap();
let mut s = serializer.serialize_struct("Notification", 3)?;
s.serialize_field("jsonrpc", "2.0")?;
s.serialize_field("method", &r["method"])?;
s.serialize_field("params", &r["params"])?;
s.end()
}
JsonRpc::Request(id, r) => {
let r = serde_json::to_value(r).unwrap();
let mut s = serializer.serialize_struct("Request", 4)?;
s.serialize_field("jsonrpc", "2.0")?;
s.serialize_field("id", id)?;
s.serialize_field("method", &r["method"])?;
s.serialize_field("params", &r["params"])?;
s.end()
}
}
}
}