use bytes::{Buf, BufMut};
use log::trace;
use std::marker::PhantomData;
use tonic::{
codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder},
Status,
};
#[derive(Debug)]
pub struct JsonEncoder<T>(PhantomData<T>);
impl<T: serde::Serialize> Encoder for JsonEncoder<T> {
type Error = Status;
type Item = T;
fn encode(
&mut self,
item: Self::Item,
buf: &mut EncodeBuf<'_>,
) -> Result<(), Self::Error> {
serde_json::to_writer(buf.writer(), &item)
.map_err(|e| Status::internal(e.to_string()))
}
}
#[derive(Debug)]
pub struct JsonDecoder<U>(PhantomData<U>);
impl<U: serde::de::DeserializeOwned> Decoder for JsonDecoder<U> {
type Error = Status;
type Item = U;
fn decode(
&mut self,
buf: &mut DecodeBuf<'_>,
) -> Result<Option<Self::Item>, Self::Error> {
if !buf.has_remaining() {
return Ok(None);
}
let bytes = buf.copy_to_bytes(buf.remaining());
trace!("grpc+json: {}", std::str::from_utf8(&bytes).unwrap_or("not valid utf-8"));
let item: Self::Item = serde_json::from_slice(&bytes)
.map_err(|e| Status::internal(e.to_string()))?;
Ok(Some(item))
}
}
#[derive(Debug, Clone)]
pub struct JsonCodec<T, U>(PhantomData<(T, U)>);
impl<T, U> Default for JsonCodec<T, U> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<T, U> Codec for JsonCodec<T, U>
where
T: serde::Serialize + Send + 'static,
U: serde::de::DeserializeOwned + Send + 'static,
{
type Decode = U;
type Decoder = JsonDecoder<U>;
type Encode = T;
type Encoder = JsonEncoder<T>;
fn encoder(&mut self) -> Self::Encoder {
JsonEncoder(PhantomData)
}
fn decoder(&mut self) -> Self::Decoder {
JsonDecoder(PhantomData)
}
}