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:
David Pedersen
2022-02-28 09:58:22 +01:00
committed by GitHub
parent 67c385cd2d
commit da74084146
5 changed files with 45 additions and 2 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
- Add `#[derive(TypedPath)]` for use with axum-extra's new "type safe" routing API ([#756])
- **added:** Add `#[derive(TypedPath)]` for use with axum-extra's new "type safe" routing API ([#756])
# 0.1.0 (31. January, 2022)
+1 -1
View File
@@ -29,7 +29,7 @@ pub(crate) fn expand(item_struct: ItemStruct) -> syn::Result<TokenStream> {
let segments = parse_path(&path)?;
expand_unnamed_fields(fields, ident, path, &segments)
}
syn::Fields::Unit => Ok(expand_unit_fields(ident, path)?),
syn::Fields::Unit => expand_unit_fields(ident, path),
}
}
@@ -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();
}