Feat: move the rsx macro around

This commit is contained in:
Jonathan Kelley committed 2021-06-03 10:42:28 -04:00
1 parent 1919f88f03
commit 50c8b93aad
23 files changed
+886 -617

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
use super::*;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{
parse::{Parse, ParseStream},
token, Expr, LitStr, Result,
};
// ==============================================
// Parse any div {} as a VElement
// ==============================================
pub enum Node {
Element(AmbiguousElement),
Text(TextNode),
RawExpr(Expr),
}
impl ToTokens for Node {
fn to_tokens(&self, tokens: &mut TokenStream2) {
match &self {
Node::Element(el) => el.to_tokens(tokens),
Node::Text(txt) => txt.to_tokens(tokens),
Node::RawExpr(exp) => exp.to_tokens(tokens),
}
}
}
impl Parse for Node {
fn parse(stream: ParseStream) -> Result<Self> {
// Supposedly this approach is discouraged due to inability to return proper errors
// TODO: Rework this to provide more informative errors
if stream.peek(token::Brace) {
let content;
syn::braced!(content in stream);
return Ok(Node::RawExpr(content.parse::<Expr>()?));
}
if stream.peek(LitStr) {
return Ok(Node::Text(stream.parse::<TextNode>()?));
}
Ok(Node::Element(stream.parse::<AmbiguousElement>()?))
}
}
// =======================================
// Parse just plain text
// =======================================
pub struct TextNode(LitStr);
impl Parse for TextNode {
fn parse(s: ParseStream) -> Result<Self> {
Ok(Self(s.parse()?))
}
}
impl ToTokens for TextNode {
fn to_tokens(&self, tokens: &mut TokenStream2) {
// todo: use heuristics to see if we can promote to &static str
let token_stream = &self.0.to_token_stream();
tokens.append_all(quote! {
{
// use bumpalo::core_alloc::fmt::Write;
// let mut s = bumpalo::collections::String::new_in(bump);
// s.write_fmt(format_args_f!(#token_stream)).unwrap();
dioxus::builder::text3(bump, format_args_f!(#token_stream))
// dioxus::builder::text2(s)
}
});
}
}