Struct multer::Multipart

source ·
pub struct Multipart<'r> { /* private fields */ }
Expand description

Represents the implementation of multipart/form-data formatted data.

This will parse the source stream into Field instances via next_field().

Field Exclusivity

A Field represents a raw, self-decoding stream into multipart data. As such, only one Field from a given Multipart instance may be live at once. That is, a Field emitted by next_field() must be dropped before calling next_field() again. Failure to do so will result in an error.

use std::convert::Infallible;

use bytes::Bytes;
use futures_util::stream::once;
use multer::Multipart;

let data = "--X-BOUNDARY\r\nContent-Disposition: form-data; \
    name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n";

let stream = once(async move { Result::<Bytes, Infallible>::Ok(Bytes::from(data)) });
let mut multipart = Multipart::new(stream, "X-BOUNDARY");

let field1 = multipart.next_field().await;
let field2 = multipart.next_field().await;

assert!(field2.is_err());

Examples

use std::convert::Infallible;

use bytes::Bytes;
use futures_util::stream::once;
use multer::Multipart;

let data = "--X-BOUNDARY\r\nContent-Disposition: form-data; \
    name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n";

let stream = once(async move { Result::<Bytes, Infallible>::Ok(Bytes::from(data)) });
let mut multipart = Multipart::new(stream, "X-BOUNDARY");

while let Some(field) = multipart.next_field().await.unwrap() {
    println!("Field: {:?}", field.text().await)
}

Implementations§

source§

impl<'r> Multipart<'r>

source

pub fn new<S, O, E, B>(stream: S, boundary: B) -> Selfwhere S: Stream<Item = Result<O, E>> + Send + 'r, O: Into<Bytes> + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'r, B: Into<String>,

Construct a new Multipart instance with the given Bytes stream and the boundary.

source

pub fn with_constraints<S, O, E, B>( stream: S, boundary: B, constraints: Constraints ) -> Selfwhere S: Stream<Item = Result<O, E>> + Send + 'r, O: Into<Bytes> + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'r, B: Into<String>,

Construct a new Multipart instance with the given Bytes stream and the boundary.

source

pub fn with_reader<R, B>(reader: R, boundary: B) -> Selfwhere R: AsyncRead + Unpin + Send + 'r, B: Into<String>,

Available on crate feature tokio-io only.

Construct a new Multipart instance with the given AsyncRead reader and the boundary.

Optional

This requires the optional tokio-io feature to be enabled.

Examples
use multer::Multipart;

let data =
    "--X-BOUNDARY\r\nContent-Disposition: form-data; name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n";
let reader = data.as_bytes();
let mut multipart = Multipart::with_reader(reader, "X-BOUNDARY");

while let Some(mut field) = multipart.next_field().await.unwrap() {
    while let Some(chunk) = field.chunk().await.unwrap() {
        println!("Chunk: {:?}", chunk);
    }
}
source

pub fn with_reader_with_constraints<R, B>( reader: R, boundary: B, constraints: Constraints ) -> Selfwhere R: AsyncRead + Unpin + Send + 'r, B: Into<String>,

Available on crate feature tokio-io only.

Construct a new Multipart instance with the given AsyncRead reader and the boundary.

Optional

This requires the optional tokio-io feature to be enabled.

Examples
use multer::Multipart;

let data =
    "--X-BOUNDARY\r\nContent-Disposition: form-data; name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n";
let reader = data.as_bytes();
let mut multipart = Multipart::with_reader(reader, "X-BOUNDARY");

while let Some(mut field) = multipart.next_field().await.unwrap() {
    while let Some(chunk) = field.chunk().await.unwrap() {
        println!("Chunk: {:?}", chunk);
    }
}
source

pub async fn next_field(&mut self) -> Result<Option<Field<'r>>>

Yields the next Field if available.

Any previous Field returned by this method must be dropped before calling this method or Multipart::next_field_with_idx() again. See field-exclusivity for details.

source

pub fn poll_next_field( &mut self, cx: &mut Context<'_> ) -> Poll<Result<Option<Field<'r>>>>

Yields the next Field if available.

Any previous Field returned by this method must be dropped before calling this method or Multipart::next_field_with_idx() again. See field-exclusivity for details.

This method is available since version 2.1.0.

source

pub async fn next_field_with_idx( &mut self ) -> Result<Option<(usize, Field<'r>)>>

Yields the next Field with their positioning index as a tuple (usize, Field).

Any previous Field returned by this method must be dropped before calling this method or Multipart::next_field() again. See field-exclusivity for details.

Examples
use std::convert::Infallible;

use bytes::Bytes;
use futures_util::stream::once;
use multer::Multipart;

let data = "--X-BOUNDARY\r\nContent-Disposition: form-data; \
    name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n";

let stream = once(async move { Result::<Bytes, Infallible>::Ok(Bytes::from(data)) });
let mut multipart = Multipart::new(stream, "X-BOUNDARY");

while let Some((idx, field)) = multipart.next_field_with_idx().await.unwrap() {
    println!("Index: {:?}, Content: {:?}", idx, field.text().await)
}

Trait Implementations§

source§

impl<'r> Debug for Multipart<'r>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'r> !RefUnwindSafe for Multipart<'r>

§

impl<'r> Send for Multipart<'r>

§

impl<'r> Sync for Multipart<'r>

§

impl<'r> Unpin for Multipart<'r>

§

impl<'r> !UnwindSafe for Multipart<'r>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more