Skip to main content

perl_lsp_protocol/
jsonrpc.rs

1//! JSON-RPC 2.0 message types
2//!
3//! Core request, response, and error types for JSON-RPC communication.
4
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8/// JSON-RPC 2.0 request message
9///
10/// Represents an incoming request from the LSP client.
11/// The `id` field is `None` for notifications.
12#[derive(Debug, Deserialize)]
13pub struct JsonRpcRequest {
14    /// JSON-RPC version (always "2.0")
15    #[serde(rename = "jsonrpc")]
16    pub _jsonrpc: String,
17
18    /// Request identifier (None for notifications)
19    pub id: Option<Value>,
20
21    /// Method name to invoke
22    pub method: String,
23
24    /// Method parameters
25    pub params: Option<Value>,
26}
27
28/// JSON-RPC 2.0 response message
29///
30/// Represents an outgoing response to the LSP client.
31/// Either `result` or `error` should be set, but not both.
32#[derive(Debug, Serialize)]
33pub struct JsonRpcResponse {
34    /// JSON-RPC version (always "2.0")
35    pub jsonrpc: String,
36
37    /// Request identifier (matches the request's id)
38    pub id: Option<Value>,
39
40    /// Success result (mutually exclusive with error)
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub result: Option<Value>,
43
44    /// Error result (mutually exclusive with result)
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub error: Option<JsonRpcError>,
47}
48
49impl JsonRpcResponse {
50    /// Create a success response
51    pub fn success(id: Option<Value>, result: Value) -> Self {
52        Self { jsonrpc: "2.0".to_string(), id, result: Some(result), error: None }
53    }
54
55    /// Create an error response
56    pub fn error(id: Option<Value>, error: JsonRpcError) -> Self {
57        Self { jsonrpc: "2.0".to_string(), id, result: None, error: Some(error) }
58    }
59
60    /// Create a null result response (for methods that return nothing)
61    pub fn null(id: Option<Value>) -> Self {
62        Self { jsonrpc: "2.0".to_string(), id, result: Some(Value::Null), error: None }
63    }
64}
65
66/// JSON-RPC 2.0 error object
67///
68/// Represents an error that occurred during request processing.
69#[derive(Debug, Serialize, Clone)]
70pub struct JsonRpcError {
71    /// Error code (see protocol/errors.rs for standard codes)
72    pub code: i32,
73
74    /// Human-readable error message
75    pub message: String,
76
77    /// Additional error data (optional)
78    pub data: Option<Value>,
79}
80
81impl JsonRpcError {
82    /// Create a new error
83    pub fn new(code: i32, message: impl Into<String>) -> Self {
84        Self { code, message: message.into(), data: None }
85    }
86
87    /// Create an error with additional data
88    pub fn with_data(code: i32, message: impl Into<String>, data: Value) -> Self {
89        Self { code, message: message.into(), data: Some(data) }
90    }
91}
92
93impl std::fmt::Display for JsonRpcError {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        write!(f, "{}: {}", self.code, self.message)
96    }
97}
98
99impl std::error::Error for JsonRpcError {}