diff --git a/Cargo.toml b/Cargo.toml index de6e260..d9d4e3c 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.6" +version = "0.6.0-beta.7" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/actix-form-data.git" diff --git a/src/middleware.rs b/src/middleware.rs index 1d25227..26e9e85 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -26,9 +26,9 @@ use actix_web::{ dev::{Payload, Service, ServiceRequest, Transform}, FromRequest, HttpMessage, HttpRequest, }; +use futures_util::future::LocalBoxFuture; use std::{ - future::{ready, Future, Ready}, - pin::Pin, + future::{ready, Ready}, task::{Context, Poll}, }; use tokio::sync::oneshot::{channel, Receiver}; @@ -45,9 +45,9 @@ pub struct MultipartMiddleware { impl FromRequest for Value where T: 'static, - { +{ type Error = Error; - type Future = Pin>>>; + type Future = LocalBoxFuture<'static, Result>; type Config = (); fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { @@ -88,7 +88,7 @@ where { type Response = S::Response; type Error = S::Error; - type Future = Pin>>>; + type Future = LocalBoxFuture<'static, Result>; fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { self.service.poll_ready(cx) diff --git a/src/types.rs b/src/types.rs index 37561f9..c56c3bd 100644 --- a/src/types.rs +++ b/src/types.rs @@ -49,17 +49,10 @@ pub struct FileMeta { /// # let mut hm = HashMap::new(); /// # hm.insert("field-name".to_owned(), Value::Int(32)); /// # let value = Value::<()>::Map(hm); -/// match value { -/// Value::Map(mut hashmap) => { -/// match hashmap.remove("field-name") { -/// Some(value) => match value { -/// Value::Int(integer) => println!("{}", integer), -/// _ => (), -/// } -/// None => (), -/// } +/// if let Value::Map(mut hashmap) = value { +/// if let Some(Value::Int(integer)) = hashmap.remove("field-name") { +/// println!("{}", integer); /// } -/// _ => (), /// } /// ``` #[derive(Debug)] @@ -186,7 +179,7 @@ impl Clone for Field { match self { Self::Array(a) => Self::Array(a.clone()), Self::Map(m) => Self::Map(m.clone()), - Self::File(file_fn) => Self::File(Arc::clone(&file_fn)), + Self::File(file_fn) => Self::File(Arc::clone(file_fn)), Self::Bytes => Self::Bytes, Self::Int => Self::Int, Self::Float => Self::Float, @@ -386,15 +379,15 @@ pub struct Array { impl Clone for Array { fn clone(&self) -> Self { - Array { inner: Box::new((*self.inner).clone()) } + Array { + inner: Box::new((*self.inner).clone()), + } } } impl fmt::Debug for Array { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Array") - .field("inner", &self.inner) - .finish() + f.debug_struct("Array").field("inner", &self.inner).finish() } } @@ -408,11 +401,8 @@ impl Array { fn valid_field(&self, mut name: VecDeque<&NamePart>) -> Option> { trace!("Checking {:?} and {:?}", self, name); match name.pop_front() { - Some(name_part) => match name_part { - NamePart::Array => self.inner.valid_field(name), - _ => None, - }, - None => None, + Some(NamePart::Array) => self.inner.valid_field(name), + _ => None, } } } @@ -424,15 +414,15 @@ pub struct Map { impl Clone for Map { fn clone(&self) -> Self { - Map { inner: self.inner.clone() } + Map { + inner: self.inner.clone(), + } } } impl fmt::Debug for Map { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Map") - .field("inner", &self.inner) - .finish() + f.debug_struct("Map").field("inner", &self.inner).finish() } } @@ -473,15 +463,12 @@ impl Map { fn valid_field(&self, mut name: VecDeque<&NamePart>) -> Option> { trace!("Checking {:?} and {:?}", self, name); match name.pop_front() { - Some(name_part) => match name_part { - NamePart::Map(part_name) => self - .inner - .iter() - .find(|&&(ref item, _)| *item == *part_name) - .and_then(|&(_, ref field)| field.valid_field(name)), - _ => None, - }, - None => None, + Some(NamePart::Map(name_part)) => self + .inner + .iter() + .find(|&&(ref item, _)| *item == *name_part) + .and_then(|&(_, ref field)| field.valid_field(name)), + _ => None, } } } @@ -533,6 +520,12 @@ impl Clone for Form { } } +impl Default for Form { + fn default() -> Self { + Self::new() + } +} + impl Form { /// Create a new form /// @@ -640,10 +633,7 @@ pub(crate) enum NamePart { impl NamePart { pub fn is_map(&self) -> bool { - match *self { - NamePart::Map(_) => true, - _ => false, - } + matches!(self, NamePart::Map(_)) } } diff --git a/src/upload.rs b/src/upload.rs index 6119bbd..9dc2650 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -78,7 +78,7 @@ fn parse_multipart_name(name: String) -> Result, Error> { }) .fold(Ok(vec![]), |acc, part| match acc { Ok(mut v) => { - if v.len() == 0 && !part.is_map() { + if v.is_empty() && !part.is_map() { return Err(Error::ContentDisposition); } @@ -177,9 +177,7 @@ where let s = String::from_utf8(bytes.to_vec()).map_err(Error::ParseField)?; match term { - FieldTerminator::Bytes | FieldTerminator::File(_) => { - return Err(Error::FieldType); - } + FieldTerminator::Bytes | FieldTerminator::File(_) => Err(Error::FieldType), FieldTerminator::Text => Ok(MultipartContent::Text(s)), FieldTerminator::Float => s .parse() @@ -219,7 +217,10 @@ where } /// Handle multipart streams from Actix Web -pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Result, Error> +pub async fn handle_multipart( + m: actix_multipart::Multipart, + form: Form, +) -> Result, Error> where T: 'static, {