mirror of https://github.com/fafhrd91/actix-web
rewrite with macro_rules
This commit is contained in:
parent
4d016ae16c
commit
2aba2bbf22
|
@ -17,10 +17,12 @@
|
|||
//! - [options](attr.options.html)
|
||||
//! - [trace](attr.trace.html)
|
||||
//! - [patch](attr.patch.html)
|
||||
//! - [route](attr.patch.html)
|
||||
//!
|
||||
//! ### Attributes:
|
||||
//!
|
||||
//! - `"path"` - Raw literal string with path for which to register handle. Mandatory.
|
||||
//! - `"path"` - *Required*, raw literal string with path for which to register handle
|
||||
//! - `method="HTTP_METHOD"` - Secondary HTTP method accepted, uppercased string. "GET", "POST" for example
|
||||
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
||||
//! - `wrap="Middleware"` - Registers a resource middleware.
|
||||
//!
|
||||
|
@ -47,100 +49,6 @@ mod route;
|
|||
|
||||
use proc_macro::TokenStream;
|
||||
|
||||
/// Creates route handler with `GET` method guard.
|
||||
///
|
||||
/// Syntax: `#[get("path" [, attributes])]`
|
||||
///
|
||||
/// ## Attributes:
|
||||
///
|
||||
/// - `"path"` - Raw literal string with path for which to register handler. Mandatory.
|
||||
/// - `guard = "function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
||||
/// - `wrap = "Middleware"` - Registers a resource middleware.
|
||||
#[proc_macro_attribute]
|
||||
pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Get), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `POST` method guard.
|
||||
///
|
||||
/// Syntax: `#[post("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html)
|
||||
#[proc_macro_attribute]
|
||||
pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Post), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `PUT` method guard.
|
||||
///
|
||||
/// Syntax: `#[put("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html)
|
||||
#[proc_macro_attribute]
|
||||
pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Put), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `DELETE` method guard.
|
||||
///
|
||||
/// Syntax: `#[delete("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html).
|
||||
#[proc_macro_attribute]
|
||||
pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Delete), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `HEAD` method guard.
|
||||
///
|
||||
/// Syntax: `#[head("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html).
|
||||
#[proc_macro_attribute]
|
||||
pub fn head(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Head), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `CONNECT` method guard.
|
||||
///
|
||||
/// Syntax: `#[connect("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html).
|
||||
#[proc_macro_attribute]
|
||||
pub fn connect(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Connect), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `OPTIONS` method guard.
|
||||
///
|
||||
/// Syntax: `#[options("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html).
|
||||
#[proc_macro_attribute]
|
||||
pub fn options(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Options), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `TRACE` method guard.
|
||||
///
|
||||
/// Syntax: `#[trace("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html).
|
||||
#[proc_macro_attribute]
|
||||
pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Trace), args, input)
|
||||
}
|
||||
|
||||
/// Creates route handler with `PATCH` method guard.
|
||||
///
|
||||
/// Syntax: `#[patch("path" [, attributes])]`
|
||||
///
|
||||
/// Attributes are the same as in [get](attr.get.html).
|
||||
#[proc_macro_attribute]
|
||||
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::Patch), args, input)
|
||||
}
|
||||
|
||||
/// Creates resource handler, allowing multiple HTTP method guards.
|
||||
///
|
||||
/// Syntax: `#[route("path"[, attributes])]`
|
||||
|
@ -158,6 +66,51 @@ pub fn route(args: TokenStream, input: TokenStream) -> TokenStream {
|
|||
route::with_method(None, args, input)
|
||||
}
|
||||
|
||||
macro_rules! doc_comment {
|
||||
($($x:expr)*; $($tt:tt)*) => {
|
||||
$(#[doc = $x])*
|
||||
$($tt)*
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! method_macro {
|
||||
(
|
||||
$(
|
||||
($method:ident, $variant:ident, $upper:ident);
|
||||
)+
|
||||
) => {
|
||||
$(
|
||||
doc_comment! {
|
||||
concat!("Creates route handler with `", stringify!($upper), "` method guard.")
|
||||
concat!("")
|
||||
concat!("Syntax: `#[", stringify!($method), "(\"path\" [, attributes])]`")
|
||||
concat!("")
|
||||
concat!("## Attributes:")
|
||||
concat!("")
|
||||
concat!("- `\"path\"` - *required* Raw literal string with path for which to register handler")
|
||||
concat!("- `guard = \"function_name\"` - Register function as guard using `actix_web::guard::fn_guard`")
|
||||
concat!("- `wrap = \"Middleware\"` - Register a resource middleware.");
|
||||
#[proc_macro_attribute]
|
||||
pub fn $method(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::with_method(Some(route::MethodType::$variant), args, input)
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
method_macro! {
|
||||
(get, Get, GET);
|
||||
(post, Post, POST);
|
||||
(put, Put, PUT);
|
||||
(delete, Delete, DELETE);
|
||||
(head, Head, HEAD);
|
||||
(connect, Connect, CONNECT);
|
||||
(options, Options, OPTIONS);
|
||||
(trace, Trace, TRACE);
|
||||
(patch, Patch, PATCH);
|
||||
}
|
||||
|
||||
/// Marks async main function as the actix system entry-point.
|
||||
///
|
||||
/// ## Usage
|
||||
|
|
Loading…
Reference in New Issue