sark-http 0.39.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use std::marker::PhantomData;

use o3::buffer::resident::bound;

use super::{Body, dispatch, encoded, headers::Application, status::Code};
use crate::{field, payload};

pub struct Output<H, B, F> {
    status: Code,
    fields: H,
    body: B,
    form: PhantomData<fn() -> F>,
}

impl<H: Copy> Copy for Output<H, payload::fixed::Body<'_>, Static> {}
impl<H: Copy> Clone for Output<H, payload::fixed::Body<'_>, Static> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<H, B, F> Output<H, B, F> {
    pub fn new(status: Code, fields: H, body: B) -> Self {
        Self {
            status,
            fields,
            body,
            form: PhantomData,
        }
    }

    pub fn status(&self) -> Code {
        self.status
    }

    pub fn body(&self) -> &B {
        &self.body
    }
}

pub enum Fixed {}
pub enum Static {}
pub struct Encoded<const EXACT: bool>;

impl<'req, 'd, D, H: field::Source<Application>, B: Body<'d>> dispatch::Response<'req, 'd, D>
    for Output<H, B, Fixed>
{
    type Kind = dispatch::Sync;
    type Stream = dispatch::Never;
    type Bound = bound::Unbounded;

    const BODY: payload::Kind = payload::Kind::Inline;

    fn consume<W: dispatch::Transaction<'req, 'd, Stream = dispatch::Never>>(
        self,
        writer: W,
    ) -> W::Done {
        writer.fixed(self.status, self.fields, self.body)
    }
}

impl<'body: 'd, 'req, 'd, D, H: field::Source<Application>> dispatch::Response<'req, 'd, D>
    for Output<H, payload::fixed::Body<'body>, Static>
{
    type Kind = dispatch::Sync;
    type Stream = dispatch::Never;
    type Bound = bound::AtMost<0>;

    const BODY: payload::Kind = payload::Kind::Static;

    fn consume<W: dispatch::Transaction<'req, 'd, Stream = dispatch::Never>>(
        self,
        writer: W,
    ) -> W::Done {
        writer.statics(self.status, self.fields, self.body)
    }
}

impl<'body: 'd, 'req, 'd, D, H: field::Source<Application>, const N: usize>
    dispatch::Response<'req, 'd, D> for Output<H, &'body [u8; N], Static>
{
    type Kind = dispatch::Sync;
    type Stream = dispatch::Never;
    type Bound = bound::AtMost<0>;

    const BODY: payload::Kind = payload::Kind::Static;

    fn consume<W: dispatch::Transaction<'req, 'd, Stream = dispatch::Never>>(
        self,
        writer: W,
    ) -> W::Done {
        writer.sized_static(self.status, self.fields, self.body)
    }
}

impl<'req, 'd, D, H: field::Source<Application>, B: encoded::Source<EXACT>, const EXACT: bool>
    dispatch::Response<'req, 'd, D> for Output<H, B, Encoded<EXACT>>
{
    type Kind = dispatch::Sync;
    type Stream = dispatch::Never;
    type Bound = bound::Unbounded;

    const BODY: payload::Kind = payload::Kind::Inline;

    fn consume<W: dispatch::Transaction<'req, 'd, Stream = dispatch::Never>>(
        self,
        writer: W,
    ) -> W::Done {
        writer.encoded::<EXACT>(self.status, self.fields, self.body)
    }
}