use async_trait::async_trait;
use crate::thrift::{Error, ProtocolError, ProtocolErrorKind, Result};
use super::*;
#[async_trait]
pub trait AsyncReadThrift: Send + Sized {
async fn stream_from_in_protocol<T: TInputStreamProtocol>(
i_prot: &mut T,
) -> crate::thrift::Result<Self>;
}
#[async_trait]
impl AsyncReadThrift for String {
async fn stream_from_in_protocol<T: TInputStreamProtocol>(
i_prot: &mut T,
) -> crate::thrift::Result<Self> {
i_prot.read_string().await
}
}
#[async_trait]
impl AsyncReadThrift for bool {
async fn stream_from_in_protocol<T: TInputStreamProtocol>(
i_prot: &mut T,
) -> crate::thrift::Result<Self> {
i_prot.read_bool().await
}
}
#[async_trait]
impl AsyncReadThrift for u8 {
async fn stream_from_in_protocol<T: TInputStreamProtocol>(
i_prot: &mut T,
) -> crate::thrift::Result<Self> {
i_prot.read_byte().await
}
}
#[async_trait]
impl AsyncReadThrift for i64 {
async fn stream_from_in_protocol<T: TInputStreamProtocol>(
i_prot: &mut T,
) -> crate::thrift::Result<Self> {
i_prot.read_i64().await
}
}
#[async_trait]
impl AsyncReadThrift for Vec<u8> {
async fn stream_from_in_protocol<T: TInputStreamProtocol>(
i_prot: &mut T,
) -> crate::thrift::Result<Self> {
i_prot.read_bytes().await
}
}
#[async_trait]
pub trait TInputStreamProtocol: Send + Sized {
async fn read_message_begin(&mut self) -> Result<TMessageIdentifier>;
async fn read_message_end(&mut self) -> Result<()>;
async fn read_struct_begin(&mut self) -> Result<Option<TStructIdentifier>>;
async fn read_struct_end(&mut self) -> Result<()>;
async fn read_field_begin(&mut self) -> Result<TFieldIdentifier>;
async fn read_field_end(&mut self) -> Result<()>;
async fn read_bool(&mut self) -> Result<bool>;
async fn read_bytes(&mut self) -> Result<Vec<u8>>;
async fn read_i8(&mut self) -> Result<i8>;
async fn read_i16(&mut self) -> Result<i16>;
async fn read_i32(&mut self) -> Result<i32>;
async fn read_i64(&mut self) -> Result<i64>;
async fn read_double(&mut self) -> Result<f64>;
async fn read_string(&mut self) -> Result<String>;
async fn read_list_begin(&mut self) -> Result<TListIdentifier>;
async fn read_list_end(&mut self) -> Result<()>;
async fn read_set_begin(&mut self) -> Result<TSetIdentifier>;
async fn read_set_end(&mut self) -> Result<()>;
async fn read_map_begin(&mut self) -> Result<TMapIdentifier>;
async fn read_map_end(&mut self) -> Result<()>;
async fn read_list<P: AsyncReadThrift>(&mut self) -> crate::thrift::Result<Vec<P>> {
let list_ident = self.read_list_begin().await?;
let mut val: Vec<P> = Vec::with_capacity(list_ident.size as usize);
for _ in 0..list_ident.size {
val.push(P::stream_from_in_protocol(self).await?);
}
self.read_list_end().await?;
Ok(val)
}
async fn skip(&mut self, field_type: TType) -> Result<()> {
self.skip_till_depth(field_type, MAXIMUM_SKIP_DEPTH).await
}
async fn skip_till_depth(&mut self, field_type: TType, depth: i8) -> Result<()> {
if depth == 0 {
return Err(Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::DepthLimit,
message: format!("cannot parse past {:?}", field_type),
}));
}
match field_type {
TType::Bool => self.read_bool().await.map(|_| ()),
TType::I08 => self.read_i8().await.map(|_| ()),
TType::I16 => self.read_i16().await.map(|_| ()),
TType::I32 => self.read_i32().await.map(|_| ()),
TType::I64 => self.read_i64().await.map(|_| ()),
TType::Double => self.read_double().await.map(|_| ()),
TType::String => self.read_string().await.map(|_| ()),
TType::Struct => {
self.read_struct_begin().await?;
loop {
let field_ident = self.read_field_begin().await?;
if field_ident.field_type == TType::Stop {
break;
}
self.skip_till_depth(field_ident.field_type, depth - 1)
.await?;
}
self.read_struct_end().await
}
TType::List => {
let list_ident = self.read_list_begin().await?;
for _ in 0..list_ident.size {
self.skip_till_depth(list_ident.element_type, depth - 1)
.await?;
}
self.read_list_end().await
}
TType::Set => {
let set_ident = self.read_set_begin().await?;
for _ in 0..set_ident.size {
self.skip_till_depth(set_ident.element_type, depth - 1)
.await?;
}
self.read_set_end().await
}
TType::Map => {
let map_ident = self.read_map_begin().await?;
for _ in 0..map_ident.size {
let key_type = map_ident
.key_type
.expect("non-zero sized map should contain key type");
let val_type = map_ident
.value_type
.expect("non-zero sized map should contain value type");
self.skip_till_depth(key_type, depth - 1).await?;
self.skip_till_depth(val_type, depth - 1).await?;
}
self.read_map_end().await
}
u => Err(Error::Protocol(ProtocolError {
kind: ProtocolErrorKind::Unknown,
message: format!("cannot skip field type {:?}", &u),
})),
}
}
async fn read_byte(&mut self) -> Result<u8>;
}
#[async_trait]
pub trait TOutputStreamProtocol: Send {
async fn write_message_begin(&mut self, identifier: &TMessageIdentifier) -> Result<usize>;
async fn write_message_end(&mut self) -> Result<usize>;
async fn write_struct_begin(&mut self, identifier: &TStructIdentifier) -> Result<usize>;
fn write_struct_end(&mut self) -> Result<usize>;
async fn write_field_begin(&mut self, identifier: &TFieldIdentifier) -> Result<usize>;
fn write_field_end(&mut self) -> Result<usize>;
async fn write_field_stop(&mut self) -> Result<usize>;
async fn write_bool(&mut self, b: bool) -> Result<usize>;
async fn write_bytes(&mut self, b: &[u8]) -> Result<usize>;
async fn write_i8(&mut self, i: i8) -> Result<usize>;
async fn write_i16(&mut self, i: i16) -> Result<usize>;
async fn write_i32(&mut self, i: i32) -> Result<usize>;
async fn write_i64(&mut self, i: i64) -> Result<usize>;
async fn write_double(&mut self, d: f64) -> Result<usize>;
async fn write_string(&mut self, s: &str) -> Result<usize>;
async fn write_list_begin(&mut self, identifier: &TListIdentifier) -> Result<usize>;
async fn write_list_end(&mut self) -> Result<usize>;
async fn write_set_begin(&mut self, identifier: &TSetIdentifier) -> Result<usize>;
async fn write_set_end(&mut self) -> Result<usize>;
async fn write_map_begin(&mut self, identifier: &TMapIdentifier) -> Result<usize>;
async fn write_map_end(&mut self) -> Result<usize>;
async fn flush(&mut self) -> Result<()>;
async fn write_byte(&mut self, b: u8) -> Result<usize>; }