use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::Error;
const VERSION: &str = "2.0";
#[derive(Debug, Deserialize)]
pub struct Request {
pub jsonrpc: String,
pub method: String,
#[serde(default)]
pub params: Value,
pub id: Option<Value>,
}
#[derive(Debug, Serialize)]
pub struct Response {
pub jsonrpc: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<Error>,
pub id: Option<Value>,
}
impl Response {
pub fn success(id: Option<Value>, result: Value) -> Self {
Self {
jsonrpc: VERSION,
result: Some(result),
error: None,
id: id.or(Some(Value::Null)),
}
}
pub fn error(id: Option<Value>, error: Error) -> Self {
Self {
jsonrpc: VERSION,
result: None,
error: Some(error),
id: id.or(Some(Value::Null)),
}
}
}