Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actix-web-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ use quote::quote;
mod route;
mod scope;

/// Creates resource handler, allowing multiple HTTP method guards.
/// Creates resource handler , allowing multiple HTTP method guards.
///
/// # Syntax
/// ```plain
Expand Down Expand Up @@ -197,6 +197,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


/// Prepends a path prefix to all handlers using routing macros inside the attached module.
///
Expand Down
3 changes: 2 additions & 1 deletion actix-web-codegen/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ macro_rules! standard_method_type {
$(
$variant,
)+
All
}

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

Expand Down
5 changes: 3 additions & 2 deletions actix-web-codegen/tests/trybuild.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[rustversion::stable(1.72)] // MSRV
//#[rustversion::stable(1.72)] // MSRV
#[test]
fn compile_macros() {
let t = trybuild::TestCases::new();
Expand Down Expand Up @@ -26,4 +26,5 @@ fn compile_macros() {
t.pass("tests/trybuild/docstring-ok.rs");

t.pass("tests/trybuild/test-runtime.rs");
}
t.pass("tests/trybuild/route-any-method.rs");
}
25 changes: 25 additions & 0 deletions actix-web-codegen/tests/trybuild/route-any-method.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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