use std::borrow::Cow;
use crate::{Error, ErrorCode, Id, Request, Response};
enum CantSerialize {}
impl serde::Serialize for CantSerialize {
#[inline(always)]
fn serialize<S>(&self, _: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match *self {}
}
}
pub fn write_request<'a, T>(
method: &str,
params: T,
id: impl Into<Option<Id<'a>>>,
) -> serde_json::Result<Vec<u8>>
where
T: serde::Serialize,
{
let request = Request {
method: Cow::Borrowed(method),
params,
id: id.into(),
};
serde_json::to_vec(&request)
}
pub fn write_response<T, E>(result: Result<T, Error<E>>, id: Id) -> serde_json::Result<Vec<u8>>
where
T: serde::Serialize,
E: serde::Serialize,
{
let response = Response { result, id };
serde_json::to_vec(&response)
}
pub fn write_success<T>(value: T, id: Id) -> serde_json::Result<Vec<u8>>
where
T: serde::Serialize,
{
let response = Response::<T, CantSerialize> {
result: Ok(value),
id,
};
serde_json::to_vec(&response)
}
pub fn write_failure<E>(
code: impl Into<ErrorCode>,
message: &str,
id: Id,
data: E,
) -> serde_json::Result<Vec<u8>>
where
E: serde::Serialize,
{
let response = Response::<CantSerialize, E> {
result: Err(Error {
code: code.into(),
message: Cow::Borrowed(message),
data: Some(data),
}),
id,
};
serde_json::to_vec(&response)
}
pub fn write_datalass_failure(
code: impl Into<ErrorCode>,
message: &str,
id: Id,
) -> serde_json::Result<Vec<u8>> {
let response = Response::<CantSerialize, CantSerialize> {
result: Err(Error {
code: code.into(),
message: Cow::Borrowed(message),
data: None,
}),
id,
};
serde_json::to_vec(&response)
}
pub fn read_request<'a, T>(bytes: &'a [u8]) -> serde_json::Result<Request<'a, T>>
where
T: serde::de::Deserialize<'a>,
{
serde_json::from_slice(bytes)
}
pub fn read_response<'a, T, E>(bytes: &'a [u8]) -> serde_json::Result<Response<T, E>>
where
T: serde::Deserialize<'a>,
E: serde::Deserialize<'a>,
{
serde_json::from_slice(bytes)
}
pub fn read_response_ignore_error<'a, T>(
bytes: &'a [u8],
) -> serde_json::Result<Response<T, serde::de::IgnoredAny>>
where
T: serde::Deserialize<'a>,
{
serde_json::from_slice(bytes)
}