From 4d51e3211740764764a6423f8ead4944e1790341 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sun, 19 Nov 2023 22:19:06 +0300 Subject: karyons jsonrpc implementation --- jsonrpc/src/message.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 jsonrpc/src/message.rs (limited to 'jsonrpc/src/message.rs') diff --git a/jsonrpc/src/message.rs b/jsonrpc/src/message.rs new file mode 100644 index 0000000..89ef613 --- /dev/null +++ b/jsonrpc/src/message.rs @@ -0,0 +1,91 @@ +use serde::{Deserialize, Serialize}; + +/// Parse error: Invalid JSON was received by the server. +pub const PARSE_ERROR_CODE: i32 = -32700; + +/// Invalid request: The JSON sent is not a valid Request object. +pub const INVALID_REQUEST_ERROR_CODE: i32 = -32600; + +/// Method not found: The method does not exist / is not available. +pub const METHOD_NOT_FOUND_ERROR_CODE: i32 = -32601; + +/// Invalid params: Invalid method parameter(s). +pub const INVALID_PARAMS_ERROR_CODE: i32 = -32602; + +/// Internal error: Internal JSON-RPC error. +pub const INTERNAL_ERROR_CODE: i32 = -32603; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Request { + pub jsonrpc: String, + pub method: String, + pub params: serde_json::Value, + pub id: serde_json::Value, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Response { + pub jsonrpc: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub result: Option, + #[serde(skip_serializing_if = "Option::is_none")] + 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, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Notification { + pub jsonrpc: String, + pub method: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub params: 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 + ) + } +} + +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 + ) + } +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "RpcError {{ code: {}, message: {}, data: {:?} }} ", + self.code, self.message, self.data + ) + } +} + +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 + ) + } +} -- cgit v1.2.3