sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use core::convert::Infallible;

use crate::field::{
    Indexing,
    metadata::{block, section::rules, strings::Cursor},
};

use crate::field::metadata::hpack::decode::{self, limit, strings::Retention};

#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Index {
    Plain,
    Never,
    Indexed,
}

impl Index {
    pub(super) fn indexing(self) -> Indexing {
        match self {
            Self::Never => Indexing::Never,
            Self::Plain | Self::Indexed => Indexing::Allowed,
        }
    }
}

#[derive(Clone, Copy)]
pub(super) struct Facts {
    pub(super) len: usize,
    pub(super) index: Index,
}

pub enum Mode<R, D, S> {
    Validate,
    IndexOnly,
    Buffered(R),
    Direct(D),
    Scratch(S),
}

pub(super) enum Route<'a, D, S> {
    Retain,
    Count,
    Validate,
    Direct(&'a mut D),
    Scratch(&'a mut S),
}

pub(super) enum Record<'a> {
    Retained(block::Named<'a>, block::Valued<'a>),
    Counted(usize),
}

impl Record<'_> {
    pub(super) fn len(&self) -> usize {
        match self {
            Self::Retained(_, value) => value.span().len(),
            Self::Counted(len) => *len,
        }
    }
}

pub(super) trait Kind {
    type Active;
    type Direct;
    type Scratch;
    type Error: From<decode::Error>;

    const NAMES: Retention;
    fn facts(active: &Self::Active) -> Facts;
    fn route(active: &mut Self::Active) -> Route<'_, Self::Direct, Self::Scratch>;
    fn validate(sink: &mut Self, value: block::Validation);
    fn begin(
        sink: &mut Self,
        span: Option<block::Named<'_>>,
        facts: Facts,
    ) -> Result<Self::Active, Self::Error>;
    fn field<A: rules::Push>(
        sink: &mut Self,
        budget: &mut limit::Budget,
        name: block::Named<'_>,
        value: block::Valued<'_>,
    ) -> Result<(), Self::Error>;
    fn finish<A: rules::Push>(
        active: Self::Active,
        sink: &mut Self,
        budget: &mut limit::Budget,
        record: Record<'_>,
    ) -> Result<(), Self::Error>;
    fn string(
        field: &mut Self::Direct,
        sink: &mut Self,
        string: &mut Cursor,
        input: &[u8],
    ) -> Result<(), Self::Error>;
    fn scratch_string(
        field: &mut Self::Scratch,
        string: &mut Cursor,
        input: &[u8],
    ) -> Result<(), Self::Error>;
    fn rollback(active: Self::Active, sink: &mut Self);
    fn failed<T>(
        table: T,
        active: Self::Active,
        sink: &mut Self,
        error: Self::Error,
    ) -> Self::Error {
        drop(table);
        Self::rollback(active, sink);
        error
    }
}

impl Kind for () {
    type Active = Facts;
    type Direct = Infallible;
    type Scratch = Infallible;
    type Error = decode::Error;

    const NAMES: Retention = Retention::Counted;
    fn facts(active: &Facts) -> Facts {
        *active
    }
    fn route(_: &mut Facts) -> Route<'_, Infallible, Infallible> {
        Route::Count
    }
    fn validate(_: &mut (), _: block::Validation) {}
    fn begin(
        _: &mut (),
        _: Option<block::Named<'_>>,
        facts: Facts,
    ) -> Result<Facts, decode::Error> {
        Ok(facts)
    }
    fn field<A: rules::Push>(
        _: &mut (),
        budget: &mut limit::Budget,
        name: block::Named<'_>,
        value: block::Valued<'_>,
    ) -> Result<(), decode::Error> {
        budget.admit(name.span().len(), value.span().len());
        Ok(())
    }
    fn finish<A: rules::Push>(
        active: Facts,
        _: &mut (),
        budget: &mut limit::Budget,
        record: Record<'_>,
    ) -> Result<(), decode::Error> {
        budget.admit(active.len, record.len());
        Ok(())
    }
    fn string(
        field: &mut Infallible,
        _: &mut (),
        _: &mut Cursor,
        _: &[u8],
    ) -> Result<(), decode::Error> {
        match *field {}
    }
    fn scratch_string(
        field: &mut Infallible,
        _: &mut Cursor,
        _: &[u8],
    ) -> Result<(), decode::Error> {
        match *field {}
    }
    fn rollback(_: Facts, _: &mut ()) {}
}