Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easily convert typed paths into URIs #790

Merged
merged 5 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions axum-extra/src/routing/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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
6 changes: 5 additions & 1 deletion axum-macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ 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])
- **added:** `#[derive(TypedPath)]` now also generates a `TryFrom<_> for Uri`
implementation ([#790])

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

# 0.1.0 (31. January, 2022)

Expand Down
22 changes: 17 additions & 5 deletions axum-macros/src/typed_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@ pub(crate) fn expand(item_struct: ItemStruct) -> syn::Result<TokenStream> {

let Attrs { path } = parse_attrs(attrs)?;

match fields {
let code = match fields {
syn::Fields::Named(_) => {
let segments = parse_path(&path)?;
Ok(expand_named_fields(ident, path, &segments))
expand_named_fields(ident, path, &segments)
}
syn::Fields::Unnamed(fields) => {
let segments = parse_path(&path)?;
expand_unnamed_fields(fields, ident, path, &segments)
expand_unnamed_fields(fields, ident, path, &segments)?
}
syn::Fields::Unit => Ok(expand_unit_fields(ident, path)?),
}
syn::Fields::Unit => expand_unit_fields(ident, path)?,
};

Ok(quote! {
#code

impl ::std::convert::TryFrom<#ident> for ::axum::http::Uri {
type Error = ::axum::http::uri::InvalidUri;

fn try_from(value: #ident) -> ::std::result::Result<Self, Self::Error> {
value.to_string().parse()
}
}
})
}

struct Attrs {
Expand Down
24 changes: 24 additions & 0 deletions axum-macros/tests/typed_path/pass/try_into_uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use axum_extra::routing::TypedPath;
use axum::http::Uri;
use serde::Deserialize;
use std::convert::TryInto;

#[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 }.try_into().unwrap();
let _: Uri = Unnamed(1).try_into().unwrap();
let _: Uri = Unit.try_into().unwrap();
}