-
So I have URLs that accept a selector field that may or may not accept another argument. In this (contrived) example the second path element is the selector and the 3rd element (if present) is a value that only pertains to certain selectors:
And I have a note that there are basically two url forms: I would like to define a single route for "/person" that can handle either variant, passing the selector and optional value into I haven't figured out how to make that work. So I resorted to defining two routes that call two separate handler fn, and then each handler calls a common worker fn. This works perfectly, but seems rather inelegant, especially as the pattern is repeated a few times.
So I guess my question boils down to: is there a way to make the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
In general, I would recommend that you just have two handler function that delegate most of their work to the same private function that is never used as a handler directly. However, both of the things you are asking can be achieved to some extent:
|
Beta Was this translation helpful? Give feedback.
-
Just reporting back that I was able to combine these techniques and it is working perfectly with a single route and single handler for each page. This keeps both the routes and handlers simpler for anyone reading the code. thx again @jplatte for the suggestions. |
Beta Was this translation helpful? Give feedback.
In general, I would recommend that you just have two handler function that delegate most of their work to the same private function that is never used as a handler directly. However, both of the things you are asking can be achieved to some extent:
.route
once to register a handler for _anything starting with/person/
, you can do so with.route("/person/*rest", get(handler))
; there is no syntax for limiting the number of trailing segments thoughaxum::extract::Path<T>
, thenT
has to be a type that implementsDeserialize
such that both inputs work, for example you could usePath<Vec<String>>
for an arbi…