actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Scope macro

Open pmd3d opened this issue 2 years ago • 1 comments

PR Type

Start of adding proc macro scope feature based on issue #1709.

PR Checklist

  • [x] Tests for the changes have been added / updated.
  • [x] Documentation comments have been added / updated.
  • [x] A changelog entry has been made for the appropriate packages.
  • [x] Format code with the latest stable rustfmt.
  • [x] (Team) Label with affected crates and semver status.

Overview

Like the project the issue referenced, I added scope proc macro that takes a path. I couldn't figure out how the rest of the features would work int he referenced project so did not implement them.

This is my first major pull request. I decided to remove non path arguments to the scope proc macro to reduce the size of this pull request assuming there may be other issues given my inexperience. Also the issue references a handler macro that I did not implement because I wasn't sure exactly if perhaps that was covered by a get macro or should be something more.

Issue #1709

For example, the scope macro together with a prefix path would be declarated before module like below. Then, web methods in the module would get the prefix path concatenated to the path argument.

use actix_web_codegen::{scope};
#[scope("/test")]
mod scope_module {
  use actix_web::{get, HttpResponse, Responder};
  #[get("/test")]
  pub async fn test() -> impl Responder {
    // this has path /test/test
    HttpResponse::Ok().finish()
  }
}

pmd3d avatar Sep 14 '23 06:09 pmd3d

Converted to draft. Rust 1.72 was mentioned in changes markdown. It seems like Rust 1.72 supports adding the proc macro directly to a module so I'm going to rework this pull request to remove the const function workaround and try having the macro directly on a module.

pmd3d avatar Feb 15 '24 18:02 pmd3d

Done a bit of re-organization and docs tweaking. Think it's in a good state to merge.

One more thing I want to add is to disallow trailing slashes to align with this piece of documentation and it's reasoning: https://docs.rs/actix-web/latest/actix_web/struct.Scope.html#avoid-trailing-slashes

robjtede avatar Jun 07 '24 21:06 robjtede