Skip to content

Commit

Permalink
allow to use fn with multiple arguments with .with()/.with_async()
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Aug 17, 2018
1 parent 248bd38 commit eb1e9a7
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 55 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changes

## [0.7.4] - 2018-08-xx
## [0.8.0] - 2018-08-xx

### Added

Expand All @@ -12,6 +12,8 @@

### Changed

* It is allowed to use function with up to 10 parameters for handler with `extractor parameters`.

* native-tls - 0.2

* `Content-Disposition` is re-worked. Its parser is now more robust and handles quoted content better. See #461
Expand Down
6 changes: 6 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.8

* `Route::with_config()`/`Route::with_async_config()` always passes configuration objects as tuple
even for handler with one parameter.


## 0.7

* `HttpRequest` does not implement `Stream` anymore. If you need to read request payload
Expand Down
3 changes: 2 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use resource::Resource;
use router::{ResourceDef, Router};
use scope::Scope;
use server::{HttpHandler, HttpHandlerTask, IntoHttpHandler, Request};
use with::WithFactory;

/// Application
pub struct HttpApplication<S = ()> {
Expand Down Expand Up @@ -249,7 +250,7 @@ where
/// ```
pub fn route<T, F, R>(mut self, path: &str, method: Method, f: F) -> App<S>
where
F: Fn(T) -> R + 'static,
F: WithFactory<T, S, R>,
R: Responder + 'static,
T: FromRequest<S> + 'static,
{
Expand Down
4 changes: 2 additions & 2 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// |r| {
/// r.method(http::Method::GET)
/// // register form handler and change form extractor configuration
/// .with_config(index, |cfg| {cfg.limit(4096);})
/// .with_config(index, |cfg| {cfg.0.limit(4096);})
/// },
/// );
/// }
Expand Down Expand Up @@ -427,7 +427,7 @@ impl<S: 'static> FromRequest<S> for Bytes {
/// let app = App::new().resource("/index.html", |r| {
/// r.method(http::Method::GET)
/// .with_config(index, |cfg| { // <- register handler with extractor params
/// cfg.limit(4096); // <- limit size of the payload
/// cfg.0.limit(4096); // <- limit size of the payload
/// })
/// });
/// }
Expand Down
2 changes: 1 addition & 1 deletion src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ where
/// let app = App::new().resource("/index.html", |r| {
/// r.method(http::Method::POST)
/// .with_config(index, |cfg| {
/// cfg.limit(4096) // <- change json extractor configuration
/// cfg.0.limit(4096) // <- change json extractor configuration
/// .error_handler(|err, req| { // <- create custom error response
/// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
Expand Down
3 changes: 2 additions & 1 deletion src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use middleware::Middleware;
use pred;
use route::Route;
use router::ResourceDef;
use with::WithFactory;

#[derive(Copy, Clone)]
pub(crate) struct RouteId(usize);
Expand Down Expand Up @@ -217,7 +218,7 @@ impl<S: 'static> Resource<S> {
/// ```
pub fn with<T, F, R>(&mut self, handler: F)
where
F: Fn(T) -> R + 'static,
F: WithFactory<T, S, R>,
R: Responder + 'static,
T: FromRequest<S> + 'static,
{
Expand Down
27 changes: 14 additions & 13 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use middleware::{
Started as MiddlewareStarted,
};
use pred::Predicate;
use with::{With, WithAsync};
use with::{WithAsyncFactory, WithFactory};

/// Resource route definition
///
Expand Down Expand Up @@ -166,15 +166,15 @@ impl<S: 'static> Route<S> {
/// ```
pub fn with<T, F, R>(&mut self, handler: F)
where
F: Fn(T) -> R + 'static,
F: WithFactory<T, S, R> + 'static,
R: Responder + 'static,
T: FromRequest<S> + 'static,
{
self.h(With::new(handler, <T::Config as Default>::default()));
self.h(handler.create());
}

/// Set handler function. Same as `.with()` but it allows to configure
/// extractor.
/// extractor. Configuration closure accepts config objects as tuple.
///
/// ```rust
/// # extern crate bytes;
Expand All @@ -192,21 +192,21 @@ impl<S: 'static> Route<S> {
/// let app = App::new().resource("/index.html", |r| {
/// r.method(http::Method::GET)
/// .with_config(index, |cfg| { // <- register handler
/// cfg.limit(4096); // <- limit size of the payload
/// cfg.0.limit(4096); // <- limit size of the payload
/// })
/// });
/// }
/// ```
pub fn with_config<T, F, R, C>(&mut self, handler: F, cfg_f: C)
where
F: Fn(T) -> R + 'static,
F: WithFactory<T, S, R>,
R: Responder + 'static,
T: FromRequest<S> + 'static,
C: FnOnce(&mut T::Config),
{
let mut cfg = <T::Config as Default>::default();
cfg_f(&mut cfg);
self.h(With::new(handler, cfg));
self.h(handler.create_with_config(cfg));
}

/// Set async handler function, use request extractor for parameters.
Expand Down Expand Up @@ -240,17 +240,18 @@ impl<S: 'static> Route<S> {
/// ```
pub fn with_async<T, F, R, I, E>(&mut self, handler: F)
where
F: Fn(T) -> R + 'static,
F: WithAsyncFactory<T, S, R, I, E>,
R: Future<Item = I, Error = E> + 'static,
I: Responder + 'static,
E: Into<Error> + 'static,
T: FromRequest<S> + 'static,
{
self.h(WithAsync::new(handler, <T::Config as Default>::default()));
self.h(handler.create());
}

/// Set async handler function, use request extractor for parameters.
/// This method allows to configure extractor.
/// This method allows to configure extractor. Configuration closure
/// accepts config objects as tuple.
///
/// ```rust
/// # extern crate bytes;
Expand All @@ -275,14 +276,14 @@ impl<S: 'static> Route<S> {
/// "/{username}/index.html", // <- define path parameters
/// |r| r.method(http::Method::GET)
/// .with_async_config(index, |cfg| {
/// cfg.limit(4096);
/// cfg.0.limit(4096);
/// }),
/// ); // <- use `with` extractor
/// }
/// ```
pub fn with_async_config<T, F, R, I, E, C>(&mut self, handler: F, cfg: C)
where
F: Fn(T) -> R + 'static,
F: WithAsyncFactory<T, S, R, I, E>,
R: Future<Item = I, Error = E> + 'static,
I: Responder + 'static,
E: Into<Error> + 'static,
Expand All @@ -291,7 +292,7 @@ impl<S: 'static> Route<S> {
{
let mut extractor_cfg = <T::Config as Default>::default();
cfg(&mut extractor_cfg);
self.h(WithAsync::new(handler, extractor_cfg));
self.h(handler.create_with_config(extractor_cfg));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use pred::Predicate;
use resource::{DefaultResource, Resource};
use scope::Scope;
use server::Request;
use with::WithFactory;

#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum ResourceId {
Expand Down Expand Up @@ -398,7 +399,7 @@ impl<S: 'static> Router<S> {

pub(crate) fn register_route<T, F, R>(&mut self, path: &str, method: Method, f: F)
where
F: Fn(T) -> R + 'static,
F: WithFactory<T, S, R>,
R: Responder + 'static,
T: FromRequest<S> + 'static,
{
Expand Down
3 changes: 2 additions & 1 deletion src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use pred::Predicate;
use resource::{DefaultResource, Resource};
use router::{ResourceDef, Router};
use server::Request;
use with::WithFactory;

/// Resources scope
///
Expand Down Expand Up @@ -222,7 +223,7 @@ impl<S: 'static> Scope<S> {
/// ```
pub fn route<T, F, R>(mut self, path: &str, method: Method, f: F) -> Scope<S>
where
F: Fn(T) -> R + 'static,
F: WithFactory<T, S, R>,
R: Responder + 'static,
T: FromRequest<S> + 'static,
{
Expand Down
Loading

0 comments on commit eb1e9a7

Please sign in to comment.