use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
pub jsonrpc: String,
pub id: Option<Value>,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse {
pub jsonrpc: String,
pub id: Value,
pub result: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcErrorResponse {
pub jsonrpc: String,
pub id: Value,
pub error: JsonRpcError,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcNotification {
pub jsonrpc: String,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
impl JsonRpcRequest {
pub fn new(id: Value, method: String, params: Option<Value>) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: Some(id),
method,
params,
}
}
}
impl JsonRpcResponse {
pub fn new(id: Value, result: Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result,
}
}
}
impl JsonRpcErrorResponse {
pub fn new(id: Value, error: JsonRpcError) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
error,
}
}
}
impl JsonRpcError {
pub fn new(code: i32, message: String, data: Option<Value>) -> Self {
Self {
code,
message,
data,
}
}
}
impl JsonRpcNotification {
pub fn new(method: String, params: Option<Value>) -> Self {
Self {
jsonrpc: "2.0".to_string(),
method,
params,
}
}
}