use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Deserialize)]
pub struct JsonRpcRequest {
pub jsonrpc: String,
#[serde(default)]
pub id: Option<Value>,
pub method: String,
#[serde(default)]
pub params: Value,
}
impl JsonRpcRequest {
pub fn is_notification(&self) -> bool {
self.id.is_none()
}
}
#[derive(Debug, Clone, Serialize)]
pub struct JsonRpcResponse {
pub jsonrpc: &'static str,
pub id: Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<JsonRpcError>,
}
#[derive(Debug, Clone, Serialize)]
pub struct JsonRpcError {
pub code: i64,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl JsonRpcResponse {
pub fn ok(id: Value, result: Value) -> Self {
Self {
jsonrpc: "2.0",
id,
result: Some(result),
error: None,
}
}
pub fn err(id: Value, code: i64, message: impl Into<String>) -> Self {
Self {
jsonrpc: "2.0",
id,
result: None,
error: Some(JsonRpcError {
code,
message: message.into(),
data: None,
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_without_id_is_notification() {
let r: JsonRpcRequest =
serde_json::from_str(r#"{"jsonrpc":"2.0","method":"x","params":{}}"#).unwrap();
assert!(r.is_notification());
}
#[test]
fn request_with_id_is_call() {
let r: JsonRpcRequest =
serde_json::from_str(r#"{"jsonrpc":"2.0","id":1,"method":"x"}"#).unwrap();
assert!(!r.is_notification());
assert_eq!(r.id, Some(Value::from(1)));
}
#[test]
fn ok_response_omits_error_field() {
let r = JsonRpcResponse::ok(Value::from(1), serde_json::json!({"y": 2}));
let s = serde_json::to_string(&r).unwrap();
assert!(s.contains("\"result\""));
assert!(!s.contains("\"error\""));
}
#[test]
fn err_response_omits_result_field() {
let r = JsonRpcResponse::err(Value::from(1), -32601, "no such method");
let s = serde_json::to_string(&r).unwrap();
assert!(s.contains("\"error\""));
assert!(!s.contains("\"result\""));
assert!(s.contains("-32601"));
}
}