use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const JSON_RPC_VERSION_2_0: &str = "2.0";
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum RequestId {
String(String),
Number(i64),
}
pub type NumberOrString = RequestId;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Request<T> {
pub jsonrpc: String,
pub method: String,
pub params: T,
pub id: RequestId,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JsonRpcResponse<T> {
pub jsonrpc: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<T>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<JsonRpcError>,
pub id: RequestId,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JsonRpcError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl JsonRpcError {
pub fn new(code: i32, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
data: None,
}
}
pub fn with_data(code: i32, message: impl Into<String>, data: Value) -> Self {
Self {
code,
message: message.into(),
data: Some(data),
}
}
pub fn parse_error() -> Self {
Self::new(-32700, "Parse error")
}
pub fn invalid_request() -> Self {
Self::new(-32600, "Invalid Request")
}
pub fn method_not_found() -> Self {
Self::new(-32601, "Method not found")
}
pub fn invalid_params() -> Self {
Self::new(-32602, "Invalid params")
}
pub fn internal_error() -> Self {
Self::new(-32603, "Internal error")
}
}