use super::{helpers, Error};
use alloc::string::{String, ToString};
use core::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
use serde_json::{Number, Value};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Request {
pub jsonrpc: String,
pub id: Value,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
impl Request {
pub fn new<M>(method: M) -> Self
where
M: ToString,
{
#[cfg(feature = "uuid")]
let id = Value::String(uuid::Uuid::new_v4().to_string());
#[cfg(not(feature = "uuid"))]
let id = Value::Number(0.into());
Request {
id,
jsonrpc: "2.0".to_string(),
method: method.to_string(),
params: None,
}
}
pub fn with_id<I>(mut self, id: I) -> Self
where
I: Into<Number>,
{
self.id = Value::Number(id.into());
self
}
pub fn with_id_string<I>(mut self, id: I) -> Self
where
I: ToString,
{
self.id = Value::String(id.to_string());
self
}
pub fn with_params<P>(self, params: P) -> Result<Self, Error>
where
P: Serialize,
{
serde_json::to_value(params)
.map_err(|e| Error {
code: Error::PARSE_ERROR,
message: e.to_string(),
data: None,
})
.map(|params| self.with_params_value(params))
}
pub fn with_params_value(mut self, params: Value) -> Self {
self.params = Some(params);
self
}
pub fn prepare(&self) -> (Value, String) {
let id = self.id.clone();
let message = self.to_string();
(id, message)
}
pub fn parse(s: &str) -> Result<(Self, &str), Error> {
let (message, remainder) = helpers::get_content_length(s)?;
let request = Request::parse_json(message)?;
Ok((request, remainder))
}
pub fn parse_json(json: &str) -> Result<Self, Error> {
serde_json::from_str(json).map_err(|e| Error {
code: Error::INVALID_REQUEST,
message: e.to_string(),
data: Some(Value::String(json.to_string())),
})
}
}
impl FromStr for Request {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s).map(|(json, _)| json)
}
}
impl fmt::Display for Request {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
serde_json::to_string(&self)
.map_err(|_| fmt::Error)
.and_then(|m| write!(f, "Content-Length: {}\r\n\r\n{}", m.len(), m))
}
}
#[cfg(feature = "std")]
mod io {
use super::*;
use std::io::prelude::*;
impl Request {
pub fn try_from_reader<R>(reader: R) -> Result<(usize, Self), Error>
where
R: Read,
{
let (n, contents) = helpers::get_content_from_reader(reader)?;
let request = Request::parse_json(&contents)?;
Ok((n, request))
}
pub fn try_to_writer<W>(&self, mut writer: W) -> Result<usize, Error>
where
W: Write,
{
writer
.write(self.to_string().as_bytes())
.map_err(|e| Error {
code: Error::PARSE_ERROR,
message: e.to_string(),
data: serde_json::to_value(&self).ok(),
})
}
}
}