From d83b0ad464c969c4305d27607cf18d54e005898d Mon Sep 17 00:00:00 2001 From: "Aode (Lion)" Date: Mon, 13 Sep 2021 20:45:09 -0500 Subject: [PATCH] Don't include generic in error type --- Cargo.toml | 2 +- examples/simple.rs | 22 ++++---------------- examples/upload.rs | 2 +- src/error.rs | 13 +++--------- src/lib.rs | 2 +- src/middleware.rs | 3 ++- src/types.rs | 52 +++++++++++++++++++++++----------------------- src/upload.rs | 41 ++++++++++++++++++++++-------------- 8 files changed, 63 insertions(+), 74 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 614d4c1..bc499f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "actix-form-data" description = "Multipart Form Data for Actix Web" -version = "0.6.0-beta.8" +version = "0.6.0-beta.9" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/actix-form-data.git" diff --git a/examples/simple.rs b/examples/simple.rs index 4db370b..536fe1a 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,24 +1,10 @@ -use actix_form_data::{Field, Form, Value}; +use actix_form_data::{Error, Field, Form, Value}; use actix_web::{ web::{post, resource}, - App, HttpResponse, HttpServer, ResponseError, + App, HttpResponse, HttpServer, }; use futures_util::stream::StreamExt; -#[derive(Debug, thiserror::Error)] -#[error("{inner}")] -struct MyError { - inner: Box, -} - -impl MyError { - fn new(e: impl std::error::Error + 'static) -> Self { - Self { inner: Box::new(e) } - } -} - -impl ResponseError for MyError {} - async fn upload(uploaded_content: Value<()>) -> HttpResponse { println!("Uploaded Content: {:#?}", uploaded_content); HttpResponse::Created().finish() @@ -39,9 +25,9 @@ async fn main() -> Result<(), anyhow::Error> { "files", Field::array(Field::file(|_, _, mut stream| async move { while let Some(res) = stream.next().await { - res.map_err(MyError::new)?; + res?; } - Ok(()) as Result<(), MyError> + Ok(()) as Result<(), Error> })), ); diff --git a/examples/upload.rs b/examples/upload.rs index aad8815..21ad216 100644 --- a/examples/upload.rs +++ b/examples/upload.rs @@ -67,7 +67,7 @@ async fn upload(uploaded_content: Value) -> HttpResponse { } async fn save_file( - stream: Pin>>>>, + stream: Pin>>>, count: usize, ) -> Result { use futures_lite::io::AsyncWriteExt; diff --git a/src/error.rs b/src/error.rs index 76533be..d335089 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,9 +30,7 @@ use actix_web::{ }; #[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("{0}")] - FileFn(E), +pub enum Error { #[error("Error parsing payload, {0}")] Payload(#[from] PayloadError), #[error("Error in multipart creation, {0}")] @@ -63,19 +61,15 @@ pub enum Error { FileSize, } -impl From for Error { +impl From for Error { fn from(m: MultipartError) -> Self { Error::Multipart(m) } } -impl ResponseError for Error -where - E: ResponseError, -{ +impl ResponseError for Error { fn status_code(&self) -> StatusCode { match *self { - Error::FileFn(ref e) => e.status_code(), Error::Payload(ref e) => e.status_code(), _ => StatusCode::BAD_REQUEST, } @@ -83,7 +77,6 @@ where fn error_response(&self) -> HttpResponse { match *self { - Error::FileFn(ref e) => e.error_response(), Error::Payload(ref e) => e.error_response(), Error::Multipart(_) | Error::ParseField(_) diff --git a/src/lib.rs b/src/lib.rs index f9f4e26..192219a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,7 @@ //! while let Some(_) = stream.next().await { //! // do something //! } -//! Ok(()) as Result<(), std::io::Error> +//! Ok(()) as Result<(), Error> //! })), //! ); //! diff --git a/src/middleware.rs b/src/middleware.rs index 98e20c7..f9ac437 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -130,7 +130,8 @@ where Box::pin(async move { let uploaded = match handle_multipart(multipart, form.clone()).await { - Ok(uploaded) => uploaded, + Ok(Ok(uploaded)) => uploaded, + Ok(Err(e)) => return Err(e.into()), Err(e) => { if let Some(f) = form.transform_error.clone() { return Err((f)(e)); diff --git a/src/types.rs b/src/types.rs index 4a9e8f1..10e19cc 100644 --- a/src/types.rs +++ b/src/types.rs @@ -157,8 +157,8 @@ pub type FileFn = Arc< dyn Fn( String, Mime, - Pin>>>>, - ) -> Pin>>>> + Pin>>>, + ) -> Pin>>> + Send + Sync, >; @@ -211,7 +211,7 @@ impl Field { /// /// # Example /// ```rust - /// # use actix_form_data::{Form, Field}; + /// # use actix_form_data::{Error, Form, Field}; /// # use tokio::sync::mpsc::channel; /// # use futures_util::stream::StreamExt; /// # @@ -226,13 +226,13 @@ impl Field { /// } /// } /// } - /// Ok(()) as Result<_, std::io::Error> + /// Ok(()) as Result<_, Error> /// } /// })); /// ``` pub fn file(f: F) -> Self where - F: Fn(String, Mime, Pin>>>>) -> Fut + F: Fn(String, Mime, Pin>>>) -> Fut + Send + Sync + Clone @@ -242,7 +242,7 @@ impl Field { { Field::File(Arc::new(move |filename, mime, stream| { let f = f.clone(); - Box::pin(async move { (f)(filename, mime, stream).await.map_err(Error::FileFn) }) + Box::pin(async move { (f)(filename, mime, stream).await }) })) } @@ -250,8 +250,8 @@ impl Field { /// /// # Example /// ```rust - /// # use actix_form_data::{Form, Field}; - /// let form = Form::<(), std::io::Error>::new().field("text-field", Field::bytes()); + /// # use actix_form_data::{Error, Form, Field}; + /// let form = Form::<(), Error>::new().field("text-field", Field::bytes()); pub fn bytes() -> Self { Field::Bytes } @@ -260,8 +260,8 @@ impl Field { /// /// # Example /// ```rust - /// # use actix_form_data::{Form, Field}; - /// let form = Form::<(), std::io::Error>::new().field("text-field", Field::text()); + /// # use actix_form_data::{Error, Form, Field}; + /// let form = Form::<(), Error>::new().field("text-field", Field::text()); pub fn text() -> Self { Field::Text } @@ -270,8 +270,8 @@ impl Field { /// /// # Example /// ```rust - /// # use actix_form_data::{Form, Field}; - /// let form = Form::<(), std::io::Error>::new().field("int-field", Field::int()); + /// # use actix_form_data::{Error, Form, Field}; + /// let form = Form::<(), Error>::new().field("int-field", Field::int()); /// ``` pub fn int() -> Self { Field::Int @@ -281,8 +281,8 @@ impl Field { /// /// # Example /// ```rust - /// # use actix_form_data::{Form, Field}; - /// let form = Form::<(), std::io::Error>::new().field("float-field", Field::float()); + /// # use actix_form_data::{Error, Form, Field}; + /// let form = Form::<(), Error>::new().field("float-field", Field::float()); /// ``` pub fn float() -> Self { Field::Float @@ -292,9 +292,9 @@ impl Field { /// /// # Example /// ```rust - /// # use actix_form_data::{Form, Field}; + /// # use actix_form_data::{Error, Form, Field}; /// # fn main() { - /// let form = Form::<(), std::io::Error>::new() + /// let form = Form::<(), Error>::new() /// .field( /// "array-field", /// Field::array(Field::text()) @@ -309,9 +309,9 @@ impl Field { /// /// # Example /// ```rust - /// # use actix_form_data::{Form, Field}; + /// # use actix_form_data::{Error, Form, Field}; /// # fn main() { - /// let form = Form::<(), std::io::Error>::new() + /// let form = Form::<(), Error>::new() /// .field( /// "map-field", /// Field::map() @@ -434,9 +434,9 @@ impl Map { /// Add a `Field` to a map /// # Example /// ```rust - /// # use actix_form_data::Field; + /// # use actix_form_data::{Error, Field}; /// # - /// Field::<(), std::io::Error>::map() + /// Field::<(), Error>::map() /// .field("sub-field", Field::text()) /// .field("sub-field-two", Field::text()) /// .finalize(); @@ -449,9 +449,9 @@ impl Map { /// Finalize the map into a `Field`, so it can be added to a Form /// ```rust - /// # use actix_form_data::Field; + /// # use actix_form_data::{Error, Field}; /// # - /// Field::<(), std::io::Error>::map() + /// Field::<(), Error>::map() /// .field("sub-field", Field::text()) /// .field("sub-field-two", Field::text()) /// .finalize(); @@ -477,8 +477,8 @@ impl Map { /// /// # Example /// ```rust -/// # use actix_form_data::{Form, Field}; -/// let form = Form::<(), std::io::Error>::new() +/// # use actix_form_data::{Error, Form, Field}; +/// let form = Form::<(), Error>::new() /// .field("field-name", Field::text()) /// .field("second-field", Field::int()) /// .field("third-field", Field::float()) @@ -502,7 +502,7 @@ pub struct Form { pub(crate) max_field_size: usize, pub(crate) max_files: u32, pub(crate) max_file_size: usize, - pub(crate) transform_error: Option) -> actix_web::Error + Send + Sync>>, + pub(crate) transform_error: Option actix_web::Error + Send + Sync>>, inner: Map, } @@ -549,7 +549,7 @@ impl Form { /// Set the Transform Error method to convert Error types into actix_web::Error by hand pub fn transform_error( mut self, - f: impl Fn(Error) -> actix_web::Error + Send + Sync + 'static, + f: impl Fn(Error) -> actix_web::Error + Send + Sync + 'static, ) -> Self { self.transform_error = Some(Arc::new(f)); self diff --git a/src/upload.rs b/src/upload.rs index 408ad9e..10d45b5 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -65,7 +65,7 @@ fn consolidate(mf: MultipartForm) -> Value { ) } -fn parse_multipart_name(name: String) -> Result, Error> { +fn parse_multipart_name(name: String) -> Result, Error> { name.split('[') .map(|part| { if part.len() == 1 && part.ends_with(']') { @@ -104,7 +104,7 @@ async fn handle_file_upload( filename: Option, form: Form, file_fn: FileFn, -) -> Result, Error> +) -> Result, E>, Error> where T: 'static, E: 'static, @@ -142,20 +142,23 @@ where } })), ) - .await?; + .await; - Ok(MultipartContent::File(FileMeta { - filename, - content_type, - result, - })) + match result { + Ok(result) => Ok(Ok(MultipartContent::File(FileMeta { + filename, + content_type, + result, + }))), + Err(e) => Ok(Err(e)), + } } async fn handle_form_data( mut field: actix_multipart::Field, term: FieldTerminator, form: Form, -) -> Result, Error> +) -> Result, Error> where T: 'static, E: 'static, @@ -195,7 +198,7 @@ where async fn handle_stream_field( field: actix_multipart::Field, form: Form, -) -> Result, Error> +) -> Result, E>, Error> where T: 'static, E: 'static, @@ -211,19 +214,22 @@ where let content = match term { FieldTerminator::File(file_fn) => { - handle_file_upload(field, content_disposition.filename, form, file_fn).await? + match handle_file_upload(field, content_disposition.filename, form, file_fn).await? { + Ok(content) => content, + Err(e) => return Ok(Err(e)), + } } term => handle_form_data(field, term, form.clone()).await?, }; - Ok((name, content)) + Ok(Ok((name, content))) } /// Handle multipart streams from Actix Web pub async fn handle_multipart( m: actix_multipart::Multipart, form: Form, -) -> Result, Error> +) -> Result, E>, Error> where T: 'static, E: 'static, @@ -245,7 +251,10 @@ where } opt = unordered.next() => { if let Some(res) = opt { - let (name_parts, content) = res?; + let (name_parts, content) = match res? { + Ok(tup) => tup, + Err(e) => return Ok(Err(e)), + }; let (l, r) = count(&content, file_count, field_count, &form)?; file_count = l; @@ -258,7 +267,7 @@ where } } - Ok(consolidate(multipart_form)) + Ok(Ok(consolidate(multipart_form))) } fn count( @@ -266,7 +275,7 @@ fn count( mut file_count: u32, mut field_count: u32, form: &Form, -) -> Result<(u32, u32), Error> { +) -> Result<(u32, u32), Error> { match content { MultipartContent::File(_) => { file_count += 1;