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>
impl<'r> Multipart<'r>
Sourcepub fn new<S, O, E, B>(stream: S, boundary: B) -> Self
pub fn new<S, O, E, B>(stream: S, boundary: B) -> Self
Construct a new Multipart instance with the given Bytes stream and
the boundary.
Sourcepub fn with_constraints<S, O, E, B>(
stream: S,
boundary: B,
constraints: Constraints,
) -> Self
pub fn with_constraints<S, O, E, B>( stream: S, boundary: B, constraints: Constraints, ) -> Self
Construct a new Multipart instance with the given Bytes stream and
the boundary.
Sourcepub async fn next_field(&mut self) -> Result<Option<Field<'r>>>
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.
Sourcepub fn poll_next_field(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<Option<Field<'r>>>>
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.
Sourcepub async fn next_field_with_idx(
&mut self,
) -> Result<Option<(usize, Field<'r>)>>
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)
}