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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::error::RpcError;
use alloc::collections::BTreeMap;
use serde::de::{self, Deserializer, Unexpected, Visitor};
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
use serde_json::{value::RawValue, Value as JsonValue};
use std::fmt;
#[derive(Serialize, Deserialize, Debug)]
pub struct JsonRpcNotificationParams<'a> {
pub subscription: u64,
#[serde(borrow)]
pub result: &'a RawValue,
}
#[derive(Deserialize, Debug)]
pub struct JsonRpcNotificationParamsAlloc<T> {
pub subscription: SubscriptionId,
pub result: T,
}
#[derive(Debug, Default, PartialEq)]
pub struct TwoPointZero;
struct TwoPointZeroVisitor;
impl<'de> Visitor<'de> for TwoPointZeroVisitor {
type Value = TwoPointZero;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(r#"a string "2.0""#)
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
match s {
"2.0" => Ok(TwoPointZero),
_ => Err(de::Error::invalid_value(Unexpected::Str(s), &self)),
}
}
}
impl<'de> Deserialize<'de> for TwoPointZero {
fn deserialize<D>(deserializer: D) -> Result<TwoPointZero, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(TwoPointZeroVisitor)
}
}
impl Serialize for TwoPointZero {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str("2.0")
}
}
#[derive(Clone, Copy, Debug)]
pub struct RpcParams<'a>(Option<&'a str>);
impl<'a> RpcParams<'a> {
pub fn new(raw: Option<&'a str>) -> Self {
Self(raw)
}
pub fn parse<T>(self) -> Result<T, RpcError>
where
T: Deserialize<'a>,
{
match self.0 {
None => Err(RpcError::InvalidParams),
Some(params) => serde_json::from_str(params).map_err(|_| RpcError::InvalidParams),
}
}
pub fn one<T>(self) -> Result<T, RpcError>
where
T: Deserialize<'a>,
{
self.parse::<[T; 1]>().map(|[res]| res)
}
}
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum JsonRpcParams<'a> {
NoParams,
Array(Vec<JsonValue>),
ArrayRef(&'a [JsonValue]),
Map(BTreeMap<&'a str, JsonValue>),
}
impl<'a> From<BTreeMap<&'a str, JsonValue>> for JsonRpcParams<'a> {
fn from(map: BTreeMap<&'a str, JsonValue>) -> Self {
Self::Map(map)
}
}
impl<'a> From<Vec<JsonValue>> for JsonRpcParams<'a> {
fn from(arr: Vec<JsonValue>) -> Self {
Self::Array(arr)
}
}
impl<'a> From<&'a [JsonValue]> for JsonRpcParams<'a> {
fn from(slice: &'a [JsonValue]) -> Self {
Self::ArrayRef(slice)
}
}
#[derive(Debug, PartialEq, Clone, Hash, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
#[serde(untagged)]
pub enum SubscriptionId {
Num(u64),
Str(String),
}
impl From<SubscriptionId> for JsonValue {
fn from(sub_id: SubscriptionId) -> Self {
match sub_id {
SubscriptionId::Num(n) => n.into(),
SubscriptionId::Str(s) => s.into(),
}
}
}
#[derive(Debug, PartialEq, Clone, Hash, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
#[serde(untagged)]
pub enum Id {
Null,
Number(u64),
Str(String),
}
impl Id {
pub fn as_number(&self) -> Option<&u64> {
match self {
Id::Number(n) => Some(n),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
Id::Str(s) => Some(s),
_ => None,
}
}
pub fn as_null(&self) -> Option<()> {
match self {
Id::Null => Some(()),
_ => None,
}
}
}