summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Scott <sam.scott89@gmail.com>2020-06-03 10:26:45 -0400
committerGitHub <noreply@github.com>2020-06-03 10:26:45 -0400
commit77cb6730f9265591dad141f4f9b840069c9cd2b9 (patch)
tree6c4cd170f74d0c789e1059a91514d04dc06decba /src
parentd5c2d3e44a5cbe2311111fb56bfe6bed8fabd961 (diff)
Support actix-web v2 (#30)
* update dependencies - actix-web v2 - percent encoding v2.1 - rust 2018 edition - remove rustfmt no longer supported rules * ci: add feature build matrix * fix actix unit tests Co-authored-by: Mario Reder <mreder1289@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/actix.rs25
-rw-r--r--src/de/mod.rs2
-rw-r--r--src/de/parse.rs40
-rw-r--r--src/lib.rs4
-rw-r--r--src/ser.rs2
5 files changed, 36 insertions, 37 deletions
diff --git a/src/actix.rs b/src/actix.rs
index fa7e105..70dc6e1 100644
--- a/src/actix.rs
+++ b/src/actix.rs
@@ -2,12 +2,14 @@
//!
//! Enable with the `actix` feature.
+use crate::de::Config as QsConfig;
+use crate::error::Error as QsError;
+
use actix_web::dev::Payload;
use actix_web::{
Error as ActixError, FromRequest, HttpRequest, HttpResponse, ResponseError,
};
-use de::Config as QsConfig;
-use error::Error as QsError;
+use futures::future::{ready, Ready};
use serde::de;
use std::fmt;
use std::fmt::{Debug, Display};
@@ -28,7 +30,7 @@ impl ResponseError for QsError {
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// extern crate actix_web;
-/// use actix_web::{web, App};
+/// use actix_web::{web, App, HttpResponse};
/// use serde_qs::actix::QsQuery;
///
/// #[derive(Deserialize)]
@@ -38,7 +40,7 @@ impl ResponseError for QsError {
///
/// // Use `QsQuery` extractor for query information.
/// // The correct request for this handler would be `/users?id[]=1124&id[]=88"`
-/// fn filter_users(info: QsQuery<UsersFilter>) -> String {
+/// fn filter_users(info: QsQuery<UsersFilter>) -> HttpResponse {
/// info.id.iter().map(|i| i.to_string()).collect::<Vec<String>>().join(", ").into()
/// }
///
@@ -88,7 +90,7 @@ where
T: de::DeserializeOwned,
{
type Error = ActixError;
- type Future = Result<Self, ActixError>;
+ type Future = Ready<Result<Self, ActixError>>;
type Config = QsQueryConfig;
#[inline]
@@ -103,7 +105,7 @@ where
.map(|c| &c.qs_config)
.unwrap_or(&default_qsconfig);
- qsconfig
+ let res = qsconfig
.deserialize_str::<T>(req.query_string())
.map(|val| Ok(QsQuery(val)))
.unwrap_or_else(move |e| {
@@ -114,7 +116,8 @@ where
};
Err(e)
- })
+ });
+ ready(res)
}
}
@@ -133,13 +136,13 @@ where
/// }
///
/// /// deserialize `Info` from request's querystring
-/// fn index(info: QsQuery<Info>) -> String {
-/// format!("Welcome {}!", info.username)
+/// fn index(info: QsQuery<Info>) -> HttpResponse {
+/// format!("Welcome {}!", info.username).into()
/// }
///
/// fn main() {
/// let app = App::new().service(
-/// web::resource("/index.html").data(
+/// web::resource("/index.html").app_data(
/// // change query extractor configuration
/// QsQuery::<Info>::configure(|cfg| {
/// cfg.error_handler(|err, req| { // <- create custom error response
@@ -155,7 +158,7 @@ where
pub struct QsQueryConfig {
ehandler:
- Option<Arc<Fn(QsError, &HttpRequest) -> ActixError + Send + Sync>>,
+ Option<Arc<dyn Fn(QsError, &HttpRequest) -> ActixError + Send + Sync>>,
qs_config: QsConfig,
}
diff --git a/src/de/mod.rs b/src/de/mod.rs
index a82a37b..923c4b9 100644
--- a/src/de/mod.rs
+++ b/src/de/mod.rs
@@ -38,7 +38,7 @@
mod parse;
-use error::*;
+use crate::error::*;
use serde::de;
use serde::de::IntoDeserializer;
diff --git a/src/de/parse.rs b/src/de/parse.rs
index 6f10ac3..c23aa7e 100644
--- a/src/de/parse.rs
+++ b/src/de/parse.rs
@@ -1,6 +1,5 @@
use super::*;
-use percent_encoding;
use serde::de;
use std::borrow::Cow;
@@ -206,7 +205,7 @@ fn replace_plus(input: &[u8]) -> Cow<[u8]> {
}
Cow::Owned(replaced)
- }
+ },
}
}
@@ -235,25 +234,26 @@ impl<'a> Parser<'a> {
/// present.
fn collect_str(&mut self) -> Result<Cow<'a, str>> {
let replaced = replace_plus(&self.inner[self.acc.0..self.acc.1 - 1]);
- let ret:Result<Cow<'a, str>> = match percent_encoding::percent_decode(&replaced).decode_utf8()? {
- Cow::Borrowed(_) => {
- match replaced {
- Cow::Borrowed(_) => {
- // In this case, neither method made replacements, so we
- // reuse the original bytes
- let res = str::from_utf8(&self.inner[self.acc.0..self.acc.1 - 1])?;
- Ok(Cow::Borrowed(res))
- },
- Cow::Owned(owned) => {
- let res = String::from_utf8(owned)?;
- Ok(Cow::Owned(res))
+ let ret: Result<Cow<'a, str>> =
+ match percent_encoding::percent_decode(&replaced).decode_utf8()? {
+ Cow::Borrowed(_) => {
+ match replaced {
+ Cow::Borrowed(_) => {
+ // In this case, neither method made replacements, so we
+ // reuse the original bytes
+ let res = str::from_utf8(
+ &self.inner[self.acc.0..self.acc.1 - 1],
+ )?;
+ Ok(Cow::Borrowed(res))
+ },
+ Cow::Owned(owned) => {
+ let res = String::from_utf8(owned)?;
+ Ok(Cow::Owned(res))
+ },
}
- }
- },
- Cow::Owned(owned) => {
- Ok(Cow::Owned(owned))
- }
- };
+ },
+ Cow::Owned(owned) => Ok(Cow::Owned(owned)),
+ };
self.clear_acc();
ret.map_err(Error::from)
}
diff --git a/src/lib.rs b/src/lib.rs
index bfbb9c3..3dae164 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -202,12 +202,8 @@
while_true
)]
-#[cfg(feature = "actix")]
-extern crate actix_web;
-extern crate data_encoding;
#[macro_use]
extern crate error_chain;
-extern crate percent_encoding;
#[macro_use]
extern crate serde;
diff --git a/src/ser.rs b/src/ser.rs
index 9086ad2..618b148 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -4,7 +4,7 @@ use data_encoding::BASE64URL_NOPAD as BASE64;
use percent_encoding::{percent_encode, AsciiSet, NON_ALPHANUMERIC};
use serde::ser;
-use error::*;
+use crate::error::*;
use std::borrow::Cow;
use std::fmt::Display;