use o3::{base::Either, buffer::resident::bound};
use crate::{
field, payload,
payload::{
fixed::Body,
response::{self, declared, encoded, headers, status::Code},
},
};
pub mod chunk;
pub enum Sync {}
pub enum Polled {}
#[derive(Debug)]
pub enum Never {}
pub trait Transaction<'req, 'd>: Sized {
type Stream;
type Done;
fn fixed<H, B>(self, status: Code, fields: H, body: B) -> Self::Done
where
H: field::Source<headers::Application>,
B: response::Body<'d>;
fn statics<'body: 'd, H>(self, status: Code, fields: H, body: Body<'body>) -> Self::Done
where
H: field::Source<headers::Application>;
fn sized_static<'body: 'd, H, const N: usize>(
self,
status: Code,
fields: H,
body: &'body [u8; N],
) -> Self::Done
where
H: field::Source<headers::Application>,
{
self.statics(status, fields, Body::new(body))
}
fn encoded<const EXACT: bool>(
self,
status: Code,
fields: impl field::Source<headers::Application>,
body: impl encoded::Source<EXACT>,
) -> Self::Done;
fn stream(
self,
status: Code,
fields: impl field::Source<headers::Application>,
source: Option<Self::Stream>,
) -> Self::Done;
}
pub trait Response<'req, 'd, D = ()>: Sized {
type Kind;
type Stream;
type Bound: bound::Bound + 'd;
const BODY: payload::Kind;
const DECLARED: Option<declared::Head<Self>> = None;
fn consume<W: Transaction<'req, 'd, Stream = Self::Stream>>(self, writer: W) -> W::Done;
}
pub trait Success {
const STATUS: Code;
const FIELDS: headers::Group<'static>;
}
impl<'req, 'd, D: Success, B: response::Body<'d>> Response<'req, 'd, D> for B {
type Kind = Sync;
type Stream = Never;
type Bound = <B as response::Body<'d>>::Bound;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
writer.fixed(D::STATUS, D::FIELDS, self)
}
}
impl<'req, 'd, D: Success> Response<'req, 'd, D> for () {
type Kind = Sync;
type Stream = Never;
type Bound = bound::AtMost<0>;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
writer.statics(D::STATUS, D::FIELDS, Body::new(b""))
}
}
impl<'req, 'd, D, L, R> Response<'req, 'd, D> for Either<L, R>
where
L: Response<'req, 'd, D, Kind = Sync, Stream = Never>,
R: Response<'req, 'd, D, Kind = Sync, Stream = Never>,
{
type Kind = Sync;
type Stream = Never;
type Bound = bound::Max<L::Bound, R::Bound>;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
match self {
Self::Left(value) => <L as Response<'req, 'd, D>>::consume(value, writer),
Self::Right(value) => <R as Response<'req, 'd, D>>::consume(value, writer),
}
}
}
impl<'req, 'd, D, T, E> Response<'req, 'd, D> for Result<T, E>
where
T: Response<'req, 'd, D, Kind = Sync, Stream = Never>,
E: Response<'req, 'd, (), Kind = Sync, Stream = Never>,
{
type Kind = Sync;
type Stream = Never;
type Bound = bound::Max<T::Bound, E::Bound>;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
match self {
Ok(value) => <T as Response<'req, 'd, D>>::consume(value, writer),
Err(error) => <E as Response<'req, 'd, ()>>::consume(error, writer),
}
}
}
pub struct Stream<S, H = ()> {
status: Code,
headers: H,
stream: Option<S>,
}
impl<S, H> Stream<S, H> {
pub fn with_status(mut self, status: Code) -> Self {
self.status = status;
self
}
}
impl<S> Stream<S> {
pub fn new(stream: Option<S>) -> Self {
Self {
status: Code::OK,
headers: (),
stream,
}
}
pub fn headers<H: field::Source<headers::Application>>(self, headers: H) -> Stream<S, H> {
Stream {
status: self.status,
headers,
stream: self.stream,
}
}
}
impl<'req, 'd, D, S, H: field::Source<headers::Application>> Response<'req, 'd, D>
for Stream<S, H>
{
type Kind = Polled;
type Stream = S;
type Bound = bound::Unbounded;
const BODY: payload::Kind = payload::Kind::Stream;
fn consume<W: Transaction<'req, 'd, Stream = S>>(self, writer: W) -> W::Done {
writer.stream(self.status, self.headers, self.stream)
}
}