fix: errors when using render macro

This commit is contained in:
Jonathan Kelley 2022-12-23 17:47:57 -05:00
parent fd8015e943
commit e7e21fa2f0
3 changed files with 26 additions and 25 deletions

View file

@ -11,9 +11,9 @@ fn main() {
fn app(cx: Scope) -> Element {
let mut count = use_state(cx, || 0);
cx.render(rsx! {
render! {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
})
}
}

View file

@ -1,5 +1,6 @@
use proc_macro::TokenStream;
use quote::ToTokens;
use rsx::RenderCallBody;
use syn::parse_macro_input;
mod inlineprops;
@ -41,10 +42,7 @@ pub fn rsx(s: TokenStream) -> TokenStream {
pub fn render(s: TokenStream) -> TokenStream {
match syn::parse::<rsx::CallBody>(s) {
Err(err) => err.to_compile_error().into(),
Ok(mut body) => {
body.inline_cx = true;
body.into_token_stream().into()
}
Ok(body) => RenderCallBody(body).into_token_stream().into(),
}
}

View file

@ -37,9 +37,6 @@ use syn::{
#[derive(Default, Debug)]
pub struct CallBody {
pub roots: Vec<BodyNode>,
// set this after
pub inline_cx: bool,
}
impl Parse for CallBody {
@ -56,10 +53,7 @@ impl Parse for CallBody {
roots.push(node);
}
Ok(Self {
roots,
inline_cx: false,
})
Ok(Self { roots })
}
}
@ -68,20 +62,29 @@ impl ToTokens for CallBody {
fn to_tokens(&self, out_tokens: &mut TokenStream2) {
let body = TemplateRenderer { roots: &self.roots };
if self.inline_cx {
out_tokens.append_all(quote! {
Some({
let __cx = cx;
#body
})
out_tokens.append_all(quote! {
::dioxus::core::LazyNodes::new( move | __cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode {
#body
})
} else {
out_tokens.append_all(quote! {
::dioxus::core::LazyNodes::new( move | __cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode {
#body
})
})
}
}
#[derive(Default, Debug)]
pub struct RenderCallBody(pub CallBody);
impl ToTokens for RenderCallBody {
fn to_tokens(&self, out_tokens: &mut TokenStream2) {
let body = TemplateRenderer {
roots: &self.0.roots,
};
out_tokens.append_all(quote! {
Some({
let __cx = cx;
#body
})
}
})
}
}