Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#[any] macro #3310

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion actix-web-codegen/Cargo.toml
Expand Up @@ -28,5 +28,5 @@ actix-utils = "3"
actix-web = "4"

futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
trybuild = "1"
trybuild = "1.0"
rustversion = "1"
3 changes: 2 additions & 1 deletion actix-web-codegen/src/lib.rs
Expand Up @@ -84,7 +84,7 @@ use quote::quote;

mod route;

/// Creates resource handler, allowing multiple HTTP method guards.
/// Creates resource handler , allowing multiple HTTP method guards.
///
/// # Syntax
/// ```plain
Expand Down Expand Up @@ -196,6 +196,7 @@ method_macro!(Connect, connect);
method_macro!(Options, options);
method_macro!(Trace, trace);
method_macro!(Patch, patch);
method_macro!(All, all);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will almost certainly be easier with a new macro creator rather than trying to retro-fit method_macro!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is making a custom http server and defining any a way or is there a better way? @robjtede

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it would need to generate would be similar to the other macros (an HttpServiceFactory implementation) except without adding any method guards to the route.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry but I am not sure if I understand @robjtede

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-03-17 123942
Do I need to define it here?
Screenshot 2024-03-17 124134
Or in service.rs in actix-http.
Pardon me for asking so many questions btw


/// Marks async main function as the Actix Web system entry-point.
///
Expand Down
3 changes: 2 additions & 1 deletion actix-web-codegen/src/route.rs
Expand Up @@ -62,12 +62,13 @@
$(
$variant,
)+
All
}

impl MethodType {
fn as_str(&self) -> &'static str {
match self {
$(Self::$variant => stringify!($variant),)+
$(Self::$variant => stringify!($variant), &MethodType::All => stringify!("All"),)+

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: unreachable pattern --> actix-web-codegen/src/route.rs:71:63 | 71 | $(Self::$variant => stringify!($variant), &MethodType::All => stringify!("All"),)+ | ^^^^^^^^^^^^^^^^ ... 92 | / standard_method_type! { 93 | | Get, GET, get, 94 | | Post, POST, post, 95 | | Put, PUT, put, ... | 101 | | Patch, PATCH, patch, 102 | | } | |_- in this macro invocation | = note: `-D unreachable-patterns` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unreachable_patterns)]` = note: this error originates in the macro `standard_method_type` (in Nightly builds, run with -Z macro-backtrace for more info) Raw Output: actix-web-codegen/src/route.rs:71:63:e:error: unreachable pattern --> actix-web-codegen/src/route.rs:71:63 | 71 | $(Self::$variant => stringify!($variant), &MethodType::All => stringify!("All"),)+ | ^^^^^^^^^^^^^^^^ ... 92 | / standard_method_type! { 93 | | Get, GET, get, 94 | | Post, POST, post, 95 | | Put, PUT, put, ... | 101 | | Patch, PATCH, patch, 102 | | } | |_- in this macro invocation | = note: `-D unreachable-patterns` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unreachable_patterns)]` = note: this error originates in the macro `standard_method_type` (in Nightly builds, run with -Z macro-backtrace for more info) __END__

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / doc tests

unreachable pattern

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Linux / stable

unreachable pattern

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / lint-docs

unreachable pattern

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / public-api-diff

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / io-uring tests

unreachable pattern

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Linux / msrv

unreachable pattern

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / macOS / stable

unreachable pattern

Check failure on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / macOS / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / msrv

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern

Check warning on line 71 in actix-web-codegen/src/route.rs

View workflow job for this annotation

GitHub Actions / Windows / stable

unreachable pattern
}
}

Expand Down
5 changes: 3 additions & 2 deletions actix-web-codegen/tests/trybuild.rs
@@ -1,4 +1,4 @@
#[rustversion::stable(1.72)] // MSRV
//#[rustversion::stable(1.72)] // MSRV
#[test]
fn compile_macros() {
let t = trybuild::TestCases::new();
Expand All @@ -20,5 +20,6 @@

t.pass("tests/trybuild/docstring-ok.rs");

t.pass("tests/trybuild/test-runtime.rs");

Check warning on line 23 in actix-web-codegen/tests/trybuild.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/actix-web/actix-web/actix-web-codegen/tests/trybuild.rs
}
t.pass("tests/trybuild/route-any-method.rs");
}
25 changes: 25 additions & 0 deletions actix-web-codegen/tests/trybuild/route-any-method.rs
@@ -0,0 +1,25 @@
use std::str::FromStr;

use actix_web::http::Method;
use actix_web_codegen::route;

#[route("/single", method = "ALL")]
async fn index() -> String {
"Hello Single!".to_owned()
}
Comment on lines +6 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we were trying to aim more so for the dedicated macro style:

#[any("/path")]
async handler() {
  // ...


#[route("/multi", method = "GET", method = "ALL")]
async fn custom() -> String {
"Hello Multi!".to_owned()
}

#[actix_web::main]
async fn main() {
use actix_web::App;

let srv = actix_test::start(|| App::new().service(index).service(custom));

let request = srv.request(Method::from_str("ALL").unwrap(), srv.url("/single"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}
14 changes: 7 additions & 7 deletions actix-web-codegen/tests/trybuild/route-custom-method.rs
Expand Up @@ -8,7 +8,7 @@ async fn index() -> String {
"Hello Single!".to_owned()
}

#[route("/multi", method = "GET", method = "CUSTOM")]
#[route("/multi", method = "CUSTOM")]
async fn custom() -> String {
"Hello Multi!".to_owned()
}
Expand All @@ -19,17 +19,17 @@ async fn main() {

let srv = actix_test::start(|| App::new().service(index).service(custom));

let request = srv.request(Method::GET, srv.url("/"));
let response = request.send().await.unwrap();
assert!(response.status().is_client_error());
// let request = srv.request(Method::GET, srv.url("/"));
// let response = request.send().await.unwrap();
// assert!(response.status().is_client_error());

let request = srv.request(Method::from_str("CUSTOM").unwrap(), srv.url("/single"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());

let request = srv.request(Method::GET, srv.url("/multi"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());
// let request = srv.request(Method::GET, srv.url("/multi"));
// let response = request.send().await.unwrap();
// assert!(response.status().is_success());

let request = srv.request(Method::from_str("CUSTOM").unwrap(), srv.url("/multi"));
let response = request.send().await.unwrap();
Expand Down