Skip to main content

json_rpc/
types.rs

1//! JSON-RPC 2.0 message types.
2//!
3//! This module defines JSON-RPC 2.0 message types as specified in:
4//! https://www.jsonrpc.org/specification
5
6use std::fmt;
7
8use serde::{Deserialize, Serialize};
9use tracing::debug;
10
11use crate::error::Error as InternalError;
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct Request {
15    pub jsonrpc: String,
16    pub id: RequestId,
17    pub method: String,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub params: Option<serde_json::Value>,
20}
21
22impl Request {
23    pub fn new(
24        id: RequestId,
25        method: impl Into<String>,
26        params: Option<serde_json::Value>,
27    ) -> Self {
28        Self {
29            jsonrpc: "2.0".to_string(),
30            id,
31            method: method.into(),
32            params,
33        }
34    }
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct Response {
39    pub jsonrpc: String,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub result: Option<serde_json::Value>,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub error: Option<Error>,
44    pub id: RequestId,
45}
46
47impl Response {
48    pub fn success(id: RequestId, result: serde_json::Value) -> Self {
49        Self {
50            jsonrpc: "2.0".to_string(),
51            id,
52            result: Some(result),
53            error: None,
54        }
55    }
56
57    pub fn error(id: RequestId, error: Error) -> Self {
58        Self {
59            jsonrpc: "2.0".to_string(),
60            id,
61            result: None,
62            error: Some(error),
63        }
64    }
65
66    pub fn validate(&self) -> Result<(), String> {
67        match (&self.result, &self.error) {
68            (Some(_), Some(_)) => Err("Response cannot have both result and error".to_string()),
69            (None, None) => Err("Response must have either result or error".to_string()),
70            _ => Ok(()),
71        }
72    }
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct Notification {
77    pub jsonrpc: String,
78    pub method: String,
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub params: Option<serde_json::Value>,
81}
82
83impl Notification {
84    pub fn new(method: impl Into<String>, params: Option<serde_json::Value>) -> Self {
85        Self {
86            jsonrpc: "2.0".to_string(),
87            method: method.into(),
88            params,
89        }
90    }
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct Error {
95    pub code: i32,
96    pub message: String,
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub data: Option<serde_json::Value>,
99}
100
101impl Error {
102    pub fn new(code: i32, message: impl Into<String>, data: Option<serde_json::Value>) -> Self {
103        Self {
104            code,
105            message: message.into(),
106            data,
107        }
108    }
109
110    pub fn parse_error(message: impl Into<String>) -> Self {
111        Self::new(-32700, message, None)
112    }
113
114    pub fn invalid_request(message: impl Into<String>) -> Self {
115        Self::new(-32600, message, None)
116    }
117
118    pub fn method_not_found(message: impl Into<String>) -> Self {
119        Self::new(-32601, message, None)
120    }
121
122    pub fn invalid_params(message: impl Into<String>) -> Self {
123        Self::new(-32602, message, None)
124    }
125
126    pub fn internal_error(message: impl Into<String>) -> Self {
127        Self::new(-32603, message, None)
128    }
129}
130
131impl fmt::Display for Error {
132    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133        write!(f, "JSON-RPC error {}: {}", self.code, self.message)
134    }
135}
136
137#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
138#[serde(untagged)]
139pub enum RequestId {
140    Null,
141    Number(u64),
142    String(String),
143}
144
145impl fmt::Display for RequestId {
146    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147        match self {
148            RequestId::Null => write!(f, "null"),
149            RequestId::Number(n) => write!(f, "{}", n),
150            RequestId::String(s) => write!(f, "{}", s),
151        }
152    }
153}
154
155#[derive(Debug, Clone)]
156pub enum Message {
157    Request(Request),
158    Response(Response),
159    Notification(Notification),
160    Batch(Vec<Message>),
161}
162
163impl Message {
164    pub fn from_json(value: serde_json::Value) -> Result<Self, InternalError> {
165        debug!("Parsing JSON value: {:?}", value);
166        if let Some(arr) = value.as_array() {
167            return Self::parse_batch(arr);
168        }
169
170        Self::parse_single(value)
171    }
172
173    fn parse_batch(arr: &[serde_json::Value]) -> Result<Self, InternalError> {
174        if arr.is_empty() {
175            return Err(InternalError::invalid_request("Invalid Request"));
176        }
177
178        let messages: Result<Vec<Message>, InternalError> =
179            arr.iter().map(Self::parse_batch_item).collect();
180
181        Ok(Message::Batch(messages?))
182    }
183
184    fn parse_batch_item(item: &serde_json::Value) -> Result<Self, InternalError> {
185        match Self::parse_single(item.clone()) {
186            Ok(msg) => Ok(msg),
187            Err(_) => Ok(Message::Response(Response::error(
188                RequestId::Null,
189                Error::invalid_request("Invalid Request"),
190            ))),
191        }
192    }
193
194    fn parse_single(value: serde_json::Value) -> Result<Self, InternalError> {
195        let has_id = value.get("id").is_some();
196
197        if has_id {
198            if value.get("error").is_some() {
199                let resp: Response = serde_json::from_value(value)
200                    .map_err(|_| InternalError::invalid_request("Invalid Request"))?;
201                resp.validate()
202                    .map_err(|_| InternalError::invalid_request("Invalid Request"))?;
203                Ok(Message::Response(resp))
204            } else if value.get("method").is_some() {
205                let req: Request = serde_json::from_value(value)
206                    .map_err(|_| InternalError::invalid_request("Invalid Request"))?;
207                if req.jsonrpc != "2.0" {
208                    return Err(InternalError::invalid_request("Invalid Request"));
209                }
210
211                Ok(Message::Request(req))
212            } else {
213                Err(InternalError::invalid_request("Invalid Request"))
214            }
215        } else {
216            let notif: Notification = serde_json::from_value(value)
217                .map_err(|_| InternalError::invalid_request("Invalid Request"))?;
218            if notif.jsonrpc != "2.0" {
219                return Err(InternalError::invalid_request("Invalid Request"));
220            }
221
222            Ok(Message::Notification(notif))
223        }
224    }
225
226    pub fn to_json(&self) -> Result<serde_json::Value, serde_json::Error> {
227        match self {
228            Message::Request(req) => serde_json::to_value(req),
229            Message::Response(res) => serde_json::to_value(res),
230            Message::Notification(notif) => serde_json::to_value(notif),
231            Message::Batch(messages) => {
232                let json_array: Result<Vec<serde_json::Value>, serde_json::Error> =
233                    messages.iter().map(|m| m.to_json()).collect();
234                Ok(serde_json::Value::Array(json_array?))
235            }
236        }
237    }
238
239    pub fn id(&self) -> Option<&RequestId> {
240        match self {
241            Message::Request(req) => Some(&req.id),
242            Message::Response(res) => Some(&res.id),
243            Message::Notification(_) => None,
244            Message::Batch(_) => None,
245        }
246    }
247
248    pub fn is_request(&self) -> bool {
249        matches!(self, Message::Request(_))
250    }
251
252    pub fn is_response(&self) -> bool {
253        matches!(self, Message::Response(_))
254    }
255
256    pub fn is_notification(&self) -> bool {
257        matches!(self, Message::Notification(_))
258    }
259
260    pub fn is_batch(&self) -> bool {
261        matches!(self, Message::Batch(_))
262    }
263}