1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

export extractor configs via web module

This commit is contained in:
Nikolay Kim 2019-03-07 14:01:52 -08:00
parent b211966c28
commit 0e57b4ad61
6 changed files with 44 additions and 43 deletions

View File

@ -189,9 +189,9 @@ where
/// multiple resources with one route would be registered for same resource path. /// multiple resources with one route would be registered for same resource path.
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, App, HttpResponse, extract::Path}; /// use actix_web::{web, App, HttpResponse};
/// ///
/// fn index(data: Path<(String, String)>) -> &'static str { /// fn index(data: web::Path<(String, String)>) -> &'static str {
/// "Welcome!" /// "Welcome!"
/// } /// }
/// ///
@ -276,9 +276,9 @@ where
/// multiple resources with one route would be registered for same resource path. /// multiple resources with one route would be registered for same resource path.
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, App, HttpResponse, extract::Path}; /// use actix_web::{web, App, HttpResponse};
/// ///
/// fn index(data: Path<(String, String)>) -> &'static str { /// fn index(data: web::Path<(String, String)>) -> &'static str {
/// "Welcome!" /// "Welcome!"
/// } /// }
/// ///

View File

@ -78,12 +78,12 @@ impl ExtractorConfig for () {
/// ## Example /// ## Example
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, http, App, extract::Path}; /// use actix_web::{web, App};
/// ///
/// /// extract path info from "/{username}/{count}/index.html" url /// /// extract path info from "/{username}/{count}/index.html" url
/// /// {username} - deserializes to a String /// /// {username} - deserializes to a String
/// /// {count} - - deserializes to a u32 /// /// {count} - - deserializes to a u32
/// fn index(info: Path<(String, u32)>) -> String { /// fn index(info: web::Path<(String, u32)>) -> String {
/// format!("Welcome {}! {}", info.0, info.1) /// format!("Welcome {}! {}", info.0, info.1)
/// } /// }
/// ///
@ -100,7 +100,7 @@ impl ExtractorConfig for () {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, App, extract::Path, Error}; /// use actix_web::{web, App, Error};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -108,7 +108,7 @@ impl ExtractorConfig for () {
/// } /// }
/// ///
/// /// extract `Info` from a path using serde /// /// extract `Info` from a path using serde
/// fn index(info: Path<Info>) -> Result<String, Error> { /// fn index(info: web::Path<Info>) -> Result<String, Error> {
/// Ok(format!("Welcome {}!", info.username)) /// Ok(format!("Welcome {}!", info.username))
/// } /// }
/// ///
@ -170,12 +170,12 @@ impl<T> From<T> for Path<T> {
/// ## Example /// ## Example
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, http, App, extract::Path}; /// use actix_web::{web, App};
/// ///
/// /// extract path info from "/{username}/{count}/index.html" url /// /// extract path info from "/{username}/{count}/index.html" url
/// /// {username} - deserializes to a String /// /// {username} - deserializes to a String
/// /// {count} - - deserializes to a u32 /// /// {count} - - deserializes to a u32
/// fn index(info: Path<(String, u32)>) -> String { /// fn index(info: web::Path<(String, u32)>) -> String {
/// format!("Welcome {}! {}", info.0, info.1) /// format!("Welcome {}! {}", info.0, info.1)
/// } /// }
/// ///
@ -192,7 +192,7 @@ impl<T> From<T> for Path<T> {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, App, extract::Path, Error}; /// use actix_web::{web, App, Error};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -200,7 +200,7 @@ impl<T> From<T> for Path<T> {
/// } /// }
/// ///
/// /// extract `Info` from a path using serde /// /// extract `Info` from a path using serde
/// fn index(info: Path<Info>) -> Result<String, Error> { /// fn index(info: web::Path<Info>) -> Result<String, Error> {
/// Ok(format!("Welcome {}!", info.username)) /// Ok(format!("Welcome {}!", info.username))
/// } /// }
/// ///
@ -244,7 +244,7 @@ impl<T: fmt::Display> fmt::Display for Path<T> {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, extract, App}; /// use actix_web::{web, App};
/// ///
/// #[derive(Debug, Deserialize)] /// #[derive(Debug, Deserialize)]
/// pub enum ResponseType { /// pub enum ResponseType {
@ -261,7 +261,7 @@ impl<T: fmt::Display> fmt::Display for Path<T> {
/// // Use `Query` extractor for query information. /// // Use `Query` extractor for query information.
/// // This handler get called only if request's query contains `username` field /// // This handler get called only if request's query contains `username` field
/// // The correct request for this handler would be `/index.html?id=64&response_type=Code"` /// // The correct request for this handler would be `/index.html?id=64&response_type=Code"`
/// fn index(info: extract::Query<AuthRequest>) -> String { /// fn index(info: web::Query<AuthRequest>) -> String {
/// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type) /// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type)
/// } /// }
/// ///
@ -299,7 +299,7 @@ impl<T> Query<T> {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, extract, App}; /// use actix_web::{web, App};
/// ///
/// #[derive(Debug, Deserialize)] /// #[derive(Debug, Deserialize)]
/// pub enum ResponseType { /// pub enum ResponseType {
@ -316,7 +316,7 @@ impl<T> Query<T> {
/// // Use `Query` extractor for query information. /// // Use `Query` extractor for query information.
/// // This handler get called only if request's query contains `username` field /// // This handler get called only if request's query contains `username` field
/// // The correct request for this handler would be `/index.html?id=64&response_type=Code"` /// // The correct request for this handler would be `/index.html?id=64&response_type=Code"`
/// fn index(info: extract::Query<AuthRequest>) -> String { /// fn index(info: web::Query<AuthRequest>) -> String {
/// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type) /// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type)
/// } /// }
/// ///
@ -368,7 +368,7 @@ impl<T: fmt::Display> fmt::Display for Query<T> {
/// ```rust /// ```rust
/// # extern crate actix_web; /// # extern crate actix_web;
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, App, extract::Form}; /// use actix_web::{web, App};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct FormData { /// struct FormData {
@ -378,7 +378,7 @@ impl<T: fmt::Display> fmt::Display for Query<T> {
/// /// Extract form data using serde. /// /// Extract form data using serde.
/// /// This handler get called only if content type is *x-www-form-urlencoded* /// /// This handler get called only if content type is *x-www-form-urlencoded*
/// /// and content of the request could be deserialized to a `FormData` struct /// /// and content of the request could be deserialized to a `FormData` struct
/// fn index(form: Form<FormData>) -> String { /// fn index(form: web::Form<FormData>) -> String {
/// format!("Welcome {}!", form.username) /// format!("Welcome {}!", form.username)
/// } /// }
/// # fn main() {} /// # fn main() {}
@ -447,7 +447,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, extract, App, Result}; /// use actix_web::{web, App, Result};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct FormData { /// struct FormData {
@ -456,7 +456,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// ///
/// /// Extract form data using serde. /// /// Extract form data using serde.
/// /// Custom configuration is used for this handler, max payload size is 4k /// /// Custom configuration is used for this handler, max payload size is 4k
/// fn index(form: extract::Form<FormData>) -> Result<String> { /// fn index(form: web::Form<FormData>) -> Result<String> {
/// Ok(format!("Welcome {}!", form.username)) /// Ok(format!("Welcome {}!", form.username))
/// } /// }
/// ///
@ -465,7 +465,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// web::resource("/index.html") /// web::resource("/index.html")
/// .route(web::get() /// .route(web::get()
/// // change `Form` extractor configuration /// // change `Form` extractor configuration
/// .config(extract::FormConfig::default().limit(4097)) /// .config(web::FormConfig::default().limit(4097))
/// .to(index)) /// .to(index))
/// ); /// );
/// } /// }
@ -520,7 +520,7 @@ impl Default for FormConfig {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, extract, App}; /// use actix_web::{web, App};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -528,7 +528,7 @@ impl Default for FormConfig {
/// } /// }
/// ///
/// /// deserialize `Info` from request's body /// /// deserialize `Info` from request's body
/// fn index(info: extract::Json<Info>) -> String { /// fn index(info: web::Json<Info>) -> String {
/// format!("Welcome {}!", info.username) /// format!("Welcome {}!", info.username)
/// } /// }
/// ///
@ -631,7 +631,7 @@ impl<T: Serialize> Responder for Json<T> {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, extract, App}; /// use actix_web::{web, App};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -639,7 +639,7 @@ impl<T: Serialize> Responder for Json<T> {
/// } /// }
/// ///
/// /// deserialize `Info` from request's body /// /// deserialize `Info` from request's body
/// fn index(info: extract::Json<Info>) -> String { /// fn index(info: web::Json<Info>) -> String {
/// format!("Welcome {}!", info.username) /// format!("Welcome {}!", info.username)
/// } /// }
/// ///
@ -679,7 +679,7 @@ where
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{error, extract, web, App, HttpResponse}; /// use actix_web::{error, web, App, HttpResponse};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -696,7 +696,7 @@ where
/// web::resource("/index.html").route( /// web::resource("/index.html").route(
/// web::post().config( /// web::post().config(
/// // change json extractor configuration /// // change json extractor configuration
/// extract::JsonConfig::default().limit(4096) /// web::JsonConfig::default().limit(4096)
/// .error_handler(|err, req| { // <- create custom error response /// .error_handler(|err, req| { // <- create custom error response
/// error::InternalError::from_response( /// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into() /// err, HttpResponse::Conflict().finish()).into()
@ -887,7 +887,7 @@ where
/// ## Example /// ## Example
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, extract, App}; /// use actix_web::{web, App};
/// ///
/// /// extract text data from request /// /// extract text data from request
/// fn index(text: String) -> String { /// fn index(text: String) -> String {
@ -898,7 +898,7 @@ where
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html").route( /// web::resource("/index.html").route(
/// web::get() /// web::get()
/// .config(extract::PayloadConfig::new(4096)) // <- limit size of the payload /// .config(web::PayloadConfig::new(4096)) // <- limit size of the payload
/// .to(index)) // <- register handler with extractor params /// .to(index)) // <- register handler with extractor params
/// ); /// );
/// } /// }

View File

@ -1,7 +1,7 @@
#![allow(clippy::type_complexity)] #![allow(clippy::type_complexity)]
mod app; mod app;
pub mod extract; mod extract;
mod handler; mod handler;
// mod info; // mod info;
pub mod blocking; pub mod blocking;
@ -84,7 +84,8 @@ pub mod web {
use crate::route::Route; use crate::route::Route;
use crate::scope::Scope; use crate::scope::Scope;
pub use crate::extract::{Json, Path, Payload, Query}; pub use crate::extract::{Form, Json, Path, Payload, Query};
pub use crate::extract::{FormConfig, JsonConfig, PayloadConfig};
pub use crate::request::HttpRequest; pub use crate::request::HttpRequest;
pub use crate::state::State; pub use crate::state::State;

View File

@ -75,9 +75,9 @@ where
/// Add match guard to a resource. /// Add match guard to a resource.
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, guard, App, HttpResponse, extract::Path}; /// use actix_web::{web, guard, App, HttpResponse};
/// ///
/// fn index(data: Path<(String, String)>) -> &'static str { /// fn index(data: web::Path<(String, String)>) -> &'static str {
/// "Welcome!" /// "Welcome!"
/// } /// }
/// ///

View File

@ -220,7 +220,7 @@ impl<P: 'static> Route<P> {
/// ///
/// ```rust /// ```rust
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, http, App, extract::Path}; /// use actix_web::{web, http, App};
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
/// struct Info { /// struct Info {
@ -228,7 +228,7 @@ impl<P: 'static> Route<P> {
/// } /// }
/// ///
/// /// extract path info using serde /// /// extract path info using serde
/// fn index(info: Path<Info>) -> String { /// fn index(info: web::Path<Info>) -> String {
/// format!("Welcome {}!", info.username) /// format!("Welcome {}!", info.username)
/// } /// }
/// ///
@ -284,7 +284,7 @@ impl<P: 'static> Route<P> {
/// ```rust /// ```rust
/// # use futures::future::ok; /// # use futures::future::ok;
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, App, Error, extract::Path}; /// use actix_web::{web, App, Error};
/// use futures::Future; /// use futures::Future;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
@ -293,7 +293,7 @@ impl<P: 'static> Route<P> {
/// } /// }
/// ///
/// /// extract path info using serde /// /// extract path info using serde
/// fn index(info: Path<Info>) -> impl Future<Item = &'static str, Error = Error> { /// fn index(info: web::Path<Info>) -> impl Future<Item = &'static str, Error = Error> {
/// ok("Hello World!") /// ok("Hello World!")
/// } /// }
/// ///
@ -324,7 +324,7 @@ impl<P: 'static> Route<P> {
/// for specific route. /// for specific route.
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, extract, App}; /// use actix_web::{web, App};
/// ///
/// /// extract text data from request /// /// extract text data from request
/// fn index(body: String) -> String { /// fn index(body: String) -> String {
@ -336,7 +336,7 @@ impl<P: 'static> Route<P> {
/// web::resource("/index.html").route( /// web::resource("/index.html").route(
/// web::get() /// web::get()
/// // limit size of the payload /// // limit size of the payload
/// .config(extract::PayloadConfig::new(4096)) /// .config(web::PayloadConfig::new(4096))
/// // register handler /// // register handler
/// .to(index) /// .to(index)
/// )); /// ));

View File

@ -89,9 +89,9 @@ where
/// Add match guard to a scope. /// Add match guard to a scope.
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, guard, App, HttpRequest, HttpResponse, extract::Path}; /// use actix_web::{web, guard, App, HttpRequest, HttpResponse};
/// ///
/// fn index(data: Path<(String, String)>) -> &'static str { /// fn index(data: web::Path<(String, String)>) -> &'static str {
/// "Welcome!" /// "Welcome!"
/// } /// }
/// ///
@ -146,9 +146,9 @@ where
/// multiple resources with one route would be registered for same resource path. /// multiple resources with one route would be registered for same resource path.
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, App, HttpResponse, extract::Path}; /// use actix_web::{web, App, HttpResponse};
/// ///
/// fn index(data: Path<(String, String)>) -> &'static str { /// fn index(data: web::Path<(String, String)>) -> &'static str {
/// "Welcome!" /// "Welcome!"
/// } /// }
/// ///