Specific argument names in route macros.
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
- Providing hints in compile time to guide users to
#[allow(non_snake_case)]. - 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.
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?
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.
That's true! Is the syntax you propose inspired by some other framework?
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.
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).