use core::convert::Infallible;
use crate::h1::{
field::{self, syntax},
head::{Limit, ParseError, Semantic, line, tail::Tail},
};
pub(super) trait Kind: Clone + Copy {
type Start: Clone + Copy;
type Fields: Clone + Copy + Default;
type Parts: Clone + Copy;
fn start<const CLOSED: bool, E>(
self,
bytes: &[u8],
) -> Result<line::Progress<Self::Start>, ParseError<E>>;
fn fields(start: Self::Start) -> u32;
fn observe(
start: &Self::Start,
fields: Self::Fields,
field: &field::Entry<'_>,
) -> Result<Self::Fields, Semantic>;
fn finish<E>(
fields: Self::Fields,
start: Self::Start,
tail: Tail,
) -> Result<Self::Parts, ParseError<E>>;
}
#[derive(Clone)]
pub(super) struct State<K: Kind> {
phase: Phase<K>,
}
#[derive(Clone, Copy)]
enum Phase<K: Kind> {
Start {
kind: K,
limit: Limit,
},
Line {
kind: K,
cursor: line::Cursor,
limit: Limit,
},
Fields {
cursor: field::Cursor,
start: K::Start,
fields: K::Fields,
bytes: usize,
},
Done(K::Parts),
}
impl<K: Kind> State<K> {
pub(super) const fn new(kind: K, limit: Limit) -> Self {
Self {
phase: Phase::Start { kind, limit },
}
}
pub(super) fn read<'owner, E>(
&mut self,
owner: &'owner [u8],
) -> Result<Option<field::Entry<'owner>>, ParseError<E>> {
loop {
match &mut self.phase {
Phase::Start { kind, limit } => {
let bounded = owner.len().min(limit.bytes());
let progress = kind.start::<false, E>(&owner[..bounded])?;
match progress {
line::Progress::Complete(start) => {
self.phase = Self::fields(start, *limit).phase;
}
line::Progress::Partial(cursor) => {
self.phase = Phase::Line {
kind: *kind,
cursor,
limit: *limit,
};
if owner.len() == bounded {
return Ok(None);
}
}
}
}
Phase::Line {
kind,
cursor,
limit,
} => {
let (next, end) = cursor
.scan(owner, limit.bytes())
.map_err(ParseError::Boundary)?;
let Some(prefix) = end else {
*cursor = next;
return Ok(None);
};
let progress = kind.start::<true, E>(prefix)?;
let line::Progress::Complete(start) = progress else {
return Err(ParseError::invalid());
};
self.phase = Self::fields(start, *limit).phase;
}
Phase::Fields {
cursor,
start,
fields,
bytes,
} => {
let bounded = &owner[..owner.len().min(*bytes)];
let unread = match bounded.get(cursor.next()..) {
Some(bytes) => bytes,
None => &[],
};
let mut next = *cursor;
match next.read(unread).map_err(ParseError::Field)? {
field::Progress::Field(line) => {
let token = bounded
.get(line.start()..)
.ok_or(ParseError::Field(syntax::Error::Invalid))?;
let field = line.bind(token).map_err(ParseError::Field)?;
let admitted =
K::observe(start, *fields, &field).map_err(ParseError::Semantic)?;
*cursor = next;
*fields = admitted;
return Ok(Some(field));
}
field::Progress::More if owner.len() > *bytes => {
return Err(ParseError::too_large(owner.len(), *bytes));
}
field::Progress::More => {
*cursor = next;
return Ok(None);
}
field::Progress::End(end) => {
let tail =
Tail::new(K::fields(*start), end).map_err(ParseError::Boundary)?;
self.phase = Phase::Done(K::finish(*fields, *start, tail)?);
return Ok(None);
}
}
}
Phase::Done(_) => return Ok(None),
}
}
}
pub(super) fn parts(&self) -> Option<K::Parts> {
match self.phase {
Phase::Done(parts) => Some(parts),
_ => None,
}
}
pub(super) fn scan(
mut self,
owner: &[u8],
) -> Result<(Self, Option<K::Parts>), ParseError<Infallible>> {
while self.read::<Infallible>(owner)?.is_some() {}
let parts = self.parts();
Ok((self, parts))
}
pub(super) fn fields(start: K::Start, limit: Limit) -> Self {
Self {
phase: Phase::Fields {
cursor: field::Cursor::new(K::fields(start), limit.fields()),
start,
fields: K::Fields::default(),
bytes: limit.bytes(),
},
}
}
}