use core::{convert::Infallible, num::NonZero};
use o3::{
buffer::{self, bytes::window::Snapshot, storage, write},
mem::Rejected,
};
use scan::Progress;
use write::CopyError;
use crate::h1::head::{
Limit, ParseError, field,
line::{Owned, Request},
request::{Kind, Parts, plan::Start, scan},
state,
};
pub struct State<S> {
source: S,
inner: state::State<Kind>,
}
impl<S> State<S> {
pub const fn new(source: S, limit: Limit) -> Self {
Self {
source,
inner: state::State::new(Kind, limit),
}
}
}
impl<'a> State<&'a [u8]> {
pub fn read_borrowed<E>(&mut self) -> Result<Option<field::Entry<'a>>, ParseError<E>> {
self.inner.read(self.source)
}
pub fn try_request(request: Request<'a>, limit: Limit) -> Result<Self, ParseError<Infallible>> {
let start = Start::try_new(request, limit)?;
Ok(Self {
source: request.source(),
inner: state::State::fields(start, limit),
})
}
}
impl<const MAX: usize> State<Snapshot<MAX>> {
pub fn try_line(
source: Owned<MAX>,
limit: Limit,
) -> Result<Self, Rejected<Owned<MAX>, ParseError<Infallible>>> {
let start = match Start::try_new(source.request(), limit) {
Ok(start) => start,
Err(error) => return Err(Rejected::new(source, error)),
};
Ok(Self {
source: source.into_source(),
inner: state::State::fields(start, limit),
})
}
}
impl<S: buffer::raw::Source> State<S> {
pub fn try_snapshot<const MAX: usize>(
&self,
capacity: NonZero<usize>,
) -> Result<State<Snapshot<MAX>>, storage::AllocError> {
let source = Snapshot::try_copy(self.source.as_ref(), capacity)?;
Ok(State {
source,
inner: self.inner.clone(),
})
}
pub fn try_copy<D>(&self, target: D) -> Result<State<D>, (D, CopyError<D::Error>)>
where
D: buffer::raw::Source + write::Sink,
{
let target = write::try_copy(target, self.source.as_ref())?;
Ok(State {
source: target,
inner: self.inner.clone(),
})
}
pub fn read<E>(&mut self) -> Result<Option<field::Entry<'_>>, ParseError<E>> {
self.inner.read(self.source.as_ref())
}
pub fn finish(self) -> Progress<S> {
match self.inner.parts() {
Some(head) => Progress::Complete(Parts::new(self.source, head)),
None => Progress::More(self),
}
}
}
impl<S: buffer::raw::Source> AsRef<[u8]> for State<S> {
fn as_ref(&self) -> &[u8] {
self.source.as_ref()
}
}
impl<S: write::Sink> State<S> {
pub fn try_extend(&mut self, bytes: &[u8]) -> Result<(), S::Error> {
self.source.write_slice(bytes)
}
}
const _: () = assert!(core::mem::size_of::<State<()>>() <= 144);