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 #[must_use]
20 pub const fn as_str(&self) -> &'static str {
21 "2.0"
22 }
23}
24
25impl serde::Serialize for JsonRpcVersion {
26 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
27 serializer.serialize_str(self.as_str())
28 }
29}
30
31impl<'de> serde::Deserialize<'de> for JsonRpcVersion {
32 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
33 let s = <std::borrow::Cow<'_, str>>::deserialize(deserializer)?;
34 if s == "2.0" {
35 Ok(Self)
36 } else {
37 Err(serde::de::Error::custom(format!(
38 "unsupported JSON-RPC version: expected \"2.0\", got {s:?}"
39 )))
40 }
41 }
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
46pub struct JsonRpcRequest<Params> {
47 pub jsonrpc: JsonRpcVersion,
49 pub method: String,
51 pub id: String,
53 pub params: Params,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
59pub struct JsonRpcNotification<Params> {
60 pub jsonrpc: JsonRpcVersion,
62 pub method: String,
64 pub params: Params,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
70pub struct JsonRpcSuccessResponse<Result> {
71 pub jsonrpc: JsonRpcVersion,
73 pub id: String,
75 pub result: Result,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
81pub struct JsonRpcErrorResponse {
82 pub jsonrpc: JsonRpcVersion,
84 pub id: String,
86 pub error: JsonRpcError,
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
92pub struct JsonRpcError {
93 pub code: i32,
95 pub message: String,
97 #[serde(skip_serializing_if = "Option::is_none")]
99 pub data: Option<Value>,
100}
101
102pub const METHOD_NOT_FOUND: i32 = -32601;
104
105#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
110pub struct RawWireMessage {
111 pub jsonrpc: JsonRpcVersion,
113 pub id: Option<String>,
115 pub method: Option<String>,
117 pub params: Option<Value>,
119 pub result: Option<Value>,
121 pub error: Option<JsonRpcError>,
123}