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()
}
}
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)
}
}