For URLs paths like:
/api/2023-11-01T16-52-00Z/FOO_BAR_BAZ/123_456
I would like to use:
let app = Router::new()
.route("/api/:start/:tokens/:coords", get(stats))
...
async fn stats(
Path((start, tokens, coords)): Path<(DateTime<Utc>, Vec<String>, (u32, u32))>,
) {
todo!()
}
but I get:
error[E0277]: the trait bound `fn(axum::extract::Path<(DateTime<Utc>, Vec<String>, (u32, u32))>) -> impl Future<Output = ()> {stats}: Handler<_, _>` is not satisfied
--> src/main.rs:22:51
|
22 | .route("/api/:start/:tokens/:coords", get(stats))
| --- ^^^^^ the trait `Handler<_, _>` is not implemented for fn item `fn(axum::extract::Path<(DateTime<Utc>, Vec<String>, (u32, u32))>) -> impl Future<Output = ()> {stats}`
| |
| required by a bound introduced by this call
|
= help: the trait `Handler<T, ReqBody>` is implemented for `Layered<S, T>`
The documentation reads:
Struct axum::extract::Path source ยท
pub struct Path(pub T);
Extractor that will get captures from the URL and parse them using serde.
What serde serialisation does it use? (Serde supports many serialisations.)
How can I get it to parse (URL-safe) ISO timestamps and underscore-delimited lists?
I expect to write parsing functions for these types, but how do I hook them up?
(I could parse all the parameters as String
s and process them inside the handler function, but I'm hoping that there's a better way.)
You have several questions, so let's address them one at a time.
Getting the code to build
The following builds for me
I have only added the
axum::Server::bind(...)
. I suspect that Rust had difficulties inferring your code. In the future, I suggest using#[axum::debug_handler]
, which improves such error messages.Deserialization format
Well, data structures compatible with Serde implement
Deserialize
, so let's look at the definition ofDeserialize
forDateTime
:also, if you do not like this format, additional formats are provided here. Ping me if you need help, I'll try and give you a hand.
Now, it's a bit different for our
Vec
. I actually have no clue what the default format is forVec
, but I'm willing to be that it's not_
-separated. So let's implement_
-separation.Does this solve your issues?