Replies: 4 comments 4 replies
-
This is not entirely correct. Serde doesn't neccessarily need to be removed. I do see some value in being able to use non-standard types like route.get(path!("/foo/{date: chrono::NaiveDate}")).to(|path| unimplemented!()) and have the What I previously experimented with was a macro that applies (possibly in addition to serde) to the struct that parses the path at runtime, and have that struct provide the template string. However, I didn't really like the solution.
That sounds very useful. Do you have a concrete idea in mind how that api could look? Ideally, we'd be able to at compile time generate such a route-lookup-struct that knows all routes and their path parameters, but again I don't think such a macro is currently possible. This feature also would be a good step towards generating OpenAPI/Swagger specifications, which has been requested before (and I'd love to have this feature so I can stop hacking custom macros on top of gotham).
Given that rust also uses |
Beta Was this translation helpful? Give feedback.
-
I come to Gotham by way of Rails, so that's my inclination. I think something like
or something in that vein would be handy. A helper for full URLs might be worthwhile, but that tends to be fraught. Do we use a "canonical" domain? The domain from request URL? |
Beta Was this translation helpful? Give feedback.
-
I see what you're saying here. I appreciate the convenience, but even if that macro could be figured out, it seems like there's drawbacks with regards to the automatically generated struct, but I think they boil down to: how do I refer to the type? In terms of design, I think there's value to investigating the idioms we'd like to see. A lot of APIs reuse fields (i.e. :user_id), and as I understand the routing API now, it's possible to
right?
In principle, Serde might allow us to validate a 1:1 relationship between parameters in that path and fields in the struct. It will certainly fail to serialize (at request-time, unfortunately) if the path lacks parameters that match the extractors, though. Again, multiple extractors complicates this. It would certainly be ideal if we could validate that all fields of the path get extracted at compile time. I don't have a clear idea how to accomplish that though. |
Beta Was this translation helpful? Give feedback.
-
I just saw #145 suggesting to keep the template string separate but validate against the struct at compile time, possibly using a custom proc macro. This is what I came up after thinking about it for a few minutes: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=93fcee7935cc1e638fac2155fdad8f78 It won't be easy to sanely implement this, since serde allows renaming structs, making certain fields optional etc., which will mess with these compile time assertions. Maybe we could reduce these issues and the resulting complexity if we didn't use |
Beta Was this translation helpful? Give feedback.
-
This is perhaps a very large topic, but I'm pulling it from a number of chats-in-passing. Most recently, I noticed #496 touching on it.
Generally speaking (and going back to basic design here), we want to group up many resources under path matchers to be processed by a single Handler, and with the varying parts delivered in a well typed parameter. @msrd0 was proposing that matching be handled by a Gotham derive rather than a Serde Deserialization.
On the one hand, I'd like to be convinced of the value there. Serde's a fine piece of engineering, and we do well leveraging it for parameter (and query) deserialization.
On the other, if we're contemplating changing the behaviors of routing, I'd like to revive one of the features I'd like to see: bidirectional routing. HTTP is driven by links, and having responses reach up and be coupled to the route formats by rendering explicit links is a pain. I'd like for routes to be named, for the routemap to be put into state, and for handlers to be able to call a method on the routemap with appropriate parameters provided to render a link that matches to the appropriate named route.
And, so long as I'm wishing, I wish that Gotham routes looked like (URL templates)[https://tools.ietf.org/html/rfc6570].
Maybe phasing in a new pattern for routes could bring all three features.
Beta Was this translation helpful? Give feedback.
All reactions