json_rpc_server/
types.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct RPCRequest<T>
7where
8    T: Clone,
9{
10    pub jsonrpc: String,
11    pub method: String,
12    pub params: T,
13    pub id: Value,
14}
15
16impl<T> RPCRequest<T>
17where
18    T: Clone,
19{
20    pub fn new(method: &str, params: T) -> Self {
21        Self {
22            jsonrpc: String::from("2.0"),
23            method: String::from(method),
24            params,
25            id: Value::from(1),
26        }
27    }
28}
29
30#[derive(Debug, Clone, Deserialize, Serialize)]
31pub struct RPCResponse<T> {
32    pub jsonrpc: String,
33    pub result: Option<T>,
34    pub error: Option<RPCError>,
35    pub id: Value,
36}
37
38#[derive(Debug, Deserialize, Serialize)]
39struct RPCResponseResult<T> {
40    pub jsonrpc: String,
41    pub result: Option<T>,
42    pub id: Value,
43}
44
45#[derive(Debug, Deserialize, Serialize)]
46struct RPCResponseError {
47    pub jsonrpc: String,
48    pub error: RPCError,
49    pub id: Value,
50}
51
52impl<T> RPCResponse<T> {
53    pub fn result(id: Value, t: Option<T>) -> Self {
54        Self {
55            jsonrpc: String::from("2.0"),
56            result: t,
57            error: None,
58            id,
59        }
60    }
61
62    pub fn error(id: Value, e: RPCError) -> Self {
63        Self {
64            jsonrpc: String::from("2.0"),
65            result: None,
66            error: Some(e),
67            id,
68        }
69    }
70}
71impl<T> RPCResponse<T>
72where
73    T: Serialize,
74{
75    pub fn into_value(self) -> Result<Value> {
76        if let Some(e) = self.error {
77            let v = RPCResponseError {
78                id: self.id,
79                jsonrpc: self.jsonrpc,
80                error: e,
81            };
82            Ok(serde_json::to_value(v)?)
83        } else {
84            let v = RPCResponseResult {
85                id: self.id,
86                jsonrpc: self.jsonrpc,
87                result: self.result,
88            };
89
90            Ok(serde_json::to_value(v)?)
91        }
92    }
93}
94
95#[derive(Debug, Clone, Deserialize, Serialize)]
96pub struct RPCError {
97    pub code: i32,
98    pub message: String,
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub data: Option<String>,
101}
102
103impl RPCError {
104    pub fn unknown_method() -> Self {
105        RPCError {
106            code: -32601,
107            message: String::from("Method not found"),
108            data: None,
109        }
110    }
111
112    pub fn parse_error() -> Self {
113        Self {
114            code: -32700,
115            message: String::from("Parse error"),
116            data: None,
117        }
118    }
119
120    pub fn invalid_params() -> Self {
121        Self {
122            code: -32602,
123            message: String::from("Invalid params"),
124            data: None,
125        }
126    }
127
128    pub fn internal_error(data: String) -> Self {
129        Self {
130            code: -32603,
131            message: String::from("Internal error"),
132            data: Some(data),
133        }
134    }
135}
136
137pub type RPCResult<T> = std::result::Result<T, RPCError>;