sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use crate::field::{
    Origin,
    metadata::{
        block,
        map::record::{Match, Span},
    },
};

/// Immutable entries with admitted values and cached name classes.
pub struct Table<'a, const N: usize> {
    entries: [(&'a [u8], &'a [u8]); N],
    names: [block::Name; N],
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error {
    pub index: usize,
}

/// One bounds-checked entry retaining its table and source bytes.
#[derive(Clone, Copy)]
pub struct Entry<'table, 'data> {
    bytes: &'table (&'data [u8], &'data [u8]),
    proof: block::Name,
}

impl<'a, const N: usize> Table<'a, N> {
    pub const fn try_new(entries: [(&'a [u8], &'a [u8]); N]) -> Result<Self, Error> {
        let mut names = [block::Name::REGULAR; N];
        let mut index = 0;
        while index < N {
            let (name, value) = entries[index];
            let mut scan = block::Value::new();
            let mut offset = 0;
            while offset < value.len() {
                scan.push(value[offset]);
                offset += 1;
            }
            if !scan.is_valid() {
                return Err(Error { index });
            }
            names[index] = block::Name::classify(name);
            index += 1;
        }
        Ok(Self { entries, names })
    }

    pub const fn len(&self) -> usize {
        N
    }

    pub const fn is_empty(&self) -> bool {
        N == 0
    }

    pub const fn entries(&self) -> &[(&'a [u8], &'a [u8]); N] {
        &self.entries
    }

    pub const fn declared<F: Origin, const ONE_BASED: bool>(
        &self,
    ) -> Option<Option<Match<usize, usize>>> {
        let Some(field) = F::FIELD else { return None };
        let field = field.as_record();
        let mut found = None;
        let mut row = 0;
        while row < N {
            let (name, value) = self.entries[row];
            if equal(name, field.name) {
                let index = row + ONE_BASED as usize;
                let first = match found {
                    Some(Match { name, .. }) => name,
                    None => index,
                };
                if equal(value, field.value) {
                    return Some(Some(Match {
                        name: first,
                        exact: Some(index),
                    }));
                }
                found = Some(Match {
                    name: first,
                    exact: None,
                });
            }
            row += 1;
        }
        Some(found)
    }

    pub fn get(&self, index: usize) -> Option<Entry<'_, 'a>> {
        let bytes = self.entries.get(index)?;
        Some(Entry {
            bytes,
            proof: self.names[index],
        })
    }
}

const fn equal(left: &[u8], right: &[u8]) -> bool {
    if left.len() != right.len() {
        return false;
    }
    let mut index = 0;
    while index < left.len() {
        if left[index] != right[index] {
            return false;
        }
        index += 1;
    }
    true
}

impl<'a> Entry<'_, 'a> {
    pub const fn name(self) -> block::Named<'a> {
        block::Named::from_scanned(Span::from_slice(self.bytes.0), self.proof)
    }

    pub const fn value(self) -> &'a [u8] {
        self.bytes.1
    }

    pub const fn valued(self) -> block::Valued<'a> {
        block::Valued::from_scanned(Span::from_slice(self.bytes.1), block::Validation::Valid)
    }

    pub const fn validation(self) -> block::Validation {
        if self.proof.is_forbidden() {
            block::Validation::Malformed
        } else {
            block::Validation::Valid
        }
    }
}

const _: () = assert!(core::mem::size_of::<Entry<'_, '_>>() == 2 * core::mem::size_of::<usize>());