sark-http 0.40.0

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

use lower::plain;
use state::{
    decode::{Final, Indexed},
    fixed,
};

use crate::field::{
    self, head,
    metadata::{
        block, lower,
        map::record,
        section::{self, rules, state, storage},
    },
};

/// Complete fields preserve their selected rule without incremental staging.
pub trait Sink<P: section::Policy, C: block::Initial = block::Plan>:
    rules::Validate + Sized
{
    type Error: Into<lower::Error>;
    type Rejected: Into<lower::Error>;
    type Rule: Copy;

    fn h3(kind: block::Kind, connect: block::Connect) -> Self;
    fn select(&mut self, name: block::Named<'_>) -> Indexed<Self::Rule>;
    fn push<A: rules::Push>(
        &mut self,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
        rule: Self::Rule,
        name: record::Span<'_>,
        value: record::Span<'_>,
        indexing: field::Indexing,
    ) -> Result<(), Self::Rejected>;
    fn finish(
        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 Error = lower::Error;
    type Rejected = lower::Error;
    type Rule = rules::Decision<P::Inspect>;

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

    fn select(&mut self, name: block::Named<'_>) -> Indexed<Self::Rule> {
        match self.decide(name.span(), name.proof()) {
            rules::Decision::Skip => Indexed::Skip,
            rule => Indexed::Buffered(rule),
        }
    }

    fn push<A: rules::Push>(
        &mut self,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
        rule: Self::Rule,
        name: record::Span<'_>,
        value: record::Span<'_>,
        indexing: field::Indexing,
    ) -> Result<(), Self::Rejected> {
        lower::push::<P, A>(
            self,
            &mut writer.lease,
            scratch,
            rule,
            name,
            value,
            indexing,
        )
    }

    fn finish(
        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 Error = Infallible;
    type Rejected = plain::Error;
    type Rule = head::Name;

    fn h3(kind: block::Kind, connect: block::Connect) -> Self {
        Self {
            core: state::Core::new(kind, connect, block::Version::Three),
            selection: S::default(),
        }
    }

    fn select(&mut self, name: block::Named<'_>) -> Indexed<Self::Rule> {
        match self.accept(name.proof()) {
            block::Validation::Valid => Indexed::Buffered(name.proof().head()),
            block::Validation::Malformed | block::Validation::RejectPush => Indexed::Skip,
        }
    }

    fn push<A: rules::Push>(
        &mut self,
        writer: &mut storage::Writer<P::Block>,
        scratch: &mut storage::Scratch,
        known: Self::Rule,
        name: record::Span<'_>,
        value: record::Span<'_>,
        indexing: field::Indexing,
    ) -> Result<(), Self::Rejected> {
        self.push_field::<A>(&mut writer.lease, scratch, known, name, value, indexing)
    }

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

impl<C: block::Initial> Sink<rules::Discard, C> for fixed::Discard<C> {
    type Error = Infallible;
    type Rejected = lower::Error;
    type Rule = fixed::Omit;

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

    fn select(&mut self, name: block::Named<'_>) -> Indexed<Self::Rule> {
        if self.accept(name.proof()) != block::Validation::Valid {
            return Indexed::Skip;
        }
        match self.route(name.proof()) {
            fixed::Omit::Skip => Indexed::Skip,
            rule => Indexed::Buffered(rule),
        }
    }

    fn push<A: rules::Push>(
        &mut self,
        writer: &mut storage::Writer<storage::Fields>,
        scratch: &mut storage::Scratch,
        rule: Self::Rule,
        name: record::Span<'_>,
        value: record::Span<'_>,
        indexing: field::Indexing,
    ) -> Result<(), Self::Rejected> {
        match rule {
            fixed::Omit::Authority => {
                self.push_authority::<A>(&mut writer.lease, scratch, name, value, indexing)?
            }
            fixed::Omit::Discard(known) => {
                self.retain::<A>(
                    known,
                    record::View::new(name, value).with_index(indexing),
                    writer.lease.as_slice(),
                    scratch.authority(),
                );
            }
            fixed::Omit::Skip => {}
        }
        Ok(())
    }

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