use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)]
pub struct RpcRequest {
pub jsonrpc: String,
pub method: String,
pub params: serde_json::Value,
pub id: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct RpcResponse {
pub jsonrpc: String,
pub result: Option<serde_json::Value>,
pub error: Option<RpcError>,
pub id: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct RpcError {
pub code: i32,
pub message: String,
pub data: Option<serde_json::Value>,
}
impl RpcResponse {
#[must_use]
pub fn success(id: Option<String>, result: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
result: Some(result),
error: None,
id,
}
}
#[must_use]
pub fn error(id: Option<String>, code: i32, message: impl Into<String>) -> Self {
Self {
jsonrpc: "2.0".to_string(),
result: None,
error: Some(RpcError {
code,
message: message.into(),
data: None,
}),
id,
}
}
}
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;
pub const UNAUTHORIZED: i32 = -32001;
pub const FORBIDDEN: i32 = -32002;
pub const NOT_FOUND: i32 = -32003;