use serde::{Deserialize, Serialize};
use super::codes::{
BUFFER_NOT_FOUND, COMMAND_NOT_FOUND, INTERNAL_ERROR, INVALID_KEY_NOTATION, INVALID_PARAMS,
INVALID_REQUEST, METHOD_NOT_FOUND, PARSE_ERROR,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcRequest {
pub jsonrpc: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<u64>,
pub method: String,
#[serde(default)]
pub params: serde_json::Value,
}
impl RpcRequest {
#[must_use]
pub fn new(id: u64, method: impl Into<String>, params: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: Some(id),
method: method.into(),
params,
}
}
#[must_use]
pub fn notification(method: impl Into<String>, params: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: None,
method: method.into(),
params,
}
}
#[must_use]
pub const fn is_notification(&self) -> bool {
self.id.is_none()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcResponse {
pub jsonrpc: String,
pub id: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<RpcError>,
}
impl RpcResponse {
#[must_use]
pub fn success(id: u64, result: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: Some(result),
error: None,
}
}
#[must_use]
pub fn error(id: u64, error: RpcError) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: None,
error: Some(error),
}
}
#[must_use]
pub fn ok(id: u64) -> Self {
Self::success(id, serde_json::Value::Null)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
}
impl RpcError {
#[must_use]
pub fn new(code: i32, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
data: None,
}
}
#[must_use]
pub fn with_data(code: i32, message: impl Into<String>, data: serde_json::Value) -> Self {
Self {
code,
message: message.into(),
data: Some(data),
}
}
#[must_use]
pub fn parse_error() -> Self {
Self::new(PARSE_ERROR, "Parse error")
}
#[must_use]
pub fn invalid_request() -> Self {
Self::new(INVALID_REQUEST, "Invalid Request")
}
#[must_use]
pub fn method_not_found(method: &str) -> Self {
Self::new(METHOD_NOT_FOUND, format!("Method not found: {method}"))
}
#[must_use]
pub fn invalid_params(details: impl Into<String>) -> Self {
Self::new(INVALID_PARAMS, format!("Invalid params: {}", details.into()))
}
#[must_use]
pub fn internal_error(details: impl Into<String>) -> Self {
Self::new(INTERNAL_ERROR, format!("Internal error: {}", details.into()))
}
#[must_use]
pub fn buffer_not_found(buffer_id: usize) -> Self {
Self::new(BUFFER_NOT_FOUND, format!("Buffer not found: {buffer_id}"))
}
#[must_use]
pub fn command_not_found(command: &str) -> Self {
Self::new(COMMAND_NOT_FOUND, format!("Command not found: {command}"))
}
#[must_use]
pub fn invalid_key_notation(details: impl Into<String>) -> Self {
Self::new(INVALID_KEY_NOTATION, format!("Invalid key notation: {}", details.into()))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcNotification {
pub jsonrpc: String,
pub method: String,
pub params: serde_json::Value,
}
impl RpcNotification {
#[must_use]
pub fn new(method: impl Into<String>, params: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
method: method.into(),
params,
}
}
}
#[cfg(test)]
#[path = "messages_tests.rs"]
mod tests;