zerolaunch_plugin_protocol/
jsonrpc.rs1use serde::{Deserialize, Serialize};
2
3use crate::error::JsonRpcError;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(untagged)]
8pub enum Message {
9 Request(Request),
10 Response(Response),
11 Notification(Notification),
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct Request {
16 pub jsonrpc: String,
17 pub id: u64,
18 pub method: String,
19 #[serde(default)]
20 pub params: serde_json::Value,
21}
22
23impl Request {
24 pub fn new(id: u64, method: impl Into<String>, params: serde_json::Value) -> Self {
25 Self {
26 jsonrpc: "2.0".into(),
27 id,
28 method: method.into(),
29 params,
30 }
31 }
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct Response {
36 pub jsonrpc: String,
37 pub id: u64,
38 #[serde(default)]
39 pub result: Option<serde_json::Value>,
40 #[serde(default)]
41 pub error: Option<JsonRpcError>,
42}
43
44impl Response {
45 pub fn ok(id: u64, result: serde_json::Value) -> Self {
46 Self {
47 jsonrpc: "2.0".into(),
48 id,
49 result: Some(result),
50 error: None,
51 }
52 }
53
54 pub fn err(id: u64, error: JsonRpcError) -> Self {
55 Self {
56 jsonrpc: "2.0".into(),
57 id,
58 result: None,
59 error: Some(error),
60 }
61 }
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct Notification {
66 pub jsonrpc: String,
67 pub method: String,
68 #[serde(default)]
69 pub params: serde_json::Value,
70}
71
72impl Notification {
73 pub fn new(method: impl Into<String>, params: serde_json::Value) -> Self {
74 Self {
75 jsonrpc: "2.0".into(),
76 method: method.into(),
77 params,
78 }
79 }
80}
81
82pub fn is_request(msg: &Message) -> bool {
84 matches!(msg, Message::Request(_))
85}
86
87pub fn is_notification(msg: &Message) -> bool {
89 matches!(msg, Message::Notification(_))
90}
91
92pub fn is_response(msg: &Message) -> bool {
94 matches!(msg, Message::Response(_))
95}