sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
//! Response-head semantics and owner-free wire parts.

mod admitted;
mod coding;
mod date;
mod fields;
mod parts;
pub mod raw;
mod server;

pub use admitted::Admitted;
pub use coding::Coding;
pub use date::Date;
pub use parts::Parts;
pub use server::Server;

use core::convert::Infallible;

use o3::buffer::{
    self,
    resident::staging::{self, reserve},
    write,
};

use crate::h1::head::{Limit, ParseError, field};
use crate::payload::response::Method;

pub struct State<S> {
    source: S,
    inner: raw::State,
}

pub struct Progress<'a> {
    source: &'a [u8],
    inner: &'a mut raw::State,
}

impl<S> State<S> {
    pub fn new(source: S, request: Method, limit: Limit) -> Self {
        Self {
            source,
            inner: raw::State::new(request, limit),
        }
    }

    pub fn into_inner(self) -> S {
        self.source
    }
}

impl<S: buffer::raw::Source> State<S> {
    pub fn source(&self) -> &S {
        &self.source
    }

    pub fn progress(&mut self) -> Progress<'_> {
        Progress {
            source: self.source.as_ref(),
            inner: &mut self.inner,
        }
    }
}

impl<'a> Progress<'a> {
    pub fn read<E>(&mut self) -> Result<Option<field::Entry<'a>>, ParseError<E>> {
        // SAFETY: this loan keeps the same append-only source unchanged.
        unsafe { self.inner.read_from(self.source) }
    }

    pub fn parts(&self) -> Option<Parts> {
        self.inner.parts()
    }

    pub fn scan(mut self) -> Result<Option<Parts>, ParseError<Infallible>> {
        while self.read::<Infallible>()?.is_some() {}
        Ok(self.parts())
    }
}

impl<S: buffer::raw::Source> AsRef<[u8]> for State<S> {
    fn as_ref(&self) -> &[u8] {
        self.source.as_ref()
    }
}

impl<S: buffer::raw::Source + write::Sink> State<S> {
    pub fn try_extend(&mut self, bytes: &[u8]) -> Result<(), S::Error> {
        self.source.write_slice(bytes)
    }
}

impl<'d> State<staging::Buf<'d>> {
    pub fn try_reserve(&mut self, target: usize) -> Result<(), reserve::Error> {
        self.source.try_reserve(target)
    }

    pub fn try_grow(&mut self) -> Result<(), reserve::Error> {
        self.source.try_grow()
    }
}

const _: () = assert!(core::mem::size_of::<State<()>>() <= 104);