Newer
Older
use crate::server::{RpcError, RpcParams};
use serde::de::{self, Deserializer, Unexpected, Visitor};
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use std::fmt;
use tokio::sync::mpsc;
pub type ConnectionId = usize;
pub type RpcSender<'a> = &'a mpsc::UnboundedSender<String>;
pub type RpcId<'a> = Option<&'a RawValue>;
pub type Method = Box<dyn Send + Sync + Fn(RpcId, RpcParams, RpcSender, ConnectionId) -> anyhow::Result<()>>;
pub type Methods = FxHashMap<&'static str, Method>;
pub trait RpcMethod<R>: Fn(RpcParams) -> Result<R, RpcError> + Send + Sync + 'static {}
impl<R, T> RpcMethod<R> for T where T: Fn(RpcParams) -> Result<R, RpcError> + Send + Sync + 'static {}
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
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct JsonRpcRequest<'a> {
pub jsonrpc: TwoPointZero,
#[serde(borrow)]
pub id: Option<&'a RawValue>,
#[serde(borrow)]
pub method: Cow<'a, str>,
#[serde(borrow)]
pub params: Option<&'a RawValue>,
}
#[derive(Deserialize, Debug)]
pub struct JsonRpcInvalidRequest<'a> {
#[serde(borrow)]
pub id: Option<&'a RawValue>,
}
#[derive(Serialize, Debug)]
pub struct JsonRpcNotification<'a> {
pub jsonrpc: TwoPointZero,
pub method: &'a str,
pub params: JsonRpcNotificationParams<'a>,
}
#[derive(Serialize, Debug)]
pub struct JsonRpcNotificationParams<'a> {
pub subscription: u64,
pub result: &'a RawValue,
}
#[derive(Serialize, Debug)]
pub struct JsonRpcResponse<'a, T> {
pub jsonrpc: TwoPointZero,
pub result: T,
pub id: Option<&'a RawValue>,
}
#[derive(Serialize, Debug)]
pub struct JsonRpcError<'a> {
pub jsonrpc: TwoPointZero,
pub error: JsonRpcErrorParams<'a>,
pub id: Option<&'a RawValue>,
}
#[derive(Serialize, Debug)]
pub struct JsonRpcErrorParams<'a> {
pub code: i32,
pub message: &'a str,
}
#[derive(Debug, Default)]
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")
}
}