Skip to content

Commit

Permalink
Easily convert typed paths into URIs (#790)
Browse files Browse the repository at this point in the history
* 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 #789

* Use a method on the `TypedPath` trait to convert to `Uri`

* fix doc ref

* Update changelogs
  • Loading branch information
davidpdrsn committed Mar 1, 2022
1 parent 1e099a9 commit 7d9d8c9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion axum-extra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **added:** Add `TypedPath::to_uri` for converting the path into a `Uri` ([#790])

[#790]: https://github.com/tokio-rs/axum/pull/790

# 0.1.4 (22. February, 2022)

Expand Down
18 changes: 18 additions & 0 deletions axum-extra/src/routing/typed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::sealed::Sealed;
use http::Uri;

/// A type safe path.
///
Expand Down Expand Up @@ -87,6 +88,8 @@ use super::sealed::Sealed;
/// things, create links to known paths and have them verified statically. Note that the
/// [`Display`] implementation for each field must return something that's compatible with its
/// [`Deserialize`] implementation.
/// - A [`TryFrom<_> for Uri`](std::convert::TryFrom) implementation to converting your paths into
/// [`Uri`](axum::http::Uri).
///
/// Additionally the macro will verify the captures in the path matches the fields of the struct.
/// For example this fails to compile since the struct doesn't have a `team_id` field:
Expand Down Expand Up @@ -148,6 +151,21 @@ use super::sealed::Sealed;
pub trait TypedPath: std::fmt::Display {
/// The path with optional captures such as `/users/:id`.
const PATH: &'static str;

/// Convert the path into a `Uri`.
///
/// # Panics
///
/// The default implementation parses the required [`Display`] implemetation. If that fails it
/// will panic.
///
/// Using `#[derive(TypedPath)]` will never result in a panic since it percent-encodes
/// arguments.
///
/// [`Display`]: std::fmt::Display
fn to_uri(&self) -> Uri {
self.to_string().parse().unwrap()
}
}

/// Utility trait used with [`RouterExt`] to ensure the first element of a tuple type is a
Expand Down
2 changes: 1 addition & 1 deletion axum-macros/src/typed_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
23 changes: 23 additions & 0 deletions axum-macros/tests/typed_path/pass/into_uri.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 7d9d8c9

Please sign in to comment.