use std::future::Future;
use futures::Stream;
pub trait HeadersHandler {
fn headers(&self) -> Vec<(String, String)>;
fn header(&self, name: &str) -> Option<String>;
fn add_header(&self, name: &str, value: &str);
fn set_header(&self, name: &str, value: &str);
fn set_headers(&self, headers: Vec<(&str, &str)>);
fn remove_header(&self, name: &str);
}
#[derive(Clone, PartialEq, Eq, Debug, thiserror::Error)]
pub enum BodyError {
#[error("Body not sent")]
BodyNotSent,
#[error("Exceeded body size: {0}")]
ExceededBodySize(usize),
}
pub trait BodyHandler {
fn body(&self) -> Vec<u8>;
fn set_body(&self, body: &[u8]) -> Result<(), BodyError>;
}
pub trait HeadersBodyHandler: HeadersHandler + BodyHandler {}
pub trait IntoBodyState {
type BodyState: BodyState;
type BodyStateFuture: Future<Output = Self::BodyState>;
#[must_use]
fn into_body_state(self) -> Self::BodyStateFuture;
}
pub trait IntoBodyStreamState {
type BodyStreamState: BodyStreamState;
type BodyStreamStateFuture: Future<Output = Self::BodyStreamState>;
#[must_use]
fn into_body_stream_state(self) -> Self::BodyStreamStateFuture;
}
#[cfg(feature = "enable_stop_iteration")]
pub trait IntoHeadersBodyState {
type HeadersBodyState: HeadersBodyState;
type HeadersBodyStateFuture: Future<Output = Self::HeadersBodyState>;
#[must_use]
fn into_headers_body_state(self) -> Self::HeadersBodyStateFuture;
}
#[cfg(feature = "enable_stop_iteration")]
pub trait EntityState: IntoBodyState + IntoBodyStreamState + IntoHeadersBodyState {
type HeadersState: HeadersState;
type HeadersStateFuture: Future<Output = Self::HeadersState>;
#[must_use]
fn into_headers_state(self) -> Self::HeadersStateFuture;
}
#[cfg(feature = "enable_stop_iteration")]
pub trait HeadersState: IntoBodyState + IntoBodyStreamState + IntoHeadersBodyState {
fn handler(&self) -> &dyn HeadersHandler;
fn contains_body(&self) -> bool;
}
#[cfg(not(feature = "enable_stop_iteration"))]
pub trait EntityState: IntoBodyState + IntoBodyStreamState {
type HeadersState: HeadersState;
type HeadersStateFuture: Future<Output = Self::HeadersState>;
#[must_use]
fn into_headers_state(self) -> Self::HeadersStateFuture;
}
#[cfg(not(feature = "enable_stop_iteration"))]
pub trait HeadersState: IntoBodyState + IntoBodyStreamState {
fn handler(&self) -> &dyn HeadersHandler;
fn contains_body(&self) -> bool;
}
pub trait BodyState {
fn handler(&self) -> &dyn BodyHandler;
fn contains_body(&self) -> bool;
}
pub trait HeadersBodyState {
fn handler(&self) -> &dyn HeadersBodyHandler;
fn contains_body(&self) -> bool;
}
pub struct Chunk {
bytes: Vec<u8>,
}
impl Chunk {
#[cfg(feature = "experimental")]
pub fn new(bytes: Vec<u8>) -> Self {
Self { bytes }
}
#[cfg(not(feature = "experimental"))]
pub(crate) fn new(bytes: Vec<u8>) -> Self {
Self { bytes }
}
pub fn bytes(&self) -> &[u8] {
&self.bytes
}
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
}
pub trait BodyStreamState {
type Stream<'b>: Stream<Item = Chunk>
where
Self: 'b;
fn contains_body(&self) -> bool;
fn stream(&self) -> Self::Stream<'_>;
#[cfg(feature = "experimental")]
fn write_chunk(&self, chunk: Chunk) -> Result<(), BodyError>;
}