kimi_wire/protocol/
jsonrpc.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
12pub struct JsonRpcVersion;
13
14impl JsonRpcVersion {
15 pub const V2: Self = Self;
17
18 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#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
45pub struct JsonRpcRequest<Params> {
46 pub jsonrpc: JsonRpcVersion,
48 pub method: String,
50 pub id: String,
52 pub params: Params,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
58pub struct JsonRpcNotification<Params> {
59 pub jsonrpc: JsonRpcVersion,
61 pub method: String,
63 pub params: Params,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
69pub struct JsonRpcSuccessResponse<Result> {
70 pub jsonrpc: JsonRpcVersion,
72 pub id: String,
74 pub result: Result,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
80pub struct JsonRpcErrorResponse {
81 pub jsonrpc: JsonRpcVersion,
83 pub id: String,
85 pub error: JsonRpcError,
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
91pub struct JsonRpcError {
92 pub code: i32,
94 pub message: String,
96 #[serde(skip_serializing_if = "Option::is_none")]
98 pub data: Option<Value>,
99}
100
101pub const METHOD_NOT_FOUND: i32 = -32601;
103
104#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
109pub struct RawWireMessage {
110 pub jsonrpc: JsonRpcVersion,
112 pub id: Option<String>,
114 pub method: Option<String>,
116 pub params: Option<Value>,
118 pub result: Option<Value>,
120 pub error: Option<JsonRpcError>,
122}