shio-rs
shio-rs copied to clipboard
Procedural macro for routing
An (untested) idea for implementation:
#[get("/user/profile")]
pub fn user_get_profile(_: Context) -> Response { ... }
// Becomes
fn __user_get_profile(_: Context) -> Response { ... }
pub static user_get_profile: ::shio::router::Route =
(::shio::Method::Get, "/user/profile", __user_get_profile).into();
Marking this as help wanted as I'll accept a PR (you'll need to make a new crate in the workspace like shio_macros) but its not a priority for me. It'll also need to be behind the nightly feature flag.
I'll give it a try.
Edit: I've implemented it for GET and POST (attributes get and post), the code is here : https://github.com/Meralis40/shio-rs/tree/shio-macro
I've also make an example derived from hello, hello_macro.
@Meralis40 From a quick look over that looks awesome! Go ahead and open a pull request when you're ready and we can discuss more there.
Some quick notes:
- I'd like multi-mounting to be possible. Unsure how to support that easily. Ideally it'd result in an array of routes. We could extend
.route( ... )to also accept an array of routes. This can be done later. It's more of a nice-to-have.
#[get("/")]
#[get("/path")]
fn index_or_path(_: Context) -> Response { }
- It'd be nice to support a more generic
#[route]decorator too. Not sure if the syntax is best. This can also be done later. It's also just a nice-to-have.
// Idea 1
#[route(GET, path = "/")]
// Idea 2
#[route("/")] // Defaults to GET
#[route("/", methods = [GET, POST])]
shio_macroswould be the crate name (as it definitely has more than 1 macro) andmacros/would be the folder name- Re-export the macros from
shiounder the nightly feature. Procedural macros can be re-exported like normal itemspub use shio_macros::*.
I've done the two last part, and open a PR (https://github.com/mehcode/shio-rs/pull/19).