Skip to content

Commit

Permalink
✨ Added anyhow::Result to further improve error handling capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Jul 16, 2024
1 parent ab62785 commit b0636da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
17 changes: 10 additions & 7 deletions packages/macro-types/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(non_snake_case)]

use anyhow::Result;

pub trait DeclRoutes: ::yew_router::Routable {
fn switch(routes: &Self) -> ::yew::Html;
}
Expand Down Expand Up @@ -40,7 +42,8 @@ where
type ClientApp;
type ServerApp;

async fn render_to_string(url: String, states: <Self as DeclType>::AppStates) -> String;
async fn render_to_string(url: String, states: <Self as DeclType>::AppStates)
-> Result<String>;

fn render_with_root(
root: web_sys::Element,
Expand All @@ -60,22 +63,22 @@ where
type Routes;
type AppStates;

fn decl_render_outside(props: &RoutesOutsideProps<Self::AppStates>) -> ::yew::Html {
::yew::html! {
fn decl_render_outside(props: &RoutesOutsideProps<Self::AppStates>) -> ::yew::HtmlResult {
Ok(::yew::html! {
<>
{props.children.clone()}
</>
}
})
}

fn render_to_string_outside(
style_raw: String,
html_raw: String,
state: Self::AppStates,
) -> String {
) -> Result<String> {
let state = ::serde_json::to_string(&state).unwrap();

format!("
Ok(format!("
<!DOCTYPE html>
<html>
<head>
Expand All @@ -90,6 +93,6 @@ where
<script>(async () => {{await wasm_vendor_entry('/a.wasm');(await (new wasm_vendor_entry.WebHandle())).start();}})()</script>
</body>
</html>
")
"))
}
}
21 changes: 12 additions & 9 deletions packages/macro/src/utils/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ pub fn root(input: DeriveApp) -> TokenStream {


#[::yew::function_component]
fn HikariContextShell(props: &HikariContextShellProps) -> yew::Html {
fn HikariContextShell(props: &HikariContextShellProps) -> yew::HtmlResult {
use yew::prelude::*;

let ctx = use_state(|| props.states.clone());

html! {
Ok(html! {
<ContextProvider
<UseStateHandle<<#ident as ::hikari_boot::DeclType>::AppStates>>
context={ctx.clone()}
Expand All @@ -112,10 +112,10 @@ pub fn root(input: DeriveApp) -> TokenStream {
},
states: props.states.clone(),
},
)
).unwrap()
}
</ContextProvider<UseStateHandle<<#ident as ::hikari_boot::DeclType>::AppStates>>>
}
})
}

#[::yew::function_component]
Expand All @@ -139,17 +139,20 @@ pub fn root(input: DeriveApp) -> TokenStream {
type ClientApp = HikariClientApp;
type ServerApp = HikariServerApp;

async fn render_to_string(url: String, states: <#ident as ::hikari_boot::DeclType>::AppStates) -> String {
async fn render_to_string(url: String, states: <#ident as ::hikari_boot::DeclType>::AppStates) -> ::anyhow::Result<String> {
use ::stylist::manager::{render_static, StyleManager};
use ::yew::ServerRenderer;

let url = url.split('?').next().unwrap().to_string();
let url = url.split('?').next().ok_or(::anyhow::anyhow!("failed to get url path from '{}'", url))?.to_string();
let (writer, reader) = render_static();

let renderer = ServerRenderer::<<#ident as ::hikari_boot::Application>::ServerApp>::with_props({
let states = states.clone();
move || {
let style_manager = StyleManager::builder().writer(writer).build().unwrap();
let style_manager = StyleManager::builder()
.writer(writer)
.build()
.expect("Failed to create style manager with writer.");
::hikari_boot::AppContextForServer {
style_manager,
url,
Expand All @@ -161,9 +164,9 @@ pub fn root(input: DeriveApp) -> TokenStream {

let style_data = reader.read_style_data();
let mut style_raw = String::new();
style_data.write_static_markup(&mut style_raw).unwrap();
style_data.write_static_markup(&mut style_raw)?;

<#ident as ::hikari_boot::DeclType>::render_to_string_outside(style_raw, html_raw, states)
Ok(<#ident as ::hikari_boot::DeclType>::render_to_string_outside(style_raw, html_raw, states)?)
}

fn render_with_root(
Expand Down

0 comments on commit b0636da

Please sign in to comment.