use std::future::Future;
use hyper::body::Bytes;
use serde::de::DeserializeOwned;
use super::error::GetBodyError;
mod impl_http;
#[cfg(feature = "websocket")]
mod impl_websocket;
pub trait ContextCreateBodyExt {
fn body_exists<T: DeserializeOwned + Send + Sync + 'static>(&self) -> bool;
fn is_body_parsed(&self) -> bool;
fn mark_body_parsed(&mut self);
fn extract_body_bytes(&mut self) -> impl Future<Output = Result<Bytes, GetBodyError>>;
fn create_body<T: DeserializeOwned + Send + Sync + 'static>(
&mut self,
) -> impl Future<Output = Result<T, GetBodyError>> {
async {
let incoming_bytes = self.extract_body_bytes().await?;
if incoming_bytes.is_empty() && self.is_body_parsed() {
return Err(GetBodyError::AlreadyParsed);
}
self.mark_body_parsed();
self.decode_data(&incoming_bytes)
}
}
fn decode_data<T: DeserializeOwned + Send + Sync + 'static>(
&mut self,
bytes_to_decode: &[u8],
) -> Result<T, GetBodyError> {
Ok(serde_json::from_slice(bytes_to_decode)?)
}
fn insert_body<T: DeserializeOwned + Send + Sync + 'static>(
&mut self,
) -> impl Future<Output = Result<(), GetBodyError>>;
}
pub trait ContextGetBodyExt: ContextCreateBodyExt {
fn body<T: DeserializeOwned + Send + Sync + 'static>(
&mut self,
) -> impl Future<Output = Result<&T, GetBodyError>>;
fn body_mut<T: DeserializeOwned + Send + Sync + 'static>(
&mut self,
) -> impl Future<Output = Result<&mut T, GetBodyError>>;
fn remove_body<T: DeserializeOwned + Send + Sync + 'static>(
&mut self,
) -> impl Future<Output = Result<T, GetBodyError>>;
}