sark-http 0.39.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use std::{convert::Infallible, ops::ControlFlow, ptr};

use o3::buffer::{bytes::scope, segments::Source, storage};

use crate::{
    owner::handoff::{Origin, Slot, stage},
    payload::{fixed, owned},
    request::body,
};

pub enum Selection<'d, H = Infallible, B = Infallible> {
    Empty,
    Owned(storage::NonEmpty<Vec<u8>>),
    Copied(storage::Shared),
    Retained(storage::Shared),
    Segmented(Source<'d>),
    Linked(Source<'d>),
    Fixed(fixed::Body<'d>),
    Body(B),
    Head(H),
}

impl<'req, 'd: 'req, H, B: stage::Body<'req, 'd>> Selection<'d, H, B> {
    pub(crate) fn try_borrow<Q: scope::Source<Selection = H>>(
        slot: &Slot<'req, 'd, Q, B>,
        bytes: &[u8],
    ) -> Result<Self, owned::Error> {
        match Self::try_select(slot, bytes)? {
            Some(selected) => Ok(selected),
            None => Self::try_copy(bytes),
        }
    }

    pub(crate) fn try_borrow_into<
        Q: scope::Source<Selection = H>,
        L: std::borrow::BorrowMut<usize>,
    >(
        slot: &Slot<'req, 'd, Q, B>,
        bytes: &[u8],
        out: &mut o3::buffer::write::Slice<'_, L>,
    ) -> Result<ControlFlow<usize, Self>, owned::Error> {
        if let Some(selected) = Self::try_select(slot, bytes)? {
            return Ok(ControlFlow::Continue(selected));
        }
        if let Some(written) = super::copy::into(out, bytes) {
            return Ok(ControlFlow::Break(written));
        }
        Self::try_copy(bytes).map(ControlFlow::Continue)
    }

    fn try_select<Q: scope::Source<Selection = H>>(
        slot: &Slot<'req, 'd, Q, B>,
        bytes: &[u8],
    ) -> Result<Option<Self>, owned::Error> {
        if bytes.is_empty() {
            return Ok(Some(Self::Empty));
        }
        if let Origin::Retained(body) = slot.origin {
            if body
                .borrow()
                .as_slice()
                .is_some_and(|whole| ptr::eq(whole, bytes))
            {
                return Ok(Some(Self::Body(body)));
            }
            if let Some(body) = body.borrow().try_slice(bytes)? {
                return Ok(Some(Self::Linked(body)));
            }
        }
        Ok(slot.head.select(bytes).map(Self::Head))
    }

    pub(crate) fn try_view<Q: scope::Source<Selection = H>>(
        slot: &Slot<'req, 'd, Q, B>,
        view: body::Ref<'_, 'd>,
    ) -> Result<Self, owned::Error> {
        if view.is_empty() {
            return Ok(Self::Empty);
        }
        match view.try_retained() {
            Ok(view) => {
                if let Origin::Retained(body) = slot.origin
                    && view.is_same(*body.borrow())
                {
                    return Ok(Self::Body(body));
                }
                Ok(Self::Segmented(view.try_clone()?))
            }
            Err(view) => Ok(Self::Copied(view.try_copy()?)),
        }
    }

    fn try_copy(bytes: &[u8]) -> Result<Self, owned::Error> {
        storage::Shared::try_copy(bytes)
            .map(Self::Copied)
            .map_err(owned::Error::Storage)
    }
}

impl<'d, H, B> From<storage::NonEmpty<Vec<u8>>> for Selection<'d, H, B> {
    fn from(body: storage::NonEmpty<Vec<u8>>) -> Self { Self::Owned(body) }
}

impl<'d, H, B> From<Option<storage::NonEmpty<Vec<u8>>>> for Selection<'d, H, B> {
    fn from(body: Option<storage::NonEmpty<Vec<u8>>>) -> Self {
        match body {
            Some(body) => Self::Owned(body),
            None => Self::Empty,
        }
    }
}