From 3682586513dfc9890883a075b044bb466567b8b3 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 11 Feb 2022 10:40:25 +0100 Subject: [PATCH] wip --- axum-macros/src/lib.rs | 6 +++--- axum-macros/src/{route.rs => uri.rs} | 14 +++++++------- examples/hello-world/src/main.rs | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) rename axum-macros/src/{route.rs => uri.rs} (93%) diff --git a/axum-macros/src/lib.rs b/axum-macros/src/lib.rs index a5204224..7e00d90d 100644 --- a/axum-macros/src/lib.rs +++ b/axum-macros/src/lib.rs @@ -49,7 +49,7 @@ use syn::parse::Parse; mod debug_handler; mod from_request; -mod route; +mod uri; /// Derive an implementation of [`FromRequest`]. /// @@ -387,8 +387,8 @@ pub fn debug_handler(_attr: TokenStream, input: TokenStream) -> TokenStream { } /// TODO -#[proc_macro_derive(Route, attributes(route))] -pub fn derive_route(input: TokenStream) -> TokenStream { +#[proc_macro_derive(Uri, attributes(uri))] +pub fn derive_uri(input: TokenStream) -> TokenStream { expand_with(input, route::expand) } diff --git a/axum-macros/src/route.rs b/axum-macros/src/uri.rs similarity index 93% rename from axum-macros/src/route.rs rename to axum-macros/src/uri.rs index ecdb2dba..3ddc8443 100644 --- a/axum-macros/src/route.rs +++ b/axum-macros/src/uri.rs @@ -47,7 +47,7 @@ fn parse_attrs(attrs: &[syn::Attribute]) -> syn::Result { let mut route = None::; for attr in attrs { - if attr.path.is_ident("route") { + if attr.path.is_ident("uri") { route = Some(attr.parse_args::()?.value()); } } @@ -70,8 +70,8 @@ fn route_method(route: &str, vis: &syn::Visibility, fields: &syn::Fields) -> Tok }); quote! { - #vis fn route(&self) -> String { - format!(#format_str, #(#set_placeholders,)*) + #vis fn path(&self) -> ::std::borrow::Cow<'static, str> { + format!(#format_str, #(#set_placeholders,)*).into() } } } @@ -85,14 +85,14 @@ fn route_method(route: &str, vis: &syn::Visibility, fields: &syn::Fields) -> Tok }); quote! { - #vis fn route(&self) -> String { - format!(#format_str, #(#set_placeholders,)*) + #vis fn path(&self) -> ::std::borrow::Cow<'static, str> { + format!(#format_str, #(#set_placeholders,)*).into() } } } syn::Fields::Unit => quote! { - #vis fn route(&self) -> &'static str { - #route + #vis fn uri(&self) -> ::std::borrow::Cow<'static, str> { + #route.into() } }, } diff --git a/examples/hello-world/src/main.rs b/examples/hello-world/src/main.rs index 571c1226..bc464da9 100644 --- a/examples/hello-world/src/main.rs +++ b/examples/hello-world/src/main.rs @@ -7,7 +7,7 @@ #![allow(dead_code)] use axum::{response::Html, routing::get, Router}; -use axum_macros::Route; +use axum_macros::Uri; use serde::Deserialize; use std::net::SocketAddr; @@ -29,12 +29,12 @@ async fn handler(_: UsersShow) -> Html<&'static str> { Html("

Hello, World!

") } -// #[derive(Deserialize, Route)] -// #[route("/users")] -// struct UsersIndex; +#[derive(Deserialize, Uri)] +#[uri("/users")] +struct UsersIndex; -#[derive(Deserialize, Route)] -#[route("/users/:id/teams/:team_id")] +#[derive(Deserialize, Uri)] +#[uri("/users/:id/teams/:team_id")] struct UsersShow { id: u32, team_id: u32,