mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-27 10:39:03 +02:00
make extractor config type explicit
This commit is contained in:
@ -73,6 +73,7 @@ impl<T> FromRequest for Form<T>
|
||||
where
|
||||
T: DeserializeOwned + 'static,
|
||||
{
|
||||
type Config = FormConfig;
|
||||
type Error = Error;
|
||||
type Future = Box<Future<Item = Self, Error = Error>>;
|
||||
|
||||
@ -115,7 +116,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
|
||||
///
|
||||
/// ```rust
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{web, App, Result};
|
||||
/// use actix_web::{web, App, FromRequest, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct FormData {
|
||||
@ -133,7 +134,9 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
|
||||
/// web::resource("/index.html")
|
||||
/// .route(web::get()
|
||||
/// // change `Form` extractor configuration
|
||||
/// .data(web::FormConfig::default().limit(4097))
|
||||
/// .data(
|
||||
/// web::Form::<FormData>::configure(|cfg| cfg.limit(4097))
|
||||
/// )
|
||||
/// .to(index))
|
||||
/// );
|
||||
/// }
|
||||
|
@ -168,6 +168,7 @@ impl<T> FromRequest for Json<T>
|
||||
where
|
||||
T: DeserializeOwned + 'static,
|
||||
{
|
||||
type Config = JsonConfig;
|
||||
type Error = Error;
|
||||
type Future = Box<Future<Item = Self, Error = Error>>;
|
||||
|
||||
@ -205,7 +206,7 @@ where
|
||||
///
|
||||
/// ```rust
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{error, web, App, HttpResponse};
|
||||
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -222,11 +223,13 @@ where
|
||||
/// web::resource("/index.html").route(
|
||||
/// web::post().data(
|
||||
/// // change json extractor configuration
|
||||
/// web::JsonConfig::default().limit(4096)
|
||||
/// .error_handler(|err, req| { // <- create custom error response
|
||||
/// error::InternalError::from_response(
|
||||
/// err, HttpResponse::Conflict().finish()).into()
|
||||
/// }))
|
||||
/// web::Json::<Info>::configure(|cfg| {
|
||||
/// cfg.limit(4096)
|
||||
/// .error_handler(|err, req| { // <- create custom error response
|
||||
/// error::InternalError::from_response(
|
||||
/// err, HttpResponse::Conflict().finish()).into()
|
||||
/// })
|
||||
/// }))
|
||||
/// .to(index))
|
||||
/// );
|
||||
/// }
|
||||
|
@ -156,6 +156,7 @@ impl<T> FromRequest for Path<T>
|
||||
where
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
type Config = ();
|
||||
type Error = Error;
|
||||
type Future = Result<Self, Error>;
|
||||
|
||||
|
@ -86,6 +86,7 @@ impl Stream for Payload {
|
||||
/// }
|
||||
/// ```
|
||||
impl FromRequest for Payload {
|
||||
type Config = PayloadConfig;
|
||||
type Error = Error;
|
||||
type Future = Result<Payload, Error>;
|
||||
|
||||
@ -121,6 +122,7 @@ impl FromRequest for Payload {
|
||||
/// }
|
||||
/// ```
|
||||
impl FromRequest for Bytes {
|
||||
type Config = PayloadConfig;
|
||||
type Error = Error;
|
||||
type Future =
|
||||
Either<Box<Future<Item = Bytes, Error = Error>>, FutureResult<Bytes, Error>>;
|
||||
@ -156,7 +158,7 @@ impl FromRequest for Bytes {
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use actix_web::{web, App};
|
||||
/// use actix_web::{web, App, FromRequest};
|
||||
///
|
||||
/// /// extract text data from request
|
||||
/// fn index(text: String) -> String {
|
||||
@ -167,12 +169,15 @@ impl FromRequest for Bytes {
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/index.html").route(
|
||||
/// web::get()
|
||||
/// .data(web::PayloadConfig::new(4096)) // <- limit size of the payload
|
||||
/// .data(String::configure(|cfg| { // <- limit size of the payload
|
||||
/// cfg.limit(4096)
|
||||
/// }))
|
||||
/// .to(index)) // <- register handler with extractor params
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
impl FromRequest for String {
|
||||
type Config = PayloadConfig;
|
||||
type Error = Error;
|
||||
type Future =
|
||||
Either<Box<Future<Item = String, Error = Error>>, FutureResult<String, Error>>;
|
||||
@ -228,7 +233,9 @@ pub struct PayloadConfig {
|
||||
impl PayloadConfig {
|
||||
/// Create `PayloadConfig` instance and set max size of payload.
|
||||
pub fn new(limit: usize) -> Self {
|
||||
Self::default().limit(limit)
|
||||
let mut cfg = Self::default();
|
||||
cfg.limit = limit;
|
||||
cfg
|
||||
}
|
||||
|
||||
/// Change max size of payload. By default max size is 256Kb
|
||||
|
@ -115,6 +115,7 @@ impl<T> FromRequest for Query<T>
|
||||
where
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
type Config = ();
|
||||
type Error = Error;
|
||||
type Future = Result<Self, Error>;
|
||||
|
||||
|
Reference in New Issue
Block a user