Skip to main content

aft/lsp/
jsonrpc.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4/// JSON-RPC request ID.
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(untagged)]
7pub enum RequestId {
8    Int(i64),
9    String(String),
10}
11
12/// Outgoing JSON-RPC request.
13#[derive(Debug, Serialize)]
14pub struct Request {
15    pub jsonrpc: &'static str,
16    pub id: RequestId,
17    pub method: String,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub params: Option<Value>,
20}
21
22impl Request {
23    pub fn new(id: RequestId, method: impl Into<String>, params: Option<Value>) -> Self {
24        Self {
25            jsonrpc: "2.0",
26            id,
27            method: method.into(),
28            params,
29        }
30    }
31}
32
33/// Outgoing JSON-RPC notification (no id).
34#[derive(Debug, Serialize)]
35pub struct Notification {
36    pub jsonrpc: &'static str,
37    pub method: String,
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub params: Option<Value>,
40}
41
42impl Notification {
43    pub fn new(method: impl Into<String>, params: Option<Value>) -> Self {
44        Self {
45            jsonrpc: "2.0",
46            method: method.into(),
47            params,
48        }
49    }
50}
51
52/// Incoming JSON-RPC response.
53#[derive(Debug, Deserialize)]
54pub struct Response {
55    pub id: RequestId,
56    pub result: Option<Value>,
57    pub error: Option<ResponseError>,
58}
59
60#[derive(Debug, Deserialize)]
61pub struct ResponseError {
62    pub code: i32,
63    pub message: String,
64    pub data: Option<Value>,
65}
66
67/// Any incoming message from the server.
68#[derive(Debug)]
69pub enum ServerMessage {
70    Response(Response),
71    Notification {
72        method: String,
73        params: Option<Value>,
74    },
75    Request {
76        id: RequestId,
77        method: String,
78        params: Option<Value>,
79    },
80}
81
82impl ServerMessage {
83    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
84        let value: Value = serde_json::from_str(json)?;
85
86        if value.get("id").is_some() && value.get("method").is_some() {
87            Ok(ServerMessage::Request {
88                id: serde_json::from_value(value.get("id").cloned().unwrap_or(Value::Null))?,
89                method: value
90                    .get("method")
91                    .and_then(Value::as_str)
92                    .unwrap_or_default()
93                    .to_string(),
94                params: value.get("params").cloned(),
95            })
96        } else if value.get("id").is_some() {
97            Ok(ServerMessage::Response(serde_json::from_value(value)?))
98        } else {
99            Ok(ServerMessage::Notification {
100                method: value
101                    .get("method")
102                    .and_then(Value::as_str)
103                    .unwrap_or_default()
104                    .to_string(),
105                params: value.get("params").cloned(),
106            })
107        }
108    }
109}