use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use crate::constants::JSONRPC_VERSION;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RequestId {
String(String),
Number(i64),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JSONRPCMessage {
Request(JSONRPCRequest),
Notification(JSONRPCNotification),
Response(JSONRPCResponse),
Error(JSONRPCError),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCRequest {
pub jsonrpc: String,
pub id: RequestId,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: 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>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCResponse {
pub jsonrpc: String,
pub id: RequestId,
pub result: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCError {
pub jsonrpc: String,
pub id: RequestId,
pub error: JSONRPCErrorObject,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCErrorObject {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
pub mod error_codes {
pub const PARSE_ERROR: i32 = -32700;
pub const INVALID_REQUEST: i32 = -32600;
pub const METHOD_NOT_FOUND: i32 = -32601;
pub const INVALID_PARAMS: i32 = -32602;
pub const INTERNAL_ERROR: i32 = -32603;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Request {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<RequestParams>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub _meta: Option<RequestMeta>,
#[serde(flatten)]
pub extra: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestMeta {
#[serde(skip_serializing_if = "Option::is_none")]
pub progress_token: Option<super::common::ProgressToken>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Notification {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<NotificationParams>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotificationParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub _meta: Option<HashMap<String, Value>>,
#[serde(flatten)]
pub extra: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Result {
#[serde(skip_serializing_if = "Option::is_none")]
pub _meta: Option<HashMap<String, Value>>,
#[serde(flatten)]
pub extra: HashMap<String, Value>,
}
pub type EmptyResult = Result;
impl JSONRPCRequest {
pub fn new(id: RequestId, method: String, params: Option<Value>) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
id,
method,
params,
}
}
}
impl JSONRPCNotification {
pub fn new(method: String, params: Option<Value>) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
method,
params,
}
}
}
impl JSONRPCResponse {
pub fn new(id: RequestId, result: Value) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
id,
result,
}
}
}
impl JSONRPCError {
pub fn new(id: RequestId, code: i32, message: String, data: Option<Value>) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
id,
error: JSONRPCErrorObject {
code,
message,
data,
},
}
}
}