rspc_legacy/internal/jsonrpc.rs
1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use specta::Type;
4
5pub use super::jsonrpc_exec::*;
6
7#[derive(Debug, Clone, Deserialize, Serialize, Type, PartialEq, Eq, Hash)]
8#[serde(untagged)]
9pub enum RequestId {
10 Null,
11 Number(u32),
12 String(String),
13}
14
15#[derive(Debug, Clone, Deserialize, Serialize)] // TODO: Type on this
16pub struct Request {
17 pub jsonrpc: Option<String>, // This is required in the JsonRPC spec but I make it optional.
18 pub id: RequestId,
19 #[serde(flatten)]
20 pub inner: RequestInner,
21}
22
23#[derive(Debug, Clone, Deserialize, Serialize, Type)]
24#[serde(tag = "method", content = "params", rename_all = "camelCase")]
25pub enum RequestInner {
26 Query {
27 path: String,
28 input: Option<Value>,
29 },
30 Mutation {
31 path: String,
32 input: Option<Value>,
33 },
34 Subscription {
35 path: String,
36 input: (RequestId, Option<Value>),
37 },
38 SubscriptionStop {
39 input: RequestId,
40 },
41}
42
43#[derive(Debug, Clone, Serialize)] // TODO: Add `specta::Type` when supported
44pub struct Response {
45 pub jsonrpc: &'static str,
46 pub id: RequestId,
47 pub result: ResponseInner,
48}
49
50#[derive(Debug, Clone, Serialize, Type)]
51#[serde(tag = "type", content = "data", rename_all = "camelCase")]
52pub enum ResponseInner {
53 Event(Value),
54 Response(Value),
55 Error(JsonRPCError),
56}
57
58#[derive(Debug, Clone, Serialize, Type)]
59pub struct JsonRPCError {
60 pub code: i32,
61 pub message: String,
62 pub data: Option<Value>,
63}
64
65// #[cfg(test)]
66// mod tests {
67// use std::{fs::File, io::Write, path::PathBuf};
68
69// use super::*;
70
71// #[test]
72// fn export_internal_bindings() {
73// // let mut file = File::create(
74// // PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("./packages/client/src/types.ts"),
75// // )
76// // .unwrap();
77// // file.write_all(
78// // b"// Do not modify this file. It was generated from the Rust types by running ``.\n\n",
79// // )
80// // .unwrap();
81// // // TODO: Add an API into Specta which allows exporting a type and all types it depends on.
82// // file.write_all(format!("{}\n\n", specta::ts_export::<RequestId>().unwrap()).as_bytes())
83// // .unwrap();
84// // file.write_all(format!("{}\n\n", specta::ts_export::<Request>().unwrap()).as_bytes())
85// // .unwrap();
86// }
87
88// #[test]
89// fn test_request_id() {
90// // println!(
91// // "{}",
92// // serde_json::to_string(&Request {
93// // jsonrpc: None,
94// // id: RequestId::Null,
95// // inner: RequestInner::Query {
96// // path: "test".into(),
97// // input: None,
98// // },
99// // })
100// // .unwrap()
101// // );
102// todo!();
103
104// // TODO: Test serde
105
106// // TODO: Test specta
107// }
108
109// #[test]
110// fn test_jsonrpc_request() {
111// todo!();
112// }
113
114// #[test]
115// fn test_jsonrpc_response() {
116// todo!();
117// }
118// }