Skip to main content

actrpc_core/
json_rpc.rs

1use serde::{Deserialize, Deserializer, Serialize, de::Error};
2use serde_json::{Map, Number, Value};
3
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5#[serde(untagged)]
6pub enum JsonRpcMessage {
7    Single(JsonRpcSingleMessage),
8    Batch(JsonRpcBatch),
9}
10
11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12#[serde(untagged)]
13pub enum JsonRpcSingleMessage {
14    Request(JsonRpcRequest),
15    Notification(JsonRpcNotification),
16    Response(JsonRpcResponse),
17}
18
19#[derive(Debug, Clone, PartialEq, Serialize)]
20#[serde(transparent)]
21pub struct JsonRpcBatch(pub Vec<JsonRpcSingleMessage>);
22
23impl<'de> Deserialize<'de> for JsonRpcBatch {
24    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
25    where
26        D: Deserializer<'de>,
27    {
28        let items = Vec::<JsonRpcSingleMessage>::deserialize(deserializer)?;
29        if items.is_empty() {
30            return Err(Error::custom("JSON-RPC batch must not be an empty array"));
31        }
32        Ok(Self(items))
33    }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
37pub enum JsonRpcVersion {
38    #[serde(rename = "2.0")]
39    #[default]
40    V2_0,
41}
42
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum JsonRpcId {
46    String(String),
47    Number(Number),
48    Null,
49}
50
51#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum JsonRpcParams {
54    Array(Vec<Value>),
55    Object(Map<String, Value>),
56}
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59#[serde(deny_unknown_fields)]
60pub struct JsonRpcRequest {
61    #[serde(default)]
62    pub jsonrpc: JsonRpcVersion,
63    pub id: JsonRpcId,
64    pub method: String,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub params: Option<JsonRpcParams>,
67}
68
69#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
70#[serde(deny_unknown_fields)]
71pub struct JsonRpcNotification {
72    #[serde(default)]
73    pub jsonrpc: JsonRpcVersion,
74    pub method: String,
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub params: Option<JsonRpcParams>,
77}
78
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80#[serde(untagged)]
81pub enum JsonRpcResponse {
82    Success(JsonRpcSuccessResponse),
83    Error(JsonRpcErrorResponse),
84}
85
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87#[serde(deny_unknown_fields)]
88pub struct JsonRpcSuccessResponse {
89    #[serde(default)]
90    pub jsonrpc: JsonRpcVersion,
91    pub id: JsonRpcId,
92    pub result: Value,
93}
94
95#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
96#[serde(deny_unknown_fields)]
97pub struct JsonRpcErrorResponse {
98    #[serde(default)]
99    pub jsonrpc: JsonRpcVersion,
100    pub id: JsonRpcId,
101    pub error: JsonRpcError,
102}
103
104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
105#[serde(deny_unknown_fields)]
106pub struct JsonRpcError {
107    pub code: i32,
108    pub message: String,
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub data: Option<Value>,
111}