use std::{convert::Infallible, marker::PhantomData};
use o3::{base::Either, buffer::resident::bound};
use crate::{
field, payload,
payload::{
fixed::Body,
response::{
self, declared, encoded, exact, headers,
status::{self, Code},
},
},
};
pub mod chunk;
mod counter;
pub(crate) use counter::Counter;
pub enum Sync {}
pub enum Polled<const KNOWN: bool = false> {}
#[derive(Debug)]
pub enum Never {}
#[doc(hidden)]
pub trait Extent<K>: Counter<K> {
type Count: Copy + Default;
fn remaining(_: &mut Self::Count) -> Option<&mut u64> {
None
}
}
impl<S> Extent<Sync> for S {
type Count = ();
}
impl<S> Extent<Polled> for S {
type Count = ();
}
impl<S> Extent<Polled<true>> for S {
type Count = u64;
fn remaining(count: &mut u64) -> Option<&mut u64> {
Some(count)
}
}
pub trait Source<Kind = Polled<true>>: Extent<Kind> {
fn length(&self) -> u64;
}
impl<S> Source<Polled> for S {
fn length(&self) -> u64 {
0
}
}
pub trait Transaction<'req, 'd, S, Ranges: Copy + 'd = (), Kind = Sync>: Sized {
type Done;
fn fixed<Status: status::Source, H, B>(
self,
status: Status::Value,
fields: H,
body: B,
) -> Self::Done
where
H: field::Source<headers::Application>,
B: response::Body<'d, Ranges>;
fn statics<'body: 'd, Status: status::Source, H>(
self,
status: Status::Value,
fields: H,
body: Body<'body>,
) -> Self::Done
where
H: field::Source<headers::Application>,
{
self.fixed::<Status, _, _>(status, fields, body)
}
fn sized_static<'body: 'd, Status: status::Source, H, const N: usize>(
self,
status: Status::Value,
fields: H,
body: &'body [u8; N],
) -> Self::Done
where
H: field::Source<headers::Application>,
{
self.statics::<Status, _>(status, fields, Body::new(body))
}
fn encoded<const EXACT: bool, Status: status::Source>(
self,
status: Status::Value,
fields: impl field::Source<headers::Application>,
body: impl encoded::Source<EXACT>,
) -> Self::Done;
fn stream<Status: status::Source>(
self,
status: Status::Value,
fields: impl field::Source<headers::Application>,
source: Option<S>,
) -> Self::Done
where
S: Source<Kind>;
}
pub trait Emit<'req, 'd, D = (), Ranges: Copy + 'd = ()>: Sized {
type Kind;
type Stream: Extent<Self::Kind>;
type Bound: bound::Bound + 'd;
const BODY: payload::Kind;
const DECLARED: Option<declared::Head<Self>> = None;
fn into_exact(self) -> Result<impl exact::Reply, Self> {
Err::<(Code, (), Infallible), _>(self)
}
fn consume<W: Transaction<'req, 'd, Self::Stream, Ranges, Self::Kind>>(
self,
writer: W,
) -> W::Done;
}
pub trait Success: headers::Fields {
const STATUS: Code;
}
impl<'req, 'd, Ranges: Copy + 'd, D: Success, B: response::Body<'d, Ranges>>
Emit<'req, 'd, D, Ranges> for B
{
type Kind = Sync;
type Stream = Never;
type Bound = <B as response::Body<'d, Ranges>>::Bound;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, writer: W) -> W::Done {
writer.fixed::<D, _, _>((), PhantomData::<D>, self)
}
}
impl<'req, 'd, Ranges: Copy + 'd, D: Success> Emit<'req, 'd, D, Ranges> 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, Never, Ranges>>(self, writer: W) -> W::Done {
writer.statics::<D, _>((), PhantomData::<D>, Body::new(b""))
}
}
impl<'req, 'd, Ranges: Copy + 'd, D, L, R> Emit<'req, 'd, D, Ranges> for Either<L, R>
where
L: Emit<'req, 'd, D, Ranges, Kind = Sync, Stream = Never>,
R: Emit<'req, 'd, D, Ranges, 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, Never, Ranges>>(self, writer: W) -> W::Done {
match self {
Self::Left(value) => <L as Emit<'req, 'd, D, Ranges>>::consume(value, writer),
Self::Right(value) => <R as Emit<'req, 'd, D, Ranges>>::consume(value, writer),
}
}
}
impl<'req, 'd, Ranges: Copy + 'd, D, T, E> Emit<'req, 'd, D, Ranges> for Result<T, E>
where
T: Emit<'req, 'd, D, Ranges, Kind = Sync, Stream = Never>,
E: Emit<'req, 'd, (), Ranges, 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 into_exact(self) -> Result<impl exact::Reply, Self> {
match self {
Ok(value) => <T as Emit<'req, 'd, D, Ranges>>::into_exact(value).map_err(Ok),
Err(error) => Err(Err(error)),
}
}
fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, writer: W) -> W::Done {
match self {
Ok(value) => <T as Emit<'req, 'd, D, Ranges>>::consume(value, writer),
Err(error) => <E as Emit<'req, 'd, (), Ranges>>::consume(error, writer),
}
}
}
pub struct Stream<S, H = (), const KNOWN: bool = false> {
status: Code,
headers: H,
stream: Option<S>,
}
impl<S, H, const KNOWN: bool> Stream<S, H, KNOWN> {
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 known(stream: Option<S>) -> Stream<S, (), true>
where
S: Source,
{
Stream {
status: Code::OK,
headers: (),
stream,
}
}
}
impl<S, const KNOWN: bool> Stream<S, (), KNOWN> {
pub fn headers<H: field::Source<headers::Application>>(
self,
headers: H,
) -> Stream<S, H, KNOWN> {
Stream {
status: self.status,
headers,
stream: self.stream,
}
}
}
impl<
'req,
'd,
Ranges: Copy + 'd,
D,
S: Source<Polled<KNOWN>>,
H: field::Source<headers::Application>,
const KNOWN: bool,
> Emit<'req, 'd, D, Ranges> for Stream<S, H, KNOWN>
{
type Kind = Polled<KNOWN>;
type Stream = S;
type Bound = bound::Unbounded;
const BODY: payload::Kind = payload::Kind::Stream;
fn consume<W: Transaction<'req, 'd, S, Ranges, Self::Kind>>(self, writer: W) -> W::Done {
writer.stream::<Code>(self.status, self.headers, self.stream)
}
}
impl<'req, 'd, Ranges: Copy + 'd, D> Emit<'req, 'd, D, Ranges> for Infallible {
type Kind = Sync;
type Stream = Never;
type Bound = bound::AtMost<0>;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, _: W) -> W::Done {
match self {}
}
}