mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-27 00:00:24 +02:00
Easily convert typed paths into URIs (#790)
* Easily convert typed paths into URIs `#[derive(TypedPath)]` will now also generate `TryFrom<_> for Uri` for easily converting paths into URIs for use with `Redirect` and friends. Fixes https://github.com/tokio-rs/axum/issues/789 * Use a method on the `TypedPath` trait to convert to `Uri` * fix doc ref * Update changelogs
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
use axum_extra::routing::TypedPath;
|
||||
use axum::http::Uri;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(TypedPath, Deserialize)]
|
||||
#[typed_path("/:id")]
|
||||
struct Named {
|
||||
id: u32,
|
||||
}
|
||||
|
||||
#[derive(TypedPath, Deserialize)]
|
||||
#[typed_path("/:id")]
|
||||
struct Unnamed(u32);
|
||||
|
||||
#[derive(TypedPath, Deserialize)]
|
||||
#[typed_path("/")]
|
||||
struct Unit;
|
||||
|
||||
fn main() {
|
||||
let _: Uri = Named { id: 1 }.to_uri();
|
||||
let _: Uri = Unnamed(1).to_uri();
|
||||
let _: Uri = Unit.to_uri();
|
||||
}
|
||||
Reference in New Issue
Block a user