Try to parse a raw JSON string as either a Response or a Notification. Responses have an `id` field; notifications have a `method` field but no `id`.
(s: &str)
| 62 | /// Try to parse a raw JSON string as either a Response or a Notification. |
| 63 | /// Responses have an `id` field; notifications have a `method` field but no `id`. |
| 64 | pub fn from_str(s: &str) -> Result<Self, serde_json::Error> { |
| 65 | // Try response first (has `id`) |
| 66 | if let Ok(resp) = serde_json::from_str::<JsonRpcResponse>(s) { |
| 67 | return Ok(JsonRpcMessage::Response(resp)); |
| 68 | } |
| 69 | // Fall back to notification (has `method`, no `id`) |
| 70 | let notif = serde_json::from_str::<JsonRpcNotification>(s)?; |
| 71 | Ok(JsonRpcMessage::Notification(notif)) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #[derive(Debug, Clone, Serialize, Deserialize)] |