Skip to main content

kimi_wire/protocol/
jsonrpc.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4/// JSON-RPC version marker.
5///
6/// This type can only represent the value `"2.0"`, matching the JSON-RPC 2.0
7/// specification. It serializes as the JSON string `"2.0"` and deserialization
8/// rejects any other value.
9///
10/// Construct via [`JsonRpcVersion::V2`] or [`JsonRpcVersion::default()`].
11#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
12pub struct JsonRpcVersion;
13
14impl JsonRpcVersion {
15    /// The only valid JSON-RPC version this crate supports.
16    pub const V2: Self = Self;
17
18    /// Wire representation as a `&'static str` (`"2.0"`).
19    pub fn as_str(&self) -> &'static str {
20        "2.0"
21    }
22}
23
24impl serde::Serialize for JsonRpcVersion {
25    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
26        serializer.serialize_str(self.as_str())
27    }
28}
29
30impl<'de> serde::Deserialize<'de> for JsonRpcVersion {
31    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
32        let s = <std::borrow::Cow<'_, str>>::deserialize(deserializer)?;
33        if s == "2.0" {
34            Ok(JsonRpcVersion)
35        } else {
36            Err(serde::de::Error::custom(format!(
37                "unsupported JSON-RPC version: expected \"2.0\", got {s:?}"
38            )))
39        }
40    }
41}
42
43/// A JSON-RPC 2.0 request.
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
45pub struct JsonRpcRequest<Params> {
46    /// JSON-RPC version.
47    pub jsonrpc: JsonRpcVersion,
48    /// Method name.
49    pub method: String,
50    /// Request id.
51    pub id: String,
52    /// Method parameters.
53    pub params: Params,
54}
55
56/// A JSON-RPC 2.0 notification (no id).
57#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
58pub struct JsonRpcNotification<Params> {
59    /// JSON-RPC version.
60    pub jsonrpc: JsonRpcVersion,
61    /// Method name.
62    pub method: String,
63    /// Method parameters.
64    pub params: Params,
65}
66
67/// A successful JSON-RPC 2.0 response.
68#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
69pub struct JsonRpcSuccessResponse<Result> {
70    /// JSON-RPC version.
71    pub jsonrpc: JsonRpcVersion,
72    /// Request id matching the request.
73    pub id: String,
74    /// Result value.
75    pub result: Result,
76}
77
78/// An error JSON-RPC 2.0 response.
79#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
80pub struct JsonRpcErrorResponse {
81    /// JSON-RPC version.
82    pub jsonrpc: JsonRpcVersion,
83    /// Request id matching the request (or `null`).
84    pub id: String,
85    /// Error details.
86    pub error: JsonRpcError,
87}
88
89/// JSON-RPC error object.
90#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
91pub struct JsonRpcError {
92    /// Error code.
93    pub code: i32,
94    /// Error message.
95    pub message: String,
96    /// Additional error data.
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub data: Option<Value>,
99}
100
101/// JSON-RPC error code for "Method not found".
102pub const METHOD_NOT_FOUND: i32 = -32601;
103
104/// A raw, untyped wire message for low-level parsing.
105///
106/// All fields are optional so that any valid JSON-RPC line can be parsed
107/// without knowing the concrete schema upfront.
108#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
109pub struct RawWireMessage {
110    /// JSON-RPC version.
111    pub jsonrpc: JsonRpcVersion,
112    /// Request/response id, if present.
113    pub id: Option<String>,
114    /// Method name for requests/notifications.
115    pub method: Option<String>,
116    /// Parameters for requests/notifications.
117    pub params: Option<Value>,
118    /// Result for success responses.
119    pub result: Option<Value>,
120    /// Error for error responses.
121    pub error: Option<JsonRpcError>,
122}