From d7579457088ec8bfc042978ebf3b62784d111ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Wed, 20 Dec 2023 12:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Routes=20can=20use=20props=20now.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/boot/tests/register_routes.rs | 19 +++++++++++- packages/macro/src/utils/routes.rs | 42 ++++++++++++++++++-------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/packages/boot/tests/register_routes.rs b/packages/boot/tests/register_routes.rs index 4dc6309..e8018ff 100644 --- a/packages/boot/tests/register_routes.rs +++ b/packages/boot/tests/register_routes.rs @@ -17,11 +17,28 @@ mod test { } } + #[derive(yew::Properties, Clone, PartialEq, Debug)] + pub struct TestProps { + pub id: String, + } + + #[function_component] + fn Test(props: &TestProps) -> yew::Html { + html! { +
{format!("Test {}", props.id)}
+ } + } + #[derive(PartialEq, Clone, Debug, DeriveRoutes, Routable)] pub enum Routes { #[at("/")] + #[not_found] #[component(Portal)] Portal, + + #[at("/test/:id")] + #[component(Test)] + Test { id: String }, } #[derive(PartialEq, Clone, Debug, Serialize, Deserialize)] @@ -40,7 +57,7 @@ mod test { #[tokio::test] async fn render_on_server() -> anyhow::Result<()> { let html = App::render_to_string( - "/".to_string(), + "/test/123".to_string(), AppStates { color: "#114514".to_string(), }, diff --git a/packages/macro/src/utils/routes.rs b/packages/macro/src/utils/routes.rs index 532ce8f..d1012a0 100644 --- a/packages/macro/src/utils/routes.rs +++ b/packages/macro/src/utils/routes.rs @@ -11,7 +11,7 @@ const COMPONENT_ATTR_IDENT: &str = "component"; pub struct DeriveRoutes { ident: Ident, - components: HashMap, + components: HashMap, Path)>, } impl Parse for DeriveRoutes { @@ -42,8 +42,8 @@ impl Parse for DeriveRoutes { fn parse_variants_attributes( variants: &Punctuated, -) -> syn::Result> { - let mut components: HashMap = Default::default(); +) -> syn::Result, Path)>> { + let mut components: HashMap, Path)> = Default::default(); for variant in variants.iter() { if let Fields::Unnamed(ref field) = variant.fields { @@ -53,6 +53,12 @@ fn parse_variants_attributes( )); } + let args = variant + .fields + .iter() + .map(|field| field.ident.clone().unwrap()) + .collect::>(); + let attrs = &variant.attrs; let at_attrs = attrs .iter() @@ -74,9 +80,9 @@ fn parse_variants_attributes( )) } }; - let lit = attr.parse_args::()?; + let path = attr.parse_args::()?; - components.insert(variant.ident.clone(), lit); + components.insert(variant.ident.clone(), (args, path)); } Ok(components) @@ -86,21 +92,33 @@ pub fn root(input: DeriveRoutes) -> TokenStream { let DeriveRoutes { components, ident, .. } = &input; - let (ats, components): (Vec<_>, Vec<_>) = components + let components = components .iter() - .map(|(ident, path)| { - let ats = quote! { #ident }; - let components = quote! { ::yew::html! { <#path /> } }; - (ats, components) + .map(|(key, (fields, path))| { + if fields.is_empty() { + quote! { + #ident::#key => ::yew::html! { + <#path /> + } + } + } else { + quote! { + #ident::#key { #(#fields),* } => ::yew::html! { + <#path + #(#fields={#fields.clone()}),* + /> + } + } + } }) - .unzip(); + .collect::>(); quote! { impl ::hikari_boot::DeclRoutes for #ident { #[allow(bindings_with_variant_name)] fn switch(route: &Self) -> ::yew::Html { match route { - #(#ats => #components,)* + #(#components,)* } } }