use codegen for web::$method fns

This commit is contained in:
Rob Ede 2021-09-03 17:35:35 +01:00
parent b7a2db85cc
commit f356bbd31d
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 55 additions and 159 deletions

View File

@ -1,10 +1,10 @@
//! # References //! # References
//! //!
//! "The Content-Disposition Header Field" https://www.ietf.org/rfc/rfc2183.txt //! "The Content-Disposition Header Field" <https://www.ietf.org/rfc/rfc2183.txt>
//! "The Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)" https://www.ietf.org/rfc/rfc6266.txt //! "The Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)" <https://www.ietf.org/rfc/rfc6266.txt>
//! "Returning Values from Forms: multipart/form-data" https://www.ietf.org/rfc/rfc7578.txt //! "Returning Values from Forms: multipart/form-data" <https://www.ietf.org/rfc/rfc7578.txt>
//! Browser conformance tests at: http://greenbytes.de/tech/tc2231/ //! Browser conformance tests at: <http://greenbytes.de/tech/tc2231/>
//! IANA assignment: http://www.iana.org/assignments/cont-disp/cont-disp.xhtml //! IANA assignment: <http://www.iana.org/assignments/cont-disp/cont-disp.xhtml>
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;

View File

@ -476,7 +476,7 @@ impl WebService {
/// Set service name. /// Set service name.
/// ///
/// Name is used for url generation. /// Name is used for URL generation.
pub fn name(mut self, name: &str) -> Self { pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_string()); self.name = Some(name.to_string());
self self

View File

@ -7,14 +7,10 @@ pub use actix_http::Response as HttpResponse;
use actix_router::IntoPatterns; use actix_router::IntoPatterns;
pub use bytes::{Buf, BufMut, Bytes, BytesMut}; pub use bytes::{Buf, BufMut, Bytes, BytesMut};
use crate::error::BlockingError; use crate::{
use crate::extract::FromRequest; error::BlockingError, extract::FromRequest, handler::Handler, resource::Resource,
use crate::handler::Handler; responder::Responder, route::Route, scope::Scope, service::WebService,
use crate::resource::Resource; };
use crate::responder::Responder;
use crate::route::Route;
use crate::scope::Scope;
use crate::service::WebService;
pub use crate::config::ServiceConfig; pub use crate::config::ServiceConfig;
pub use crate::data::Data; pub use crate::data::Data;
@ -22,25 +18,21 @@ pub use crate::request::HttpRequest;
pub use crate::request_data::ReqData; pub use crate::request_data::ReqData;
pub use crate::types::*; pub use crate::types::*;
/// Create resource for a specific path. /// Creates a new resource for a specific path.
/// ///
/// Resources may have variable path segments. For example, a /// Resources may have dynamic path segments. For example, a resource with the path `/a/{name}/c`
/// resource with the path `/a/{name}/c` would match all incoming /// would match all incoming requests with paths such as `/a/b/c`, `/a/1/c`, or `/a/etc/c`.
/// requests with paths such as `/a/b/c`, `/a/1/c`, or `/a/etc/c`.
/// ///
/// A variable segment is specified in the form `{identifier}`, /// A dynamic segment is specified in the form `{identifier}`, where the identifier can be used
/// where the identifier can be used later in a request handler to /// later in a request handler to access the matched value for that segment. This is done by looking
/// access the matched value for that segment. This is done by /// up the identifier in the `Path` object returned by [`HttpRequest.match_info()`] method.
/// looking up the identifier in the `Params` object returned by
/// `HttpRequest.match_info()` method.
/// ///
/// By default, each segment matches the regular expression `[^{}/]+`. /// By default, each segment matches the regular expression `[^{}/]+`.
/// ///
/// You can also specify a custom regex in the form `{identifier:regex}`: /// You can also specify a custom regex in the form `{identifier:regex}`:
/// ///
/// For instance, to route `GET`-requests on any route matching /// For instance, to route `GET`-requests on any route matching `/users/{userid}/{friend}` and store
/// `/users/{userid}/{friend}` and store `userid` and `friend` in /// `userid` and `friend` in the exposed `Path` object:
/// the exposed `Params` object:
/// ///
/// ``` /// ```
/// use actix_web::{web, App, HttpResponse}; /// use actix_web::{web, App, HttpResponse};
@ -55,10 +47,16 @@ pub fn resource<T: IntoPatterns>(path: T) -> Resource {
Resource::new(path) Resource::new(path)
} }
/// Configure scope for common root path. /// Creates scope for common path prefix.
/// ///
/// Scopes collect multiple paths under a common path prefix. /// Scopes collect multiple paths under a common path prefix. The scope's path can contain dynamic
/// Scope path can contain variable path segments as resources. /// path segments.
///
/// # Examples
/// In this example, three routes are set up (and will handle any method):
/// * `/{project_id}/path1`
/// * `/{project_id}/path2`
/// * `/{project_id}/path3`
/// ///
/// ``` /// ```
/// use actix_web::{web, App, HttpResponse}; /// use actix_web::{web, App, HttpResponse};
@ -70,148 +68,50 @@ pub fn resource<T: IntoPatterns>(path: T) -> Resource {
/// .service(web::resource("/path3").to(|| HttpResponse::MethodNotAllowed())) /// .service(web::resource("/path3").to(|| HttpResponse::MethodNotAllowed()))
/// ); /// );
/// ``` /// ```
///
/// In the above example, three routes get added:
/// * /{project_id}/path1
/// * /{project_id}/path2
/// * /{project_id}/path3
///
pub fn scope(path: &str) -> Scope { pub fn scope(path: &str) -> Scope {
Scope::new(path) Scope::new(path)
} }
/// Create *route* without configuration. /// Creates a new un-configured route.
pub fn route() -> Route { pub fn route() -> Route {
Route::new() Route::new()
} }
/// Create *route* with `GET` method guard. macro_rules! method_route {
($method_fn:ident, $method_const:ident) => {
paste::paste! {
#[doc = "Creates a new route with `" $method_const "` method guard."]
/// ///
/// # Examples
#[doc = "In this example, one `" $method_const " /{project_id}` route is set up:"]
/// ``` /// ```
/// use actix_web::{web, App, HttpResponse}; /// use actix_web::{web, App, HttpResponse};
/// ///
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/{project_id}") /// web::resource("/{project_id}")
/// .route(web::get().to(|| HttpResponse::Ok())) #[doc = " .route(web::" $method_fn "().to(|| HttpResponse::Ok()))"]
///
/// ); /// );
/// ``` /// ```
/// pub fn $method_fn() -> Route {
/// In the above example, one `GET` route gets added: method(Method::$method_const)
/// * /{project_id} }
/// }
pub fn get() -> Route { };
method(Method::GET)
} }
/// Create *route* with `POST` method guard. method_route!(get, GET);
/// method_route!(post, POST);
/// ``` method_route!(put, PUT);
/// use actix_web::{web, App, HttpResponse}; method_route!(patch, PATCH);
/// method_route!(delete, DELETE);
/// let app = App::new().service( method_route!(head, HEAD);
/// web::resource("/{project_id}") method_route!(trace, TRACE);
/// .route(web::post().to(|| HttpResponse::Ok()))
/// );
/// ```
///
/// In the above example, one `POST` route gets added:
/// * /{project_id}
///
pub fn post() -> Route {
method(Method::POST)
}
/// Create *route* with `PUT` method guard. /// Creates a new route with specified method guard.
/// ///
/// ``` /// # Examples
/// use actix_web::{web, App, HttpResponse}; /// In this example, one `GET /{project_id}` route is set up:
///
/// let app = App::new().service(
/// web::resource("/{project_id}")
/// .route(web::put().to(|| HttpResponse::Ok()))
/// );
/// ```
///
/// In the above example, one `PUT` route gets added:
/// * /{project_id}
///
pub fn put() -> Route {
method(Method::PUT)
}
/// Create *route* with `PATCH` method guard.
///
/// ```
/// use actix_web::{web, App, HttpResponse};
///
/// let app = App::new().service(
/// web::resource("/{project_id}")
/// .route(web::patch().to(|| HttpResponse::Ok()))
/// );
/// ```
///
/// In the above example, one `PATCH` route gets added:
/// * /{project_id}
///
pub fn patch() -> Route {
method(Method::PATCH)
}
/// Create *route* with `DELETE` method guard.
///
/// ```
/// use actix_web::{web, App, HttpResponse};
///
/// let app = App::new().service(
/// web::resource("/{project_id}")
/// .route(web::delete().to(|| HttpResponse::Ok()))
/// );
/// ```
///
/// In the above example, one `DELETE` route gets added:
/// * /{project_id}
///
pub fn delete() -> Route {
method(Method::DELETE)
}
/// Create *route* with `HEAD` method guard.
///
/// ```
/// use actix_web::{web, App, HttpResponse};
///
/// let app = App::new().service(
/// web::resource("/{project_id}")
/// .route(web::head().to(|| HttpResponse::Ok()))
/// );
/// ```
///
/// In the above example, one `HEAD` route gets added:
/// * /{project_id}
///
pub fn head() -> Route {
method(Method::HEAD)
}
/// Create *route* with `TRACE` method guard.
///
/// ```
/// use actix_web::{web, App, HttpResponse};
///
/// let app = App::new().service(
/// web::resource("/{project_id}")
/// .route(web::trace().to(|| HttpResponse::Ok()))
/// );
/// ```
///
/// In the above example, one `HEAD` route gets added:
/// * /{project_id}
///
pub fn trace() -> Route {
method(Method::TRACE)
}
/// Create *route* and add method guard.
/// ///
/// ``` /// ```
/// use actix_web::{web, http, App, HttpResponse}; /// use actix_web::{web, http, App, HttpResponse};
@ -221,15 +121,11 @@ pub fn trace() -> Route {
/// .route(web::method(http::Method::GET).to(|| HttpResponse::Ok())) /// .route(web::method(http::Method::GET).to(|| HttpResponse::Ok()))
/// ); /// );
/// ``` /// ```
///
/// In the above example, one `GET` route gets added:
/// * /{project_id}
///
pub fn method(method: Method) -> Route { pub fn method(method: Method) -> Route {
Route::new().method(method) Route::new().method(method)
} }
/// Create a new route and add handler. /// Creates a new any-method route with handler.
/// ///
/// ``` /// ```
/// use actix_web::{web, App, HttpResponse, Responder}; /// use actix_web::{web, App, HttpResponse, Responder};
@ -253,7 +149,7 @@ where
Route::new().to(handler) Route::new().to(handler)
} }
/// Create raw service for a specific path. /// Creates a raw service for a specific path.
/// ///
/// ``` /// ```
/// use actix_web::{dev, web, guard, App, Error, HttpResponse}; /// use actix_web::{dev, web, guard, App, Error, HttpResponse};
@ -272,8 +168,8 @@ pub fn service<T: IntoPatterns>(path: T) -> WebService {
WebService::new(path) WebService::new(path)
} }
/// Execute blocking function on a thread pool, returns future that resolves /// Executes blocking function on a thread pool, returns future that resolves to result of the
/// to result of the function execution. /// function execution.
pub fn block<F, R>(f: F) -> impl Future<Output = Result<R, BlockingError>> pub fn block<F, R>(f: F) -> impl Future<Output = Result<R, BlockingError>>
where where
F: FnOnce() -> R + Send + 'static, F: FnOnce() -> R + Send + 'static,