1use 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 let value_ref = &value;
167
168 if let Some(arr) = value_ref.as_array() {
169 debug!("Detected batch request with {} items", arr.len());
170 if arr.is_empty() {
171 debug!("Empty array detected - returning Invalid Request error");
172 return Err(InternalError::invalid_request("Invalid Request"));
173 }
174
175 let mut messages = Vec::new();
176 for (index, item) in arr.iter().enumerate() {
177 debug!("Processing batch item {}: {:?}", index, item);
178 match Self::from_json_internal(item.clone()) {
179 Ok(msg) => {
180 debug!("Batch item {} parsed successfully", index);
181 messages.push(msg);
182 }
183 Err(e) => {
184 debug!("Batch item {} failed to parse: {:?}", index, e);
185 let id = item.get("id").and_then(|id_value| match id_value {
186 serde_json::Value::Null => Some(RequestId::Null),
187 serde_json::Value::Number(n) => n.as_u64().map(RequestId::Number),
188 serde_json::Value::String(s) => Some(RequestId::String(s.clone())),
189 _ => None,
190 });
191 if let Some(id) = id {
192 let error_response =
193 Response::error(id, Error::invalid_request("Invalid Request"));
194 messages.push(Message::Response(error_response));
195 } else if item.get("method").is_some() {
196 debug!(
197 "Batch item {} is a notification (has method but no id), skipping",
198 index
199 );
200 } else {
201 debug!(
202 "Batch item {} is invalid (no id or method), creating error response",
203 index
204 );
205 let error_response = Response::error(
206 RequestId::Null,
207 Error::invalid_request("Invalid Request"),
208 );
209 messages.push(Message::Response(error_response));
210 }
211 }
212 }
213 }
214 debug!("Batch parsing complete, {} messages", messages.len());
215 return Ok(Message::Batch(messages));
216 }
217
218 if value_ref.get("id").is_some() {
219 debug!("Message has 'id' field, checking for error/method");
220 if value_ref.get("error").is_some() {
221 debug!("Message has 'error' field, parsing as Response");
222 serde_json::from_value(value)
223 .map(Message::Response)
224 .map_err(|e| {
225 debug!("Failed to parse as Response: {}", e);
226 InternalError::invalid_request("Invalid Request")
227 })
228 } else if value_ref.get("method").is_some() {
229 debug!("Message has 'method' field, parsing as Request");
230 let req: Request = serde_json::from_value(value).map_err(|e| {
231 debug!("Failed to deserialize as Request: {}", e);
232 InternalError::invalid_request("Invalid Request")
233 })?;
234
235 if req.jsonrpc != "2.0" {
236 debug!("Invalid jsonrpc value: '{}', expected '2.0'", req.jsonrpc);
237 return Err(InternalError::invalid_request("Invalid Request"));
238 }
239
240 debug!("Request parsed successfully: {}", req.method);
241 Ok(Message::Request(req))
242 } else {
243 debug!("Message has 'id' but no 'method' or 'error' - Invalid Request");
244 Err(InternalError::invalid_request("Invalid Request"))
245 }
246 } else {
247 debug!("Message has no 'id' field, parsing as Notification");
248 let notif: Notification = serde_json::from_value(value).map_err(|e| {
249 debug!("Failed to deserialize as Notification: {}", e);
250 InternalError::invalid_request("Invalid Request")
251 })?;
252
253 if notif.jsonrpc != "2.0" {
254 debug!("Invalid jsonrpc value: '{}', expected '2.0'", notif.jsonrpc);
255 return Err(InternalError::invalid_request("Invalid Request"));
256 }
257
258 debug!("Notification parsed successfully: {}", notif.method);
259 Ok(Message::Notification(notif))
260 }
261 }
262
263 pub fn to_json(&self) -> Result<serde_json::Value, serde_json::Error> {
264 match self {
265 Message::Request(req) => serde_json::to_value(req),
266 Message::Response(res) => serde_json::to_value(res),
267 Message::Notification(notif) => serde_json::to_value(notif),
268 Message::Batch(messages) => {
269 let json_array: Result<Vec<serde_json::Value>, serde_json::Error> =
270 messages.iter().map(|m| m.to_json()).collect();
271 Ok(serde_json::Value::Array(json_array?))
272 }
273 }
274 }
275
276 pub fn id(&self) -> Option<&RequestId> {
277 match self {
278 Message::Request(req) => Some(&req.id),
279 Message::Response(res) => Some(&res.id),
280 Message::Notification(_) => None,
281 Message::Batch(_) => None,
282 }
283 }
284
285 pub fn is_request(&self) -> bool {
286 matches!(self, Message::Request(_))
287 }
288
289 pub fn is_response(&self) -> bool {
290 matches!(self, Message::Response(_))
291 }
292
293 pub fn is_notification(&self) -> bool {
294 matches!(self, Message::Notification(_))
295 }
296
297 pub fn is_batch(&self) -> bool {
298 matches!(self, Message::Batch(_))
299 }
300
301 fn from_json_internal(value: serde_json::Value) -> Result<Self, InternalError> {
302 if value.get("id").is_some() {
303 if value.get("error").is_some() {
304 serde_json::from_value(value)
305 .map(Message::Response)
306 .map_err(|_| InternalError::invalid_request("Invalid Request"))
307 } else if value.get("method").is_some() {
308 serde_json::from_value::<Request>(value)
309 .map(|req| {
310 if req.jsonrpc != "2.0" {
311 return Err(InternalError::invalid_request("Invalid Request"));
312 }
313 Ok(Message::Request(req))
314 })
315 .map_err(|_| InternalError::invalid_request("Invalid Request"))?
316 } else {
317 Err(InternalError::invalid_request("Invalid Request"))
318 }
319 } else {
320 serde_json::from_value::<Notification>(value)
321 .map(|notif| {
322 if notif.jsonrpc != "2.0" {
323 return Err(InternalError::invalid_request("Invalid Request"));
324 }
325 Ok(Message::Notification(notif))
326 })
327 .map_err(|_| InternalError::invalid_request("Invalid Request"))?
328 }
329 }
330}