shio-rs icon indicating copy to clipboard operation
shio-rs copied to clipboard

Procedural macro for routing

Open mehcode opened this issue 8 years ago • 3 comments

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.

mehcode avatar Sep 01 '17 03:09 mehcode

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 avatar Sep 07 '17 07:09 Meralis40

@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_macros would be the crate name (as it definitely has more than 1 macro) and macros/ would be the folder name
  • Re-export the macros from shio under the nightly feature. Procedural macros can be re-exported like normal items pub use shio_macros::*.

mehcode avatar Sep 08 '17 07:09 mehcode

I've done the two last part, and open a PR (https://github.com/mehcode/shio-rs/pull/19).

Meralis40 avatar Sep 08 '17 07:09 Meralis40