2019-03-07 19:09:42 +00:00
|
|
|
#![recursion_limit = "512"]
|
2020-06-22 19:09:48 +00:00
|
|
|
|
|
|
|
//! Helper and convenience macros for Actix-web.
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
2020-06-22 19:09:48 +00:00
|
|
|
//! ## Runtime Setup
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
2020-06-22 19:09:48 +00:00
|
|
|
//! - [main](attr.main.html)
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
2020-06-22 19:09:48 +00:00
|
|
|
//! ## Resource Macros:
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
|
|
|
//! - [get](attr.get.html)
|
|
|
|
//! - [post](attr.post.html)
|
|
|
|
//! - [put](attr.put.html)
|
|
|
|
//! - [delete](attr.delete.html)
|
2019-06-04 16:30:43 +00:00
|
|
|
//! - [head](attr.head.html)
|
|
|
|
//! - [connect](attr.connect.html)
|
|
|
|
//! - [options](attr.options.html)
|
|
|
|
//! - [trace](attr.trace.html)
|
|
|
|
//! - [patch](attr.patch.html)
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
|
|
|
//! ### Attributes:
|
|
|
|
//!
|
|
|
|
//! - `"path"` - Raw literal string with path for which to register handle. Mandatory.
|
|
|
|
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
2020-05-07 09:31:12 +00:00
|
|
|
//! - `wrap="Middleware"` - Registers a resource middleware.
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
2020-06-22 19:09:48 +00:00
|
|
|
//! ### Notes
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
|
|
|
//! Function name can be specified as any expression that is going to be accessible to the generate
|
|
|
|
//! code (e.g `my_guard` or `my_module::my_guard`)
|
|
|
|
//!
|
2020-06-22 19:09:48 +00:00
|
|
|
//! ### Example:
|
2019-03-31 07:23:15 +00:00
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use actix_web::HttpResponse;
|
|
|
|
//! use actix_web_codegen::get;
|
|
|
|
//!
|
|
|
|
//! #[get("/test")]
|
2019-11-21 07:01:07 +00:00
|
|
|
//! async fn async_test() -> Result<HttpResponse, actix_web::Error> {
|
|
|
|
//! Ok(HttpResponse::Ok().finish())
|
2019-03-31 07:23:15 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
2019-03-07 19:09:42 +00:00
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
2019-03-31 07:23:15 +00:00
|
|
|
mod route;
|
|
|
|
|
2019-03-07 19:09:42 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
|
2019-03-31 07:23:15 +00:00
|
|
|
/// Creates route handler with `GET` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[get("path" [, attributes])]`
|
2019-03-31 07:23:15 +00:00
|
|
|
///
|
|
|
|
/// ## Attributes:
|
|
|
|
///
|
|
|
|
/// - `"path"` - Raw literal string with path for which to register handler. Mandatory.
|
2020-09-13 15:31:08 +00:00
|
|
|
/// - `guard = "function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
|
|
|
/// - `wrap = "Middleware"` - Registers a resource middleware.
|
2019-03-07 19:09:42 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Get)
|
2019-03-07 19:09:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 07:23:15 +00:00
|
|
|
/// Creates route handler with `POST` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[post("path" [, attributes])]`
|
2019-03-31 07:23:15 +00:00
|
|
|
///
|
|
|
|
/// Attributes are the same as in [get](attr.get.html)
|
2019-03-07 19:09:42 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Post)
|
2019-03-07 19:09:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 07:23:15 +00:00
|
|
|
/// Creates route handler with `PUT` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[put("path" [, attributes])]`
|
2019-03-31 07:23:15 +00:00
|
|
|
///
|
|
|
|
/// Attributes are the same as in [get](attr.get.html)
|
2019-03-07 19:09:42 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Put)
|
2019-03-31 07:23:15 +00:00
|
|
|
}
|
2019-03-07 19:09:42 +00:00
|
|
|
|
2019-03-31 07:23:15 +00:00
|
|
|
/// Creates route handler with `DELETE` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[delete("path" [, attributes])]`
|
2019-03-31 07:23:15 +00:00
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Attributes are the same as in [get](attr.get.html).
|
2019-03-31 07:23:15 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Delete)
|
2019-03-07 19:09:42 +00:00
|
|
|
}
|
2019-06-04 16:30:43 +00:00
|
|
|
|
|
|
|
/// Creates route handler with `HEAD` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[head("path" [, attributes])]`
|
2019-06-04 16:30:43 +00:00
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Attributes are the same as in [get](attr.get.html).
|
2019-06-04 16:30:43 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn head(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Head)
|
2019-06-04 16:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates route handler with `CONNECT` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[connect("path" [, attributes])]`
|
2019-06-04 16:30:43 +00:00
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Attributes are the same as in [get](attr.get.html).
|
2019-06-04 16:30:43 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn connect(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Connect)
|
2019-06-04 16:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates route handler with `OPTIONS` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[options("path" [, attributes])]`
|
2019-06-04 16:30:43 +00:00
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Attributes are the same as in [get](attr.get.html).
|
2019-06-04 16:30:43 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn options(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Options)
|
2019-06-04 16:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates route handler with `TRACE` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[trace("path" [, attributes])]`
|
2019-06-04 16:30:43 +00:00
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Attributes are the same as in [get](attr.get.html).
|
2019-06-04 16:30:43 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Trace)
|
2019-06-04 16:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates route handler with `PATCH` method guard.
|
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Syntax: `#[patch("path" [, attributes])]`
|
2019-06-04 16:30:43 +00:00
|
|
|
///
|
2020-09-13 15:31:08 +00:00
|
|
|
/// Attributes are the same as in [get](attr.get.html).
|
2019-06-04 16:30:43 +00:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
|
2020-03-19 19:40:42 +00:00
|
|
|
route::generate(args, input, route::GuardType::Patch)
|
2019-06-05 02:43:13 +00:00
|
|
|
}
|
2020-06-22 19:09:48 +00:00
|
|
|
|
2020-09-16 21:37:41 +00:00
|
|
|
/// Creates resource handler, allowing multiple HTTP method guards.
|
|
|
|
///
|
|
|
|
/// Syntax: `#[route("path"[, attributes])]`
|
|
|
|
///
|
|
|
|
/// Example: `#[route("/", method="GET", method="HEAD")]`
|
|
|
|
///
|
|
|
|
/// ## Attributes
|
|
|
|
///
|
|
|
|
/// - `"path"` - Raw literal string with path for which to register handler. Mandatory.
|
|
|
|
/// - `method="HTTP_METHOD"` - Registers HTTP method to provide guard for.
|
|
|
|
/// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
|
|
|
/// - `wrap="Middleware"` - Registers a resource middleware.
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn route(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
route::generate(args, input, route::GuardType::Multi)
|
|
|
|
}
|
|
|
|
|
2020-06-22 19:09:48 +00:00
|
|
|
/// Marks async main function as the actix system entry-point.
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// #[actix_web::main]
|
|
|
|
/// async fn main() {
|
|
|
|
/// async { println!("Hello world"); }.await
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
#[cfg(not(test))] // Work around for rust-lang/rust#62127
|
|
|
|
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
use quote::quote;
|
|
|
|
|
|
|
|
let mut input = syn::parse_macro_input!(item as syn::ItemFn);
|
|
|
|
let attrs = &input.attrs;
|
|
|
|
let vis = &input.vis;
|
|
|
|
let sig = &mut input.sig;
|
|
|
|
let body = &input.block;
|
|
|
|
let name = &sig.ident;
|
|
|
|
|
|
|
|
if sig.asyncness.is_none() {
|
|
|
|
return syn::Error::new_spanned(sig.fn_token, "only async fn is supported")
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
sig.asyncness = None;
|
|
|
|
|
|
|
|
(quote! {
|
|
|
|
#(#attrs)*
|
|
|
|
#vis #sig {
|
|
|
|
actix_web::rt::System::new(stringify!(#name))
|
|
|
|
.block_on(async move { #body })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.into()
|
|
|
|
}
|