actix_web_codegen/lib.rs
1//! Routing and runtime macros for Actix Web.
2//!
3//! # Actix Web Re-exports
4//! Actix Web re-exports a version of this crate in it's entirety so you usually don't have to
5//! specify a dependency on this crate explicitly. Sometimes, however, updates are made to this
6//! crate before the actix-web dependency is updated. Therefore, code examples here will show
7//! explicit imports. Check the latest [actix-web attributes docs] to see which macros
8//! are re-exported.
9//!
10//! # Runtime Setup
11//! Used for setting up the actix async runtime. See [macro@main] macro docs.
12//!
13//! ```
14//! #[actix_web_codegen::main] // or `#[actix_web::main]` in Actix Web apps
15//! async fn main() {
16//! async { println!("Hello world"); }.await
17//! }
18//! ```
19//!
20//! # Single Method Handler
21//! There is a macro to set up a handler for each of the most common HTTP methods that also define
22//! additional guards and route-specific middleware.
23//!
24//! See docs for: [GET], [POST], [PATCH], [PUT], [DELETE], [HEAD], [CONNECT], [OPTIONS], [TRACE]
25//!
26//! ```
27//! # use actix_web::HttpResponse;
28//! # use actix_web_codegen::get;
29//! #[get("/test")]
30//! async fn get_handler() -> HttpResponse {
31//! HttpResponse::Ok().finish()
32//! }
33//! ```
34//!
35//! # Multiple Method Handlers
36//! Similar to the single method handler macro but takes one or more arguments for the HTTP methods
37//! it should respond to. See [macro@route] macro docs.
38//!
39//! ```
40//! # use actix_web::HttpResponse;
41//! # use actix_web_codegen::route;
42//! #[route("/test", method = "GET", method = "HEAD")]
43//! async fn get_and_head_handler() -> HttpResponse {
44//! HttpResponse::Ok().finish()
45//! }
46//! ```
47//!
48//! # Multiple Path Handlers
49//! Acts as a wrapper for multiple single method handler macros. It takes no arguments and
50//! delegates those to the macros for the individual methods. See [macro@routes] macro docs.
51//!
52//! ```
53//! # use actix_web::HttpResponse;
54//! # use actix_web_codegen::routes;
55//! #[routes]
56//! #[get("/test")]
57//! #[get("/test2")]
58//! #[delete("/test")]
59//! async fn example() -> HttpResponse {
60//! HttpResponse::Ok().finish()
61//! }
62//! ```
63//!
64//! [actix-web attributes docs]: https://docs.rs/actix-web/latest/actix_web/#attributes
65//! [GET]: macro@get
66//! [POST]: macro@post
67//! [PUT]: macro@put
68//! [HEAD]: macro@head
69//! [CONNECT]: macro@macro@connect
70//! [OPTIONS]: macro@options
71//! [TRACE]: macro@trace
72//! [PATCH]: macro@patch
73//! [DELETE]: macro@delete
74
75#![recursion_limit = "512"]
76#![deny(rust_2018_idioms, nonstandard_style)]
77#![warn(future_incompatible)]
78#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
79#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
80#![cfg_attr(docsrs, feature(doc_auto_cfg))]
81
82use proc_macro::TokenStream;
83use quote::quote;
84
85mod route;
86mod scope;
87
88/// Creates resource handler, allowing multiple HTTP method guards.
89///
90/// # Syntax
91/// ```plain
92/// #[route("path", method="HTTP_METHOD"[, attributes])]
93/// ```
94///
95/// # Attributes
96/// - `"path"`: Raw literal string with path for which to register handler.
97/// - `name = "resource_name"`: Specifies resource name for the handler. If not set, the function
98/// name of handler is used.
99/// - `method = "HTTP_METHOD"`: Registers HTTP method to provide guard for. Upper-case string,
100/// "GET", "POST" for example.
101/// - `guard = "function_name"`: Registers function as guard using `actix_web::guard::fn_guard`.
102/// - `wrap = "Middleware"`: Registers a resource middleware.
103///
104/// # Notes
105/// Function name can be specified as any expression that is going to be accessible to the generate
106/// code, e.g `my_guard` or `my_module::my_guard`.
107///
108/// # Examples
109/// ```
110/// # use actix_web::HttpResponse;
111/// # use actix_web_codegen::route;
112/// #[route("/test", method = "GET", method = "HEAD", method = "CUSTOM")]
113/// async fn example() -> HttpResponse {
114/// HttpResponse::Ok().finish()
115/// }
116/// ```
117#[proc_macro_attribute]
118pub fn route(args: TokenStream, input: TokenStream) -> TokenStream {
119 route::with_method(None, args, input)
120}
121
122/// Creates resource handler, allowing multiple HTTP methods and paths.
123///
124/// # Syntax
125/// ```plain
126/// #[routes]
127/// #[<method>("path", ...)]
128/// #[<method>("path", ...)]
129/// ...
130/// ```
131///
132/// # Attributes
133/// The `routes` macro itself has no parameters, but allows specifying the attribute macros for
134/// the multiple paths and/or methods, e.g. [`GET`](macro@get) and [`POST`](macro@post).
135///
136/// These helper attributes take the same parameters as the [single method handlers](crate#single-method-handler).
137///
138/// # Examples
139/// ```
140/// # use actix_web::HttpResponse;
141/// # use actix_web_codegen::routes;
142/// #[routes]
143/// #[get("/test")]
144/// #[get("/test2")]
145/// #[delete("/test")]
146/// async fn example() -> HttpResponse {
147/// HttpResponse::Ok().finish()
148/// }
149/// ```
150#[proc_macro_attribute]
151pub fn routes(_: TokenStream, input: TokenStream) -> TokenStream {
152 route::with_methods(input)
153}
154
155macro_rules! method_macro {
156 ($variant:ident, $method:ident) => {
157 #[doc = concat!("Creates route handler with `actix_web::guard::", stringify!($variant), "`.")]
158 ///
159 /// # Syntax
160 /// ```plain
161 #[doc = concat!("#[", stringify!($method), r#"("path"[, attributes])]"#)]
162 /// ```
163 ///
164 /// # Attributes
165 /// - `"path"`: Raw literal string with path for which to register handler.
166 /// - `name = "resource_name"`: Specifies resource name for the handler. If not set, the
167 /// function name of handler is used.
168 /// - `guard = "function_name"`: Registers function as guard using `actix_web::guard::fn_guard`.
169 /// - `wrap = "Middleware"`: Registers a resource middleware.
170 ///
171 /// # Notes
172 /// Function name can be specified as any expression that is going to be accessible to the
173 /// generate code, e.g `my_guard` or `my_module::my_guard`.
174 ///
175 /// # Examples
176 /// ```
177 /// # use actix_web::HttpResponse;
178 #[doc = concat!("# use actix_web_codegen::", stringify!($method), ";")]
179 #[doc = concat!("#[", stringify!($method), r#"("/")]"#)]
180 /// async fn example() -> HttpResponse {
181 /// HttpResponse::Ok().finish()
182 /// }
183 /// ```
184 #[proc_macro_attribute]
185 pub fn $method(args: TokenStream, input: TokenStream) -> TokenStream {
186 route::with_method(Some(route::MethodType::$variant), args, input)
187 }
188 };
189}
190
191method_macro!(Get, get);
192method_macro!(Post, post);
193method_macro!(Put, put);
194method_macro!(Delete, delete);
195method_macro!(Head, head);
196method_macro!(Connect, connect);
197method_macro!(Options, options);
198method_macro!(Trace, trace);
199method_macro!(Patch, patch);
200
201/// Prepends a path prefix to all handlers using routing macros inside the attached module.
202///
203/// # Syntax
204///
205/// ```
206/// # use actix_web_codegen::scope;
207/// #[scope("/prefix")]
208/// mod api {
209/// // ...
210/// }
211/// ```
212///
213/// # Arguments
214///
215/// - `"/prefix"` - Raw literal string to be prefixed onto contained handlers' paths.
216///
217/// # Example
218///
219/// ```
220/// # use actix_web_codegen::{scope, get};
221/// # use actix_web::Responder;
222/// #[scope("/api")]
223/// mod api {
224/// # use super::*;
225/// #[get("/hello")]
226/// pub async fn hello() -> impl Responder {
227/// // this has path /api/hello
228/// "Hello, world!"
229/// }
230/// }
231/// # fn main() {}
232/// ```
233#[proc_macro_attribute]
234pub fn scope(args: TokenStream, input: TokenStream) -> TokenStream {
235 scope::with_scope(args, input)
236}
237
238/// Marks async main function as the Actix Web system entry-point.
239///
240/// Note that Actix Web also works under `#[tokio::main]` since version 4.0. However, this macro is
241/// still necessary for actor support (since actors use a `System`). Read more in the
242/// [`actix_web::rt`](https://docs.rs/actix-web/4/actix_web/rt) module docs.
243///
244/// # Examples
245/// ```
246/// #[actix_web::main]
247/// async fn main() {
248/// async { println!("Hello world"); }.await
249/// }
250/// ```
251#[proc_macro_attribute]
252pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
253 let mut output: TokenStream = (quote! {
254 #[::actix_web::rt::main(system = "::actix_web::rt::System")]
255 })
256 .into();
257
258 output.extend(item);
259 output
260}
261
262/// Marks async test functions to use the Actix Web system entry-point.
263///
264/// # Examples
265/// ```
266/// #[actix_web::test]
267/// async fn test() {
268/// assert_eq!(async { "Hello world" }.await, "Hello world");
269/// }
270/// ```
271#[proc_macro_attribute]
272pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
273 let mut output: TokenStream = (quote! {
274 #[::actix_web::rt::test(system = "::actix_web::rt::System")]
275 })
276 .into();
277
278 output.extend(item);
279 output
280}
281
282/// Converts the error to a token stream and appends it to the original input.
283///
284/// Returning the original input in addition to the error is good for IDEs which can gracefully
285/// recover and show more precise errors within the macro body.
286///
287/// See <https://github.com/rust-analyzer/rust-analyzer/issues/10468> for more info.
288fn input_and_compile_error(mut item: TokenStream, err: syn::Error) -> TokenStream {
289 let compile_err = TokenStream::from(err.to_compile_error());
290 item.extend(compile_err);
291 item
292}