From 913a85be482e232e3fb02b0d96439ce961160fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Tue, 19 Dec 2023 20:06:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20the=20macro.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/boot/tests/register_routes.rs | 10 +++++-- packages/macro-types/src/register.rs | 14 +++++----- packages/macro/src/utils/app.rs | 38 ++++++++++++-------------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/packages/boot/tests/register_routes.rs b/packages/boot/tests/register_routes.rs index 0924e65..4dc6309 100644 --- a/packages/boot/tests/register_routes.rs +++ b/packages/boot/tests/register_routes.rs @@ -4,7 +4,7 @@ #[cfg(test)] mod test { use serde::{Deserialize, Serialize}; - use std::str::FromStr; + use yew::prelude::*; use yew_router::prelude::*; @@ -39,7 +39,13 @@ mod test { #[tokio::test] async fn render_on_server() -> anyhow::Result<()> { - let html = App::render_to_string(url::Url::from_str("/")?).await; + let html = App::render_to_string( + "/".to_string(), + AppStates { + color: "#114514".to_string(), + }, + ) + .await; println!("{}", html); Ok(()) diff --git a/packages/macro-types/src/register.rs b/packages/macro-types/src/register.rs index 143187b..cf17aba 100644 --- a/packages/macro-types/src/register.rs +++ b/packages/macro-types/src/register.rs @@ -1,27 +1,27 @@ #![allow(non_snake_case)] -pub trait DeclRoutes: yew_router::Routable { - fn switch(routes: &Self) -> yew::Html; +pub trait DeclRoutes: ::yew_router::Routable { + fn switch(routes: &Self) -> ::yew::Html; } -#[derive(Debug, PartialEq, Clone, yew::Properties)] +#[derive(Debug, PartialEq, Clone, ::yew::Properties)] pub struct AppContext where T: PartialEq + Clone + ::serde::Serialize + ::serde::Deserialize<'static>, { - pub style_manager: stylist::manager::StyleManager, - pub url: url::Url, + pub style_manager: ::stylist::manager::StyleManager, + pub url: String, pub states: T, } #[async_trait::async_trait] pub trait Application: DeclType { - async fn render_to_string(url: url::Url) -> String; + async fn render_to_string(url: String, status: ::AppStates) -> String; } pub trait DeclType where - Self::Routes: DeclRoutes + yew_router::Routable, + Self::Routes: DeclRoutes + ::yew_router::Routable, Self::AppStates: PartialEq + Clone + ::serde::Serialize + ::serde::Deserialize<'static>, { type Routes; diff --git a/packages/macro/src/utils/app.rs b/packages/macro/src/utils/app.rs index 1dab4dd..7ec6511 100644 --- a/packages/macro/src/utils/app.rs +++ b/packages/macro/src/utils/app.rs @@ -37,7 +37,7 @@ pub fn root(input: DeriveApp) -> TokenStream { quote! { #[::yew::function_component] - pub fn __HikariApp() -> yew::Html { + pub fn HikariApp() -> yew::Html { use ::stylist::{manager::StyleManager, yew::ManagerProvider}; use ::yew::prelude::*; use ::yew_router::BrowserRouter; @@ -52,7 +52,7 @@ pub fn root(input: DeriveApp) -> TokenStream { .expect("Cannot get the global window object") .document() .expect("Cannot get the global document object") - .get_element_by_id("__ssr_data") + .get_element_by_id("ssr_data") .expect("Cannot get the root DOM element"); let page_data = page_data_el.inner_html(); let page_data = { @@ -73,7 +73,7 @@ pub fn root(input: DeriveApp) -> TokenStream { - <__HikariContextShell states={page_data} /> + @@ -81,7 +81,7 @@ pub fn root(input: DeriveApp) -> TokenStream { } #[::yew::function_component] - pub fn __HikariServerApp( + pub fn HikariServerApp( props: &::hikari_boot::AppContext<<#ident as ::hikari_boot::DeclType>::AppStates> ) -> yew::Html { use ::stylist::yew::ManagerProvider; @@ -93,15 +93,13 @@ pub fn root(input: DeriveApp) -> TokenStream { let fallback = html! {
{"Loading..."}
}; let history = AnyHistory::from(MemoryHistory::new()); - history - .push(&*props.uri) - .unwrap(); + history.push(&props.url); html! { - <__HikariContextShell states={props.states.to_owned()} /> + @@ -109,13 +107,13 @@ pub fn root(input: DeriveApp) -> TokenStream { } #[derive(Clone, Debug, PartialEq, ::yew::Properties)] - struct __HikariContextShellProps { + struct HikariContextShellProps { states: <#ident as ::hikari_boot::DeclType>::AppStates, } #[::yew::function_component] - fn __HikariContextShell(states: &__HikariContextShellProps) -> yew::Html { + fn HikariContextShell(states: &HikariContextShellProps) -> yew::Html { use yew::prelude::*; type AppStates = <#ident as ::hikari_boot::DeclType>::AppStates; @@ -124,23 +122,20 @@ pub fn root(input: DeriveApp) -> TokenStream { html! { context={ctx.clone()}> - <__HikariContent /> + > } } #[::stylist::yew::styled_component] - pub fn __HikariContent() -> yew::Html { + pub fn HikariContent() -> yew::Html { use yew::prelude::*; use yew_router::prelude::*; html! { <> ::Routes> - render={< - <#ident as ::hikari_boot::DeclType>::Routes as - ::hikari_boot::DeclRoutes>::switch - } + render={|r| <<#ident as ::hikari_boot::DeclType>::Routes as ::hikari_boot::DeclRoutes>::switch(&r)} /> } @@ -149,16 +144,17 @@ pub fn root(input: DeriveApp) -> TokenStream { #[automatically_derived] #[::async_trait::async_trait] impl ::hikari_boot::Application for #ident { - async fn render_to_string(url: ::url::Url) -> String { + async fn render_to_string(url: String, states: <#ident as ::hikari_boot::DeclType>::AppStates) -> String { use ::stylist::manager::{render_static, StyleManager}; use ::yew::ServerRenderer; let (writer, reader) = render_static(); - let renderer = ServerRenderer::<__HikariServerApp>::with_props(move || { + let renderer = ServerRenderer::::with_props(move || { let style_manager = StyleManager::builder().writer(writer).build().unwrap(); - ::AppStates { + ::hikari_boot::AppContext { style_manager, url, + states, } }); let html_raw = renderer.render().await; @@ -176,10 +172,10 @@ pub fn root(input: DeriveApp) -> TokenStream { - +
{html_raw}
- + ")