Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Procedural macro for routing #15

Open
mehcode opened this issue Sep 1, 2017 · 3 comments
Open

Procedural macro for routing #15

mehcode opened this issue Sep 1, 2017 · 3 comments

Comments

@mehcode
Copy link
Owner

mehcode commented Sep 1, 2017

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.

@Meralis40
Copy link
Contributor

Meralis40 commented Sep 7, 2017

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.

@mehcode
Copy link
Owner Author

mehcode commented Sep 8, 2017

@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::*.

@Meralis40
Copy link
Contributor

I've done the two last part, and open a PR (#19).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants