From c53e9468bc1d977afb3514fe7f8239ed9deb2e68 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 24 Sep 2020 23:54:01 +0100 Subject: [PATCH] prepare codegen 0.4.0 release (#1702) --- actix-web-codegen/CHANGES.md | 3 + actix-web-codegen/Cargo.toml | 4 +- actix-web-codegen/src/lib.rs | 97 +++++++++++++++---- .../trybuild/route-duplicate-method-fail.rs | 8 +- .../route-duplicate-method-fail.stderr | 4 +- .../route-missing-method-fail-msrv.rs | 16 +-- .../route-missing-method-fail-msrv.stderr | 4 +- .../trybuild/route-missing-method-fail.rs | 8 +- .../trybuild/route-missing-method-fail.stderr | 4 +- actix-web-codegen/tests/trybuild/route-ok.rs | 8 +- .../trybuild/route-unexpected-method-fail.rs | 8 +- .../route-unexpected-method-fail.stderr | 4 +- .../tests/trybuild/simple-fail.rs | 22 ++--- actix-web-codegen/tests/trybuild/simple.rs | 3 +- 14 files changed, 124 insertions(+), 69 deletions(-) mode change 100644 => 120000 actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.rs diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index 793864d4..ad1a22b8 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2020-xx-xx + + +## 0.4.0 - 2020-09-20 * Added compile success and failure testing. [#1677] * Add `route` macro for supporting multiple HTTP methods guards. diff --git a/actix-web-codegen/Cargo.toml b/actix-web-codegen/Cargo.toml index da37b8de..fd99a837 100644 --- a/actix-web-codegen/Cargo.toml +++ b/actix-web-codegen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web-codegen" -version = "0.3.0" +version = "0.4.0" description = "Actix web proc macros" readme = "README.md" homepage = "https://actix.rs" @@ -19,7 +19,7 @@ syn = { version = "1", features = ["full", "parsing"] } proc-macro2 = "1" [dev-dependencies] -actix-rt = "1.0.0" +actix-rt = "1.1.1" actix-web = "3.0.0" futures-util = { version = "0.3.5", default-features = false } trybuild = "1" diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index 62a1cc5f..af2bc7f1 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -1,6 +1,64 @@ -#![recursion_limit = "512"] +//! Macros for reducing boilerplate code in Actix Web applications. +//! +//! ## Actix Web Re-exports +//! Actix Web re-exports a version of this crate in it's entirety so you usually don't have to +//! specify a dependency on this crate explicitly. Sometimes, however, updates are made to this +//! crate before the actix-web dependency is updated. Therefore, code examples here will show +//! explicit imports. Check the latest [actix-web attributes docs] to see which macros +//! are re-exported. +//! +//! # Runtime Setup +//! Used for setting up the actix async runtime. See [main] macro docs. +//! +//! ```rust +//! #[actix_web_codegen::main] // or `#[actix_web::main]` in Actix Web apps +//! async fn main() { +//! async { println!("Hello world"); }.await +//! } +//! ``` +//! +//! # Single Method Handler +//! There is a macro to set up a handler for each of the most common HTTP methods that also define +//! additional guards and route-specific middleware. +//! +//! See docs for: [GET], [POST], [PATCH], [PUT], [DELETE], [HEAD], [CONNECT], [OPTIONS], [TRACE] +//! +//! ```rust +//! # use actix_web::HttpResponse; +//! # use actix_web_codegen::get; +//! #[get("/test")] +//! async fn get_handler() -> HttpResponse { +//! HttpResponse::Ok().finish() +//! } +//! ``` +//! +//! # Multiple Method Handlers +//! Similar to the single method handler macro but takes one or more arguments for the HTTP methods +//! it should respond to. See [route] macro docs. +//! +//! ```rust +//! # use actix_web::HttpResponse; +//! # use actix_web_codegen::route; +//! #[route("/test", method="GET", method="HEAD")] +//! async fn get_and_head_handler() -> HttpResponse { +//! HttpResponse::Ok().finish() +//! } +//! ``` +//! +//! [actix-web attributes docs]: https://docs.rs/actix-web/*/actix_web/#attributes +//! [main]: attr.main.html +//! [route]: attr.route.html +//! [GET]: attr.get.html +//! [POST]: attr.post.html +//! [PUT]: attr.put.html +//! [DELETE]: attr.delete.html +//! [HEAD]: attr.head.html +//! [CONNECT]: attr.connect.html +//! [OPTIONS]: attr.options.html +//! [TRACE]: attr.trace.html +//! [PATCH]: attr.patch.html -extern crate proc_macro; +#![recursion_limit = "512"] use proc_macro::TokenStream; @@ -8,28 +66,27 @@ mod route; /// Creates resource handler, allowing multiple HTTP method guards. /// -/// ## Syntax +/// # Syntax /// ```text /// #[route("path", method="HTTP_METHOD"[, attributes])] /// ``` /// -/// ### Attributes +/// # Attributes /// - `"path"` - Raw literal string with path for which to register handler. /// - `method="HTTP_METHOD"` - Registers HTTP method to provide guard for. Upper-case string, "GET", "POST" for example. /// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard` /// - `wrap="Middleware"` - Registers a resource middleware. /// -/// ### Notes +/// # Notes /// 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`. /// -/// ## Example +/// # Example /// /// ```rust -/// use actix_web::HttpResponse; -/// use actix_web_codegen::route; -/// -/// #[route("/", method="GET", method="HEAD")] +/// # use actix_web::HttpResponse; +/// # use actix_web_codegen::route; +/// #[route("/test", method="GET", method="HEAD")] /// async fn example() -> HttpResponse { /// HttpResponse::Ok().finish() /// } @@ -54,26 +111,25 @@ macro_rules! method_macro { concat!(" Creates route handler with `actix_web::guard::", stringify!($variant), "`. -## Syntax +# Syntax ```text #[", stringify!($method), r#"("path"[, attributes])] ``` -### Attributes +# Attributes - `"path"` - Raw literal string with path for which to register handler. - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`. - `wrap="Middleware"` - Registers a resource middleware. -### Notes +# Notes 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`. -## Example +# Example ```rust -use actix_web::HttpResponse; -use actix_web_codegen::"#, stringify!($method), "; - +# use actix_web::HttpResponse; +# use actix_web_codegen::"#, stringify!($method), "; #[", stringify!($method), r#"("/")] async fn example() -> HttpResponse { HttpResponse::Ok().finish() @@ -102,16 +158,17 @@ method_macro! { /// Marks async main function as the actix system entry-point. /// -/// ## Usage +/// # Actix Web Re-export +/// This macro can be applied with `#[actix_web::main]` when used in Actix Web applications. /// +/// # Usage /// ```rust -/// #[actix_web::main] +/// #[actix_web_codegen::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; diff --git a/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs index 9ce98025..9a38050f 100644 --- a/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs +++ b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.rs @@ -1,12 +1,14 @@ -use actix_web::*; +use actix_web_codegen::*; #[route("/", method="GET", method="GET")] -async fn index() -> impl Responder { - HttpResponse::Ok() +async fn index() -> String { + "Hello World!".to_owned() } #[actix_web::main] async fn main() { + use actix_web::{App, test}; + let srv = test::start(|| App::new().service(index)); let request = srv.get("/"); diff --git a/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr index 613054de..f3eda68a 100644 --- a/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr +++ b/actix-web-codegen/tests/trybuild/route-duplicate-method-fail.stderr @@ -5,7 +5,7 @@ error: HTTP method defined more than once: `GET` | ^^^^^ error[E0425]: cannot find value `index` in this scope - --> $DIR/route-duplicate-method-fail.rs:10:49 + --> $DIR/route-duplicate-method-fail.rs:12:49 | -10 | let srv = test::start(|| App::new().service(index)); +12 | let srv = test::start(|| App::new().service(index)); | ^^^^^ not found in this scope diff --git a/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.rs b/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.rs deleted file mode 100644 index 5c30b57c..00000000 --- a/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.rs +++ /dev/null @@ -1,15 +0,0 @@ -use actix_web::*; - -#[route("/")] -async fn index() -> impl Responder { - HttpResponse::Ok() -} - -#[actix_web::main] -async fn main() { - let srv = test::start(|| App::new().service(index)); - - let request = srv.get("/"); - let response = request.send().await.unwrap(); - assert!(response.status().is_success()); -} diff --git a/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.rs b/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.rs new file mode 120000 index 00000000..70a5c0e3 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.rs @@ -0,0 +1 @@ +route-missing-method-fail.rs \ No newline at end of file diff --git a/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.stderr b/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.stderr index f59f6c27..d3e2b60a 100644 --- a/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.stderr +++ b/actix-web-codegen/tests/trybuild/route-missing-method-fail-msrv.stderr @@ -5,7 +5,7 @@ error: The #[route(..)] macro requires at least one `method` attribute | ^^^^^^^^^^^^^ error[E0425]: cannot find value `index` in this scope - --> $DIR/route-missing-method-fail-msrv.rs:10:49 + --> $DIR/route-missing-method-fail-msrv.rs:12:49 | -10 | let srv = test::start(|| App::new().service(index)); +12 | let srv = test::start(|| App::new().service(index)); | ^^^^^ not found in this scope diff --git a/actix-web-codegen/tests/trybuild/route-missing-method-fail.rs b/actix-web-codegen/tests/trybuild/route-missing-method-fail.rs index 5c30b57c..ce87a55a 100644 --- a/actix-web-codegen/tests/trybuild/route-missing-method-fail.rs +++ b/actix-web-codegen/tests/trybuild/route-missing-method-fail.rs @@ -1,12 +1,14 @@ -use actix_web::*; +use actix_web_codegen::*; #[route("/")] -async fn index() -> impl Responder { - HttpResponse::Ok() +async fn index() -> String { + "Hello World!".to_owned() } #[actix_web::main] async fn main() { + use actix_web::{App, test}; + let srv = test::start(|| App::new().service(index)); let request = srv.get("/"); diff --git a/actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr b/actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr index 6d35ea60..0518a61e 100644 --- a/actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr +++ b/actix-web-codegen/tests/trybuild/route-missing-method-fail.stderr @@ -7,7 +7,7 @@ error: The #[route(..)] macro requires at least one `method` attribute = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find value `index` in this scope - --> $DIR/route-missing-method-fail.rs:10:49 + --> $DIR/route-missing-method-fail.rs:12:49 | -10 | let srv = test::start(|| App::new().service(index)); +12 | let srv = test::start(|| App::new().service(index)); | ^^^^^ not found in this scope diff --git a/actix-web-codegen/tests/trybuild/route-ok.rs b/actix-web-codegen/tests/trybuild/route-ok.rs index bfac56e1..c4f67960 100644 --- a/actix-web-codegen/tests/trybuild/route-ok.rs +++ b/actix-web-codegen/tests/trybuild/route-ok.rs @@ -1,12 +1,14 @@ -use actix_web::*; +use actix_web_codegen::*; #[route("/", method="GET", method="HEAD")] -async fn index() -> impl Responder { - HttpResponse::Ok() +async fn index() -> String { + "Hello World!".to_owned() } #[actix_web::main] async fn main() { + use actix_web::{App, test}; + let srv = test::start(|| App::new().service(index)); let request = srv.get("/"); diff --git a/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs index f4d8d944..28cd1344 100644 --- a/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs +++ b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.rs @@ -1,12 +1,14 @@ -use actix_web::*; +use actix_web_codegen::*; #[route("/", method="UNEXPECTED")] -async fn index() -> impl Responder { - HttpResponse::Ok() +async fn index() -> String { + "Hello World!".to_owned() } #[actix_web::main] async fn main() { + use actix_web::{App, test}; + let srv = test::start(|| App::new().service(index)); let request = srv.get("/"); diff --git a/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr index fe17fdf1..9d87f310 100644 --- a/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr +++ b/actix-web-codegen/tests/trybuild/route-unexpected-method-fail.stderr @@ -5,7 +5,7 @@ error: Unexpected HTTP method: `UNEXPECTED` | ^^^^^^^^^^^^ error[E0425]: cannot find value `index` in this scope - --> $DIR/route-unexpected-method-fail.rs:10:49 + --> $DIR/route-unexpected-method-fail.rs:12:49 | -10 | let srv = test::start(|| App::new().service(index)); +12 | let srv = test::start(|| App::new().service(index)); | ^^^^^ not found in this scope diff --git a/actix-web-codegen/tests/trybuild/simple-fail.rs b/actix-web-codegen/tests/trybuild/simple-fail.rs index 368cff04..a57fdc16 100644 --- a/actix-web-codegen/tests/trybuild/simple-fail.rs +++ b/actix-web-codegen/tests/trybuild/simple-fail.rs @@ -1,30 +1,30 @@ -use actix_web::*; +use actix_web_codegen::*; #[get("/one", other)] -async fn one() -> impl Responder { - HttpResponse::Ok() +async fn one() -> String { + "Hello World!".to_owned() } #[post(/two)] -async fn two() -> impl Responder { - HttpResponse::Ok() +async fn two() -> String { + "Hello World!".to_owned() } static PATCH_PATH: &str = "/three"; #[patch(PATCH_PATH)] -async fn three() -> impl Responder { - HttpResponse::Ok() +async fn three() -> String { + "Hello World!".to_owned() } #[delete("/four", "/five")] -async fn four() -> impl Responder { - HttpResponse::Ok() +async fn four() -> String { + "Hello World!".to_owned() } #[delete("/five", method="GET")] -async fn five() -> impl Responder { - HttpResponse::Ok() +async fn five() -> String { + "Hello World!".to_owned() } fn main() {} diff --git a/actix-web-codegen/tests/trybuild/simple.rs b/actix-web-codegen/tests/trybuild/simple.rs index 6b1e6744..761b0490 100644 --- a/actix-web-codegen/tests/trybuild/simple.rs +++ b/actix-web-codegen/tests/trybuild/simple.rs @@ -1,4 +1,5 @@ -use actix_web::*; +use actix_web::{Responder, HttpResponse, App, test}; +use actix_web_codegen::*; #[get("/config")] async fn config() -> impl Responder {