#![warn(missing_docs)]
#![warn(missing_copy_implementations, missing_debug_implementations)]
#![warn(unused_qualifications, unused_results)]
#![warn(future_incompatible)]
#![warn(unused)]
#![forbid(broken_intra_doc_links)]
use dialectic_tokio_serde::*;
use serde::{Deserialize, Serialize};
use serde_json as json;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::LinesCodec;
#[derive(Debug, Clone, Copy)]
pub struct Json {
pretty: bool,
}
impl Json {
pub fn new() -> Self {
Json::default()
}
pub fn pretty() -> Self {
Json { pretty: true }
}
}
impl Default for Json {
fn default() -> Self {
Json { pretty: false }
}
}
impl Serializer for Json {
type Error = json::Error;
type Output = String;
fn serialize<T: Serialize>(&mut self, item: &T) -> Result<Self::Output, Self::Error> {
json::to_string(item)
}
}
impl<Input: AsRef<str>> Deserializer<Input> for Json {
type Error = json::Error;
fn deserialize<T: for<'a> Deserialize<'a>>(&mut self, src: &Input) -> Result<T, Self::Error> {
json::from_str(src.as_ref())
}
}
pub fn lines<W: AsyncWrite, R: AsyncRead>(
writer: W,
reader: R,
max_length: usize,
) -> (Sender<Json, LinesCodec, W>, Receiver<Json, LinesCodec, R>) {
symmetrical(
Json::default(),
LinesCodec::new_with_max_length(max_length),
writer,
reader,
)
}