use serde::{Deserialize, Serialize};
use serde_json::Value;
pub struct JsonRpcBuilder {
jsonrpc: String,
}
impl Default for JsonRpcBuilder {
fn default() -> Self {
Self {
jsonrpc: "2.0".to_string(),
}
}
}
impl JsonRpcBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn request(self, id: RequestId, method: String, params: Option<Value>) -> JsonRpcRequest {
JsonRpcRequest {
jsonrpc: self.jsonrpc,
id,
method,
params,
}
}
pub fn response_success(self, id: RequestId, result: Value) -> JsonRpcResponse {
JsonRpcResponse {
jsonrpc: self.jsonrpc,
id,
result: Some(result),
error: None,
}
}
pub fn response_error(self, id: RequestId, error: JsonRpcError) -> JsonRpcResponse {
JsonRpcResponse {
jsonrpc: self.jsonrpc,
id,
result: None,
error: Some(error),
}
}
pub fn notification(self, method: String, params: Option<Value>) -> JsonRpcNotification {
JsonRpcNotification {
jsonrpc: self.jsonrpc,
method,
params,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum RequestId {
Number(i64),
String(String),
}
impl Default for RequestId {
fn default() -> Self {
RequestId::Number(1)
}
}
impl std::fmt::Display for RequestId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RequestId::Number(n) => write!(f, "{}", n),
RequestId::String(s) => write!(f, "{}", s),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
pub jsonrpc: String,
pub id: RequestId,
pub method: String,
pub params: Option<Value>,
}
impl JsonRpcRequest {
pub fn new(id: RequestId, method: String, params: Option<Value>) -> Self {
JsonRpcBuilder::new().request(id, method, params)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse {
pub jsonrpc: String,
pub id: RequestId,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<JsonRpcError>,
}
impl JsonRpcResponse {
pub fn success(id: RequestId, result: Value) -> Self {
JsonRpcBuilder::new().response_success(id, result)
}
pub fn error(id: RequestId, error: JsonRpcError) -> Self {
JsonRpcBuilder::new().response_error(id, error)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcNotification {
pub jsonrpc: String,
pub method: String,
pub params: Option<Value>,
}
impl JsonRpcNotification {
pub fn new(method: String, params: Option<Value>) -> Self {
JsonRpcBuilder::new().notification(method, params)
}
}
#[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>,
}
impl JsonRpcError {
fn create_error(code: i32, message: String, data: Option<Value>) -> Self {
Self {
code,
message,
data,
}
}
pub fn parse_error() -> Self {
Self::create_error(-32700, "Parse error".to_string(), None)
}
pub fn invalid_request() -> Self {
Self::create_error(-32600, "Invalid Request".to_string(), None)
}
pub fn method_not_found(method: &str) -> Self {
Self::create_error(-32601, format!("Method not found: {}", method), None)
}
pub fn invalid_params(message: &str) -> Self {
Self::create_error(-32602, format!("Invalid params: {}", message), None)
}
pub fn internal_error(message: &str) -> Self {
Self::create_error(-32603, format!("Internal error: {}", message), None)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsonRpcMessage {
Request(JsonRpcRequest),
Response(JsonRpcResponse),
Notification(JsonRpcNotification),
}
impl std::str::FromStr for JsonRpcMessage {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
impl JsonRpcMessage {
pub fn parse(s: &str) -> Result<Self, serde_json::Error> {
s.parse()
}
pub fn to_string(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
}