Skip to content

Commit

Permalink
add changes custom json macro to json_rpc in v1 module
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Jan 14, 2025
1 parent ede3466 commit 3b715d2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 27 deletions.
3 changes: 3 additions & 0 deletions protocols/v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ serde = { version = "1.0.89", default-features = false, features = ["derive", "a
serde_json = { version = "1.0.64", default-features = false, features = ["alloc"] }
tracing = {version = "0.1"}
binary_sv2 = { version = "^1.0.0", path = "../v2/binary-sv2/binary-sv2"}
sv2_serde_json = {path = "../../utils/sv2_serde_json"}
sv2_serde_json_macros = {path = "../../utils/sv2_serde_json_macros"}


[dev-dependencies]
quickcheck = "1"
Expand Down
37 changes: 37 additions & 0 deletions protocols/v1/src/json_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! https://www.jsonrpc.org/specification#response_object
use serde::{Deserialize, Serialize};
use sv2_serde_json_macros::{DeJson, SerJson};
use sv2_serde_json::value::{ToJsonValue, FromJsonValue};

#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(untagged)]
Expand All @@ -10,6 +12,14 @@ pub enum Message {
ErrorResponse(Response),
}

#[derive(Clone, SerJson, DeJson, Debug)]
pub enum Message_ {
StandardRequest(StandardRequest_),
Notification(Notification_),
OkResponse(Response_),
ErrorResponse(Response_),
}

impl Message {
// TODO REMOVE it
pub fn is_response(&self) -> bool {
Expand All @@ -36,26 +46,53 @@ pub struct StandardRequest {
pub params: serde_json::Value,
}

#[derive(SerJson, DeJson, Clone, Debug, PartialEq)]
pub struct StandardRequest_ {
pub id: u64,
pub method: String,
pub params: sv2_serde_json::value::Value
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Notification {
pub method: String,
pub params: serde_json::Value,
}

#[derive(Clone, SerJson, DeJson, Debug)]
pub struct Notification_ {
pub method: String,
pub params: sv2_serde_json::value::Value,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Response {
pub id: u64,
pub error: Option<JsonRpcError>,
pub result: serde_json::Value,
}

#[derive(Clone, SerJson, DeJson, Debug)]
pub struct Response_ {
pub id: u64,
pub error: Option<JsonRpcError_>,
pub result: sv2_serde_json::value::Value,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct JsonRpcError {
pub code: i32, // json do not specify precision which one should be used?
pub message: String,
pub data: Option<serde_json::Value>,
}

#[derive(Clone, SerJson, DeJson, Debug)]
pub struct JsonRpcError_ {
pub code: i32, // json do not specify precision which one should be used?
pub message: String,
pub data: Option<sv2_serde_json::value::Value>,
}

impl From<Response> for Message {
fn from(res: Response) -> Self {
if res.error.is_some() {
Expand Down
49 changes: 32 additions & 17 deletions roles/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 26 additions & 10 deletions utils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3b715d2

Please sign in to comment.