From 028940fe3e0a87cdc421a6d07f1ecfb6c208b9d0 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Tue, 21 May 2024 02:20:45 +0200 Subject: jsonrpc: support pubsub --- jsonrpc/src/message.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'jsonrpc/src/message.rs') diff --git a/jsonrpc/src/message.rs b/jsonrpc/src/message.rs index f4bf490..9c89362 100644 --- a/jsonrpc/src/message.rs +++ b/jsonrpc/src/message.rs @@ -23,9 +23,12 @@ pub struct Request { pub method: String, pub params: serde_json::Value, pub id: serde_json::Value, + #[serde(skip_serializing_if = "Option::is_none")] + pub subscriber: Option, } #[derive(Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] pub struct Response { pub jsonrpc: String, #[serde(skip_serializing_if = "Option::is_none")] @@ -34,30 +37,35 @@ pub struct Response { pub error: Option, #[serde(skip_serializing_if = "Option::is_none")] pub id: Option, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct Error { - pub code: i32, - pub message: String, #[serde(skip_serializing_if = "Option::is_none")] - pub data: Option, + pub subscription: Option, } #[derive(Debug, Serialize, Deserialize)] pub struct Notification { pub jsonrpc: String, - pub method: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub method: Option, #[serde(skip_serializing_if = "Option::is_none")] pub params: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub subscription: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Error { + pub code: i32, + pub message: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub data: Option, } impl std::fmt::Display for Request { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "{{jsonrpc: {}, method: {}, params: {:?}, id: {:?}}}", - self.jsonrpc, self.method, self.params, self.id + "{{jsonrpc: {}, method: {}, params: {:?}, id: {:?}, subscribe: {:?}}}", + self.jsonrpc, self.method, self.params, self.id, self.subscriber ) } } @@ -66,8 +74,8 @@ impl std::fmt::Display for Response { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "{{jsonrpc: {}, result': {:?}, error: {:?} , id: {:?}}}", - self.jsonrpc, self.result, self.error, self.id + "{{jsonrpc: {}, result': {:?}, error: {:?} , id: {:?}, subscription: {:?}}}", + self.jsonrpc, self.result, self.error, self.id, self.subscription ) } } @@ -86,8 +94,8 @@ impl std::fmt::Display for Notification { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "{{jsonrpc: {}, method: {}, params: {:?}}}", - self.jsonrpc, self.method, self.params + "{{jsonrpc: {}, method: {:?}, params: {:?}, subscription: {:?}}}", + self.jsonrpc, self.method, self.params, self.subscription ) } } -- cgit v1.2.3