improve extract docs

This commit is contained in:
Rob Ede 2021-09-11 16:24:20 +01:00
parent 48745756bd
commit 2b6bb6ac52
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 22 additions and 12 deletions

View File

@ -13,28 +13,37 @@ use futures_core::ready;
use crate::{dev::Payload, Error, HttpRequest}; use crate::{dev::Payload, Error, HttpRequest};
/// A type that implements [`FromRequest`] is called an **extractor** and can extract data /// A type that implements [`FromRequest`] is called an **extractor** and can extract data from
/// from the request. Examples of types that implement this trait are [`Json`], [`Form`], [`Path`]. /// the request. Some types that implement this trait are: [`Json`], [`Header`], and [`Path`].
/// ///
/// # Configuration
/// An extractor can be customized by injecting the corresponding configuration with one of: /// An extractor can be customized by injecting the corresponding configuration with one of:
/// ///
/// - [`App::app_data()`](`crate::App::app_data`) /// - [`App::app_data()`][crate::App::app_data]
/// - [`Scope::app_data()`](`crate::Scope::app_data`) /// - [`Scope::app_data()`][crate::Scope::app_data]
/// - [`Resource::app_data()`](`crate::Resource::app_data`) /// - [`Resource::app_data()`][crate::Resource::app_data]
/// ///
/// Here are some built-in extractors and their corresponding configuration. /// Here are some built-in extractors and their corresponding configuration.
/// Please refer to the respective documentation for details. /// Please refer to the respective documentation for details.
/// ///
/// | Extractor | Configuration | /// | Extractor | Configuration |
/// |-------------|-------------------| /// |-------------|-------------------|
/// | [`Header`] | _None_ |
/// | [`Path`] | [`PathConfig`] |
/// | [`Json`] | [`JsonConfig`] | /// | [`Json`] | [`JsonConfig`] |
/// | [`Form`] | [`FormConfig`] | /// | [`Form`] | [`FormConfig`] |
/// | [`Path`] | [`PathConfig`] |
/// | [`Query`] | [`QueryConfig`] | /// | [`Query`] | [`QueryConfig`] |
/// | [`Payload`] | [`PayloadConfig`] |
/// | [`String`] | [`PayloadConfig`] |
/// | [`Bytes`] | [`PayloadConfig`] | /// | [`Bytes`] | [`PayloadConfig`] |
/// | [`String`] | [`PayloadConfig`] |
/// | [`Payload`] | [`PayloadConfig`] |
/// ///
/// # Implementing An Extractor
/// To reduce duplicate code in handlers where extracting certain parts of a request has a common
/// structure, you can implement `FromRequest` for your own types.
///
/// Note that the request payload can only be consumed by one extractor.
///
/// [`Header`]: crate::web::Header
/// [`Json`]: crate::web::Json /// [`Json`]: crate::web::Json
/// [`JsonConfig`]: crate::web::JsonConfig /// [`JsonConfig`]: crate::web::JsonConfig
/// [`Form`]: crate::web::Form /// [`Form`]: crate::web::Form
@ -47,7 +56,8 @@ use crate::{dev::Payload, Error, HttpRequest};
/// [`PayloadConfig`]: crate::web::PayloadConfig /// [`PayloadConfig`]: crate::web::PayloadConfig
/// [`String`]: FromRequest#impl-FromRequest-for-String /// [`String`]: FromRequest#impl-FromRequest-for-String
/// [`Bytes`]: crate::web::Bytes#impl-FromRequest /// [`Bytes`]: crate::web::Bytes#impl-FromRequest
#[cfg_attr(docsrs, doc(alias = "Extractor"))] /// [`Either`]: crate::web::Either
#[doc(alias = "extract", alias = "extractor")]
pub trait FromRequest: Sized { pub trait FromRequest: Sized {
/// The associated error which can be returned. /// The associated error which can be returned.
type Error: Into<Error>; type Error: Into<Error>;

View File

@ -32,7 +32,7 @@ use crate::{
/// To extract typed data from a request body, the inner type `T` must implement the /// To extract typed data from a request body, the inner type `T` must implement the
/// [`DeserializeOwned`] trait. /// [`DeserializeOwned`] trait.
/// ///
/// Use [`FormConfig`] to configure extraction process. /// Use [`FormConfig`] to configure extraction options.
/// ///
/// ``` /// ```
/// use actix_web::{post, web}; /// use actix_web::{post, web};

View File

@ -34,7 +34,7 @@ use crate::{
/// To extract typed data from a request body, the inner type `T` must implement the /// To extract typed data from a request body, the inner type `T` must implement the
/// [`serde::Deserialize`] trait. /// [`serde::Deserialize`] trait.
/// ///
/// Use [`JsonConfig`] to configure extraction process. /// Use [`JsonConfig`] to configure extraction options.
/// ///
/// ``` /// ```
/// use actix_web::{post, web, App}; /// use actix_web::{post, web, App};

View File

@ -14,7 +14,7 @@ use crate::{
/// Extract typed data from request path segments. /// Extract typed data from request path segments.
/// ///
/// Use [`PathConfig`] to configure extraction process. /// Use [`PathConfig`] to configure extraction option.
/// ///
/// # Examples /// # Examples
/// ``` /// ```