Rocket icon indicating copy to clipboard operation
Rocket copied to clipboard

Specific argument names in route macros.

Open burningtnt opened this issue 6 months ago • 5 comments

What's missing?

Consider this situation:

#[get("/foo?<anApple>")]
fn foo(anApple: String) {
    return;
}

The name of variables in codes have to be the same with the declaration in url. Therefore, if users are willing to create a query argument, who doesn't have a snake_case name, Rust will generate a warning.

Ideal Solution

Extended query arguments declaration to things like <anApple:an_apple>, <anApple..:an_apple>. Any other proper syntax is OK.

Why can't this be implemented outside of Rocket?

Users have to use Route::from and maually implement Handler with complex codes.

Are there workarounds usable today?

#[allow(non_snake_case)], or

#[derive(FromForm)]
struct Form {
    #[field(name = "anApple")]
    apple: String,
}

#[get("/foo?<form...>")]
fn foo(form: Form) {
    // form.apple
    return;
}

Alternative Solutions

  1. Providing hints in compile time to guide users to #[allow(non_snake_case)].
  2. Automatically add #[allow(non_snake_case)] to compilers.

Additional Context

No response

System Checks

  • [x] I do not believe that this feature can or should be implemented outside of Rocket.
  • [x] I was unable to find a previous request for this feature.

burningtnt avatar Aug 18 '25 02:08 burningtnt

You can already do this (for query arguements) by using a struct:

#[derive(FromForm)]
struct Form {
    #[field(name = "anApple")]
    apple: String,
}

#[get("/foo?<form...>")]
fn foo(form: String) {
    // form.apple
    return;
}

Does this suffice to close the issue?

SergioBenitez avatar Aug 31 '25 16:08 SergioBenitez

You can already do this (for query arguements) by using a struct: Does this suffice to close the issue?

Thanks, it is a workaround usable today. However, this solution involves many 'template codes', while directly providing an alias of query arguments would be clearer.

burningtnt avatar Sep 01 '25 11:09 burningtnt

That's true! Is the syntax you propose inspired by some other framework?

SergioBenitez avatar Sep 01 '25 12:09 SergioBenitez

That's true! Is the syntax you propose inspired by some other framework?

No. It's just a random idea. (May be a bad one, as it's unclear that the phrase after ':' is an alias) Any better syntax is OK.

burningtnt avatar Sep 01 '25 12:09 burningtnt

One alternative would be to not have any syntax at all, but just convert all query parameters to snake_case automatically. Then something like the following would just work:

#[get("/?<anApple>")]
fn foo(an_apple: &str) {}

We could also support matching against either name (e.g., when looking for anApple, we would actually check for anApple and an_apple, and it's a compilation error to have both).

the10thWiz avatar Sep 06 '25 23:09 the10thWiz