Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ features = ["all"]

[features]
default = []
all = ["json", "reader"]
all = ["json"]
json = ["serde", "serde_json"]
reader = ["tokio", "tokio-util"]
tokio-io = ["tokio", "tokio-util"]

[dependencies]
log = "0.4"
bytes = "0.5"
futures = "0.3"
futures_codec = "0.4.1"
twoway = "0.2"
http = "0.2"
httparse = "1.3"
Expand All @@ -46,8 +47,3 @@ serde = { version = "1.0", features = ["derive"] }
tokio = { version = "0.2", features = ["full"] }
hyper = "0.13"
routerify = "1.1"

[[example]]
name = "parse_async_read"
path = "examples/parse_async_read.rs"
required-features = ["reader"]
4 changes: 4 additions & 0 deletions examples/parse_async_read.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#[cfg(not(feature = "tokio-io"))]
use futures::io::AsyncRead;
#[cfg(feature = "tokio-io")]
use tokio::io::AsyncRead;

// Import multer types.
use multer::Multipart;

Expand Down
40 changes: 21 additions & 19 deletions src/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ use crate::helpers;
use crate::state::{MultipartState, StreamingStage};
use crate::Field;
use bytes::Bytes;
#[cfg(not(feature = "tokio-io"))]
use futures::io::AsyncRead;
use futures::stream::{Stream, TryStreamExt};
#[cfg(not(feature = "tokio-io"))]
use futures_codec::{BytesCodec, FramedRead};
use std::marker::Unpin;
use std::ops::DerefMut;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
#[cfg(feature = "reader")]
#[cfg(feature = "tokio-io")]
use tokio::io::AsyncRead;
#[cfg(feature = "reader")]
#[cfg(feature = "tokio-io")]
use tokio_util::codec::{BytesCodec, FramedRead};

/// Represents the implementation of `multipart/form-data` formatted data.
Expand Down Expand Up @@ -115,11 +120,7 @@ impl Multipart {
}
}

/// Construct a new `Multipart` instance with the given [`AsyncRead`](https://docs.rs/tokio/0.2.20/tokio/io/trait.AsyncRead.html) reader and the boundary.
///
/// # Optional
///
/// This requires the optional `reader` feature to be enabled.
/// Construct a new `Multipart` instance with the given [`AsyncRead`][] reader and the boundary.
///
/// # Examples
///
Expand All @@ -139,21 +140,21 @@ impl Multipart {
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(run());
/// ```
#[cfg(feature = "reader")]
///
/// [`AsyncRead`]: https://docs.rs/futures-io/0.3.7/futures_io/trait.AsyncRead.html
pub fn with_reader<R, B>(reader: R, boundary: B) -> Multipart
where
R: AsyncRead + Send + 'static,
R: AsyncRead + Unpin + Send + 'static,
B: Into<String>,
{
#[cfg(feature = "tokio-io")]
let stream = FramedRead::new(reader, BytesCodec::new());
#[cfg(not(feature = "tokio-io"))]
let stream = FramedRead::new(reader, BytesCodec);
Multipart::new(stream, boundary)
}

/// Construct a new `Multipart` instance with the given [`AsyncRead`](https://docs.rs/tokio/0.2.20/tokio/io/trait.AsyncRead.html) reader and the boundary.
///
/// # Optional
///
/// This requires the optional `reader` feature to be enabled.
/// Construct a new `Multipart` instance with the given [`AsyncRead`][] reader and the boundary.
///
/// # Examples
///
Expand All @@ -173,13 +174,17 @@ impl Multipart {
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(run());
/// ```
#[cfg(feature = "reader")]
///
/// [`AsyncRead`]: https://docs.rs/futures-io/0.3.7/futures_io/trait.AsyncRead.html
pub fn with_reader_with_constraints<R, B>(reader: R, boundary: B, constraints: Constraints) -> Multipart
where
R: AsyncRead + Send + 'static,
R: AsyncRead + Unpin + Send + 'static,
B: Into<String>,
{
#[cfg(feature = "tokio-io")]
let stream = FramedRead::new(reader, BytesCodec::new());
#[cfg(not(feature = "tokio-io"))]
let stream = FramedRead::new(reader, BytesCodec);
Multipart::new_with_constraints(stream, boundary, constraints)
}

Expand All @@ -197,8 +202,6 @@ impl Multipart {
/// # Examples
///
/// ```
/// # #[cfg(feature = "reader")]
/// # {
/// use multer::Multipart;
///
/// # async fn run() {
Expand All @@ -211,7 +214,6 @@ impl Multipart {
/// }
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(run());
/// # }
/// ```
pub async fn next_field_with_idx(&mut self) -> crate::Result<Option<(usize, Field)>> {
self.try_next().await.map(|f| f.map(|field| (field.index(), field)))
Expand Down