sark-http 0.40.0

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

use crate::h1::chunk::{Boundary, Fragment, Run, trailer};

pub(super) mod state;

impl From<trailer::Pending<'_>> for NonZero<usize> {
    fn from(pending: trailer::Pending<'_>) -> Self {
        pending.consumed()
    }
}

/// Output selection for the shared framing machine. Whole scans continue at
/// data boundaries; fragment decodes retain each narrowed transition result.
pub(in crate::h1) trait Mode<'input>: Sized {
    fn data(consumed: NonZero<usize>, bytes: &'input [u8], boundary: Boundary) -> Option<Self>;

    fn pending(input: &'input [u8], consumed: usize) -> Self;

    fn trailer(input: &'input [u8], pending: trailer::Pending<'input>) -> Self;

    fn complete(input: &'input [u8], consumed: NonZero<usize>, bytes: &'input [u8]) -> Self;

    fn finished(input: &'input [u8]) -> Self;
}

impl<'input> Mode<'input> for Run<'input> {
    fn data(_: NonZero<usize>, _: &'input [u8], _: Boundary) -> Option<Self> {
        None
    }

    fn pending(input: &'input [u8], consumed: usize) -> Self {
        Run::pending(input, consumed)
    }

    fn trailer(input: &'input [u8], pending: trailer::Pending<'input>) -> Self {
        Run::pending(input, pending.consumed().get())
    }

    fn complete(input: &'input [u8], consumed: NonZero<usize>, _: &'input [u8]) -> Self {
        Run::complete(input, consumed.get())
    }

    fn finished(input: &'input [u8]) -> Self {
        Run::complete(input, 0)
    }
}

impl<'input, T: From<trailer::Pending<'input>>> Mode<'input> for Fragment<'input, T> {
    fn data(consumed: NonZero<usize>, bytes: &'input [u8], boundary: Boundary) -> Option<Self> {
        Some(Self::Data {
            consumed,
            bytes,
            boundary,
        })
    }

    fn pending(_: &'input [u8], consumed: usize) -> Self {
        Self::More(consumed)
    }

    fn trailer(_: &'input [u8], pending: trailer::Pending<'input>) -> Self {
        Self::Trailer(pending.into())
    }

    fn complete(_: &'input [u8], consumed: NonZero<usize>, bytes: &'input [u8]) -> Self {
        Self::Complete {
            consumed,
            bytes: bytes
                .split_last_chunk::<2>()
                .map_or(&bytes[..0], |(fields, _)| fields),
        }
    }

    fn finished(_: &'input [u8]) -> Self {
        Self::More(0)
    }
}