use super::{helpers, Error};
use alloc::string::{String, ToString};
use core::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Notification {
pub jsonrpc: String,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
impl Notification {
pub fn new<M>(method: M) -> Self
where
M: ToString,
{
Notification {
jsonrpc: "2.0".to_string(),
method: method.to_string(),
params: None,
}
}
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 parse(s: &str) -> Result<(Self, &str), Error> {
let (message, remainder) = helpers::get_content_length(s)?;
let notification = Notification::parse_json(message)?;
Ok((notification, 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 Notification {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s).map(|(json, _)| json)
}
}
impl fmt::Display for Notification {
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 Notification {
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 notification = Notification::parse_json(&contents)?;
Ok((n, notification))
}
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(),
})
}
}
}