sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use std::ops;

use o3::buffer::write::{Sink, consume, edit};

use crate::field::{
    Indexing,
    metadata::{lower::plain::Error, map::record},
};

mod decoded;
pub mod prefix;
pub mod raw;

pub use decoded::Decoded;
use prefix::Prefix;
use record::Entry;

pub struct RangeIter<'a> {
    current: raw::Iter<'a>,
    offset: usize,
}

impl<'a> RangeIter<'a> {
    pub(in crate::field::metadata) fn new(current: raw::Iter<'a>, offset: usize) -> Self {
        Self { current, offset }
    }
}

impl<'a> Iterator for RangeIter<'a> {
    type Item = (Entry<'a>, ops::Range<usize>);

    fn next(&mut self) -> Option<Self::Item> {
        let field = self.current.next()?;
        let start = self.offset + Prefix::LEN + field.name.len();
        self.offset = start + field.value.len();
        Some((field, start..self.offset))
    }
}

pub struct Record<'a> {
    output: edit::Edit<'a, { Prefix::LEN }>,
}

impl<'a> Record<'a> {
    pub fn finish(mut self, name: usize, indexing: Indexing) -> Result<(), Error> {
        let payload = self.output.len() - Prefix::LEN;
        let value = payload
            .checked_sub(name)
            .ok_or(Error::Layout([0, name, payload]))?;
        *self.output.prefix() = Prefix::try_new(name, value, indexing)?.to_bytes();
        self.output.commit();
        Ok(())
    }
}

impl Sink for Record<'_> {
    type Error = consume::Error;

    fn write_slice(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        self.output.write_slice(bytes)
    }

    fn write_slices<const N: usize>(&mut self, slices: [&[u8]; N]) -> Result<(), Self::Error> {
        self.output.write_slices(slices)
    }
}