Skip to main content

Parser

Struct Parser 

Source
pub struct Parser<'b, const LINE: usize = 512, const MAX_HEADERS: usize = 16> { /* private fields */ }
Expand description

Streaming, zero-copy multipart/form-data parser.

LINE is the size of the only buffer the parser owns: a part header line (including its CRLF) must fit into it. MAX_HEADERS bounds the number of header lines per part and costs no memory.

size_of::<Parser<LINE, _>>() == LINE + 5 * size_of::<usize>() (asserted by a test): the boundary slice, the line length, the header counter and the state.

Implementations§

Source§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> Parser<'b, LINE, MAX_HEADERS>

Source

pub fn new(boundary: &'b [u8]) -> Result<Self, Error>

Creates a parser for boundary (rule 1), which is borrowed, not copied.

§Errors

Error::Boundary if boundary is not 1–70 RFC 2046 bchars ending in a non-space byte.

Source

pub fn from_content_type(content_type: &'b [u8]) -> Result<Self, Error>

Creates a parser from a Content-Type header value such as multipart/form-data; boundary=----x (rule 1). The boundary is borrowed from content_type, quoted or unquoted.

§Errors

Error::Boundary if the value is not a multipart/* media type with a valid boundary parameter.

Source

pub const fn boundary(&self) -> &'b [u8]

The boundary this parser was created with.

Source

pub const fn min_buffer(&self) -> usize

The smallest input.len() that guarantees progress on every call (boundary.len() + MAX_PADDING + 6, see DESIGN.md §9).

Source

pub const fn is_finished(&self) -> bool

true once Finished has been returned.

Source

pub fn parse<'i>( &mut self, input: &'i [u8], eof: bool, ) -> Result<(usize, Option<Event<'i, '_>>), Error>

Consumes a prefix of input and yields at most one event.

The parser may consume fewer bytes than given (a possible delimiter prefix at the end is held back): drop the first consumed bytes, append new ones and call again. eof = true means no more input will follow.

Progress guarantee: if input.len() >= self.min_buffer() or eof, every call consumes ≥ 1 byte, returns an event, or returns an error. After an error every call returns the same error; after Finished every call returns (input.len(), Some(Finished)).

§Errors

See Error; the parser is unusable afterwards.

Trait Implementations§

Source§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> Clone for Parser<'b, LINE, MAX_HEADERS>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> Debug for Parser<'b, LINE, MAX_HEADERS>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> Freeze for Parser<'b, LINE, MAX_HEADERS>
where [u8; LINE]: Freeze,

§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> RefUnwindSafe for Parser<'b, LINE, MAX_HEADERS>
where [u8; LINE]: RefUnwindSafe,

§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> Send for Parser<'b, LINE, MAX_HEADERS>
where [u8; LINE]: Send,

§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> Sync for Parser<'b, LINE, MAX_HEADERS>
where [u8; LINE]: Sync,

§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> Unpin for Parser<'b, LINE, MAX_HEADERS>
where [u8; LINE]: Unpin,

§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> UnsafeUnpin for Parser<'b, LINE, MAX_HEADERS>
where [u8; LINE]: UnsafeUnpin,

§

impl<'b, const LINE: usize, const MAX_HEADERS: usize> UnwindSafe for Parser<'b, LINE, MAX_HEADERS>
where [u8; LINE]: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.