From 3ddefb4d846b08343a87af58f7f1dfa40fef15a3 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 14 Feb 2022 14:35:38 +0100 Subject: [PATCH] Break things up a bit --- axum-macros/src/typed_path.rs | 38 +++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/axum-macros/src/typed_path.rs b/axum-macros/src/typed_path.rs index ef6bd999..e14d4d32 100644 --- a/axum-macros/src/typed_path.rs +++ b/axum-macros/src/typed_path.rs @@ -1,5 +1,5 @@ use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote_spanned}; +use quote::{format_ident, quote, quote_spanned}; use syn::{ItemStruct, LitStr}; pub(crate) fn expand(item_struct: ItemStruct) -> syn::Result { @@ -67,12 +67,14 @@ fn expand_named_fields(ident: &syn::Ident, path: LitStr, segments: &[Segment]) - let format_str = format_str_from_path(segments); let captures = captures_from_path(segments); - quote_spanned! {path.span()=> + let typed_path_impl = quote_spanned! {path.span()=> #[automatically_derived] impl ::axum_extra::routing::TypedPath for #ident { const PATH: &'static str = #path; } + }; + let display_impl = quote_spanned! {path.span()=> #[automatically_derived] impl ::std::fmt::Display for #ident { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { @@ -80,7 +82,9 @@ fn expand_named_fields(ident: &syn::Ident, path: LitStr, segments: &[Segment]) - write!(f, #format_str, #(#captures = #captures,)*) } } + }; + let from_request_impl = quote_spanned! {path.span()=> #[::axum::async_trait] #[automatically_derived] impl ::axum::extract::FromRequest for #ident @@ -93,6 +97,12 @@ fn expand_named_fields(ident: &syn::Ident, path: LitStr, segments: &[Segment]) - ::axum::extract::Path::from_request(req).await.map(|path| path.0) } } + }; + + quote! { + #typed_path_impl + #display_impl + #from_request_impl } } @@ -142,12 +152,14 @@ fn expand_unnamed_fields( let format_str = format_str_from_path(segments); let captures = captures_from_path(segments); - Ok(quote_spanned! {path.span()=> + let typed_path_impl = quote_spanned! {path.span()=> #[automatically_derived] impl ::axum_extra::routing::TypedPath for #ident { const PATH: &'static str = #path; } + }; + let display_impl = quote_spanned! {path.span()=> #[automatically_derived] impl ::std::fmt::Display for #ident { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { @@ -155,7 +167,9 @@ fn expand_unnamed_fields( write!(f, #format_str, #(#captures = #captures,)*) } } + }; + let from_request_impl = quote! { #[::axum::async_trait] #[automatically_derived] impl ::axum::extract::FromRequest for #ident @@ -168,6 +182,12 @@ fn expand_unnamed_fields( ::axum::extract::Path::from_request(req).await.map(|path| path.0) } } + }; + + Ok(quote! { + #typed_path_impl + #display_impl + #from_request_impl }) } @@ -192,19 +212,23 @@ fn expand_unit_fields(ident: &syn::Ident, path: LitStr) -> syn::Result + let typed_path_impl = quote_spanned! {path.span()=> #[automatically_derived] impl ::axum_extra::routing::TypedPath for #ident { const PATH: &'static str = #path; } + }; + let display_impl = quote_spanned! {path.span()=> #[automatically_derived] impl ::std::fmt::Display for #ident { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { write!(f, #path) } } + }; + let from_request_impl = quote! { #[::axum::async_trait] #[automatically_derived] impl ::axum::extract::FromRequest for #ident @@ -221,6 +245,12 @@ fn expand_unit_fields(ident: &syn::Ident, path: LitStr) -> syn::Result