sark-http 0.40.0

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

use crate::field::{
    self, head,
    metadata::{
        block,
        hpack::decode::mode::Mode,
        lower,
        lower::{scratch::Scratch, tagged::Tagged},
        section::{
            self, rules,
            state::{self, complete, decode::Final, fixed},
            storage,
        },
        strings,
    },
};

type Begin<R, D, S> = Result<Mode<R, D, S>, lower::Error>;

pub trait Sink<P: section::Policy, C: block::Initial>: complete::Sink<P, C> {
    type Direct;
    type Scratch;

    fn h2(kind: block::Kind, connect: block::Connect) -> Self;
    fn begin(
        &mut self,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
        name: block::Named<'_>,
        indexing: field::Indexing,
    ) -> Begin<Self::Rule, Self::Direct, Self::Scratch>;
    fn string(
        field: &mut Self::Direct,
        writer: &mut storage::Writer<P::Block>,
        cursor: &mut strings::Cursor,
        input: &[u8],
    ) -> Result<(), lower::StringError>;
    fn scratch_string(
        field: &mut Self::Scratch,
        cursor: &mut strings::Cursor,
        input: &[u8],
    ) -> Result<(), lower::StringError>;
    fn finish_direct<A: rules::Push>(
        &mut self,
        field: Self::Direct,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
    );
    fn finish_scratch<A: rules::Push>(
        &mut self,
        field: Self::Scratch,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
    );
    fn rollback_direct(field: Self::Direct, writer: &mut storage::Writer<P::Block>);
    fn rollback_scratch(field: Self::Scratch, scratch: &mut storage::Scratch);
    fn seal(
        self,
        writer: storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
    ) -> Result<Final<P, C>, Self::Error>;
}

impl<P: rules::Plan, C: block::Initial> Sink<P, C> for state::Decoder<P, C> {
    type Direct = Tagged;
    type Scratch = Scratch;

    fn h2(kind: block::Kind, connect: block::Connect) -> Self {
        Self {
            core: state::Core::new(kind, connect, block::Version::Two),
            policy: P::default(),
        }
    }

    fn begin(
        &mut self,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
        name: block::Named<'_>,
        indexing: field::Indexing,
    ) -> Begin<Self::Rule, Tagged, Scratch> {
        let known = name.proof().head();
        let names = self.core.control.next_name(known);
        let start = match self.decision(name.span(), known, names.check) {
            rules::Decision::Tagged(known, tag) => {
                Mode::Direct(Tagged::begin(&mut writer.lease, known, tag, indexing)?)
            }
            rules::Decision::Scratch => Mode::Scratch(Scratch::begin(scratch, indexing)),
            rules::Decision::Skip => Mode::Validate,
            rule => Mode::Buffered(rule),
        };
        self.core.control.names = names;
        Ok(start)
    }

    fn string(
        field: &mut Tagged,
        writer: &mut storage::Writer<P::Block>,
        cursor: &mut strings::Cursor,
        input: &[u8],
    ) -> Result<(), lower::StringError> {
        field.try_string(&mut writer.lease, cursor, input)
    }

    fn scratch_string(
        field: &mut Scratch,
        cursor: &mut strings::Cursor,
        input: &[u8],
    ) -> Result<(), lower::StringError> {
        field.try_string(cursor, input)
    }

    fn finish_direct<A: rules::Push>(
        &mut self,
        field: Tagged,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
    ) {
        field.finish::<P, A, C>(&mut writer.lease, scratch, self);
    }

    fn finish_scratch<A: rules::Push>(
        &mut self,
        field: Scratch,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
    ) {
        field.finish::<P, A, C>(&mut writer.lease, scratch, self);
    }

    fn rollback_direct(field: Tagged, writer: &mut storage::Writer<P::Block>) {
        field.rollback(&mut writer.lease);
    }

    fn rollback_scratch(field: Scratch, scratch: &mut storage::Scratch) {
        field.rollback(scratch);
    }

    fn seal(
        self,
        writer: storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
    ) -> Result<Final<P, C>, lower::Error> {
        self.finish_block(writer, scratch)
    }
}

impl<P, C: block::Initial, S: rules::RawSelection> Sink<P, C> for fixed::Raw<C, S>
where
    P: section::Policy<Selection = S, Block: storage::Block<Format = storage::Raw>>,
{
    type Direct = Infallible;
    type Scratch = Infallible;

    fn h2(kind: block::Kind, connect: block::Connect) -> Self {
        Self::h2(kind, connect)
    }

    fn begin(
        &mut self,
        _: &mut storage::Writer<P::Block>,
        _: &mut storage::Scratch,
        name: block::Named<'_>,
        _: field::Indexing,
    ) -> Begin<head::Name, Infallible, Infallible> {
        Ok(if self.accept(name.proof()) == block::Validation::Valid {
            Mode::Buffered(name.proof().head())
        } else {
            Mode::Validate
        })
    }

    fn string(
        field: &mut Infallible,
        _: &mut storage::Writer<P::Block>,
        _: &mut strings::Cursor,
        _: &[u8],
    ) -> Result<(), lower::StringError> {
        match *field {}
    }

    fn scratch_string(
        field: &mut Infallible,
        _: &mut strings::Cursor,
        _: &[u8],
    ) -> Result<(), lower::StringError> {
        match *field {}
    }

    fn finish_direct<A: rules::Push>(
        &mut self,
        field: Infallible,
        _: &mut storage::Writer<P::Block>,
        _: &mut storage::Scratch,
    ) {
        match field {}
    }

    fn finish_scratch<A: rules::Push>(
        &mut self,
        field: Infallible,
        _: &mut storage::Writer<P::Block>,
        _: &mut storage::Scratch,
    ) {
        match field {}
    }

    fn rollback_direct(field: Infallible, _: &mut storage::Writer<P::Block>) {
        match field {}
    }
    fn rollback_scratch(field: Infallible, _: &mut storage::Scratch) {
        match field {}
    }

    fn seal(
        self,
        writer: storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
    ) -> Result<Final<P, C>, Infallible> {
        scratch.clear();
        let outcome = C::finish(&self.core.control);
        Ok(Final::new(
            writer.finish(),
            self.selection,
            block::Validation::Valid,
            outcome,
            self.core.control.framing(),
        ))
    }
}

impl<C: block::Initial> Sink<rules::Discard, C> for fixed::Discard<C> {
    type Direct = Tagged;
    type Scratch = Infallible;

    fn h2(kind: block::Kind, connect: block::Connect) -> Self {
        Self {
            core: state::Core::new(kind, connect, block::Version::Two),
        }
    }

    fn begin(
        &mut self,
        writer: &mut storage::Writer<storage::Fields>,
        _: &mut storage::Scratch,
        name: block::Named<'_>,
        indexing: field::Indexing,
    ) -> Begin<fixed::Omit, Tagged, Infallible> {
        let rule = self.route(name.proof());
        let names = self.core.control.next_name(name.proof().head());
        let start = match (names.check, rule) {
            (block::Validation::Valid, fixed::Omit::Authority) => Mode::Direct(Tagged::begin(
                &mut writer.lease,
                head::Name::Authority,
                storage::Tag::AUTHORITY,
                indexing,
            )?),
            (block::Validation::Valid, fixed::Omit::Discard(known)) => {
                Mode::Buffered(fixed::Omit::Discard(known))
            }
            _ => Mode::Validate,
        };
        self.core.control.names = names;
        Ok(start)
    }

    fn string(
        field: &mut Tagged,
        writer: &mut storage::Writer<storage::Fields>,
        cursor: &mut strings::Cursor,
        input: &[u8],
    ) -> Result<(), lower::StringError> {
        field.try_string(&mut writer.lease, cursor, input)
    }

    fn scratch_string(
        field: &mut Infallible,
        _: &mut strings::Cursor,
        _: &[u8],
    ) -> Result<(), lower::StringError> {
        match *field {}
    }

    fn finish_direct<A: rules::Push>(
        &mut self,
        field: Tagged,
        writer: &mut storage::Writer<storage::Fields>,
        scratch: &mut storage::Scratch,
    ) {
        field.omit::<A, C>(&mut writer.lease, scratch, self);
    }

    fn finish_scratch<A: rules::Push>(
        &mut self,
        field: Infallible,
        _: &mut storage::Writer<storage::Fields>,
        _: &mut storage::Scratch,
    ) {
        match field {}
    }

    fn rollback_direct(field: Tagged, writer: &mut storage::Writer<storage::Fields>) {
        field.rollback(&mut writer.lease);
    }
    fn rollback_scratch(field: Infallible, _: &mut storage::Scratch) {
        match field {}
    }

    fn seal(
        self,
        writer: storage::Writer<storage::Fields>,
        scratch: &mut storage::Scratch,
    ) -> Result<Final<rules::Discard, C>, Infallible> {
        Ok(self.finish_fields(writer, scratch))
    }
}