Skip to content

Commit

Permalink
✨ Routes can use props now.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Dec 20, 2023
1 parent 913a85b commit d757945
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
19 changes: 18 additions & 1 deletion packages/boot/tests/register_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
<div>{format!("Test {}", props.id)}</div>
}
}

#[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)]
Expand All @@ -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(),
},
Expand Down
42 changes: 30 additions & 12 deletions packages/macro/src/utils/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const COMPONENT_ATTR_IDENT: &str = "component";

pub struct DeriveRoutes {
ident: Ident,
components: HashMap<Ident, Path>,
components: HashMap<Ident, (Vec<Ident>, Path)>,
}

impl Parse for DeriveRoutes {
Expand Down Expand Up @@ -42,8 +42,8 @@ impl Parse for DeriveRoutes {

fn parse_variants_attributes(
variants: &Punctuated<Variant, syn::token::Comma>,
) -> syn::Result<HashMap<Ident, Path>> {
let mut components: HashMap<Ident, Path> = Default::default();
) -> syn::Result<HashMap<Ident, (Vec<Ident>, Path)>> {
let mut components: HashMap<Ident, (Vec<Ident>, Path)> = Default::default();

for variant in variants.iter() {
if let Fields::Unnamed(ref field) = variant.fields {
Expand All @@ -53,6 +53,12 @@ fn parse_variants_attributes(
));
}

let args = variant
.fields
.iter()
.map(|field| field.ident.clone().unwrap())
.collect::<Vec<_>>();

let attrs = &variant.attrs;
let at_attrs = attrs
.iter()
Expand All @@ -74,9 +80,9 @@ fn parse_variants_attributes(
))
}
};
let lit = attr.parse_args::<Path>()?;
let path = attr.parse_args::<Path>()?;

components.insert(variant.ident.clone(), lit);
components.insert(variant.ident.clone(), (args, path));
}

Ok(components)
Expand All @@ -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::<Vec<_>>();

quote! {
impl ::hikari_boot::DeclRoutes for #ident {
#[allow(bindings_with_variant_name)]
fn switch(route: &Self) -> ::yew::Html {
match route {
#(#ats => #components,)*
#(#components,)*
}
}
}
Expand Down

0 comments on commit d757945

Please sign in to comment.