From 06390b9be59ee16296171371bb64b13192e3a572 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sat, 28 May 2022 12:10:32 -0500 Subject: [PATCH] allow rsx to be used outside of a proc_macro context --- packages/core-macro/Cargo.toml | 6 ++--- packages/core-macro/src/lib.rs | 1 - packages/rsx/Cargo.toml | 11 ++++++---- packages/rsx/src/component.rs | 4 +++- packages/rsx/src/element.rs | 40 ++++++++++------------------------ packages/rsx/src/errors.rs | 8 ++----- packages/rsx/src/ifmt.rs | 2 ++ packages/rsx/src/node.rs | 4 ++-- 8 files changed, 30 insertions(+), 46 deletions(-) diff --git a/packages/core-macro/Cargo.toml b/packages/core-macro/Cargo.toml index 968736d83..d72bafbcd 100644 --- a/packages/core-macro/Cargo.toml +++ b/packages/core-macro/Cargo.toml @@ -15,10 +15,9 @@ keywords = ["dom", "ui", "gui", "react", "wasm"] proc-macro = true [dependencies] -proc-macro-error = "1" -proc-macro2 = { version = "1.0.6" } +proc-macro2 = { version = "1.0" } quote = "1.0" -syn = { version = "1.0.11", features = ["full", "extra-traits"] } +syn = { version = "1.0", features = ["full", "extra-traits"] } dioxus-rsx = { path = "../rsx" } dioxus-rsx-interperter = { path = "../rsx_interperter", optional = true } @@ -28,4 +27,5 @@ rustversion = "1.0" trybuild = "1.0" [features] +default = [] hot_reload = ["dioxus-rsx-interperter"] \ No newline at end of file diff --git a/packages/core-macro/src/lib.rs b/packages/core-macro/src/lib.rs index 4adc6eb93..0c5503a4e 100644 --- a/packages/core-macro/src/lib.rs +++ b/packages/core-macro/src/lib.rs @@ -177,7 +177,6 @@ pub fn derive_typed_builder(input: proc_macro::TokenStream) -> proc_macro::Token /// todo!() /// } /// ``` -#[proc_macro_error::proc_macro_error] #[proc_macro] pub fn rsx(s: TokenStream) -> TokenStream { #[cfg(feature = "hot_reload")] diff --git a/packages/rsx/Cargo.toml b/packages/rsx/Cargo.toml index 5b492c9fe..aa47a2366 100644 --- a/packages/rsx/Cargo.toml +++ b/packages/rsx/Cargo.toml @@ -6,7 +6,10 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -proc-macro2 = { version = "1.0.6" } -proc-macro-error = "1" -quote = "1.0" -syn = { version = "1.0.11", features = ["full", "extra-traits"] } +proc-macro2 = { version = "1.0" } +syn = { version = "1.0", features = ["full", "extra-traits"] } +quote = { version = "1.0", optional = true } + +[features] +default = ["to_tokens"] +to_tokens = ["quote"] \ No newline at end of file diff --git a/packages/rsx/src/component.rs b/packages/rsx/src/component.rs index de37a052e..3fe690bb3 100644 --- a/packages/rsx/src/component.rs +++ b/packages/rsx/src/component.rs @@ -14,11 +14,12 @@ use super::*; use proc_macro2::TokenStream as TokenStream2; +#[cfg(feature = "to_tokens")] use quote::{quote, ToTokens, TokenStreamExt}; use syn::{ ext::IdentExt, parse::{Parse, ParseBuffer, ParseStream}, - token, Expr, Ident, LitStr, Result, Token, + token, Error, Expr, Ident, LitStr, Result, Token, }; #[derive(PartialEq, Eq)] @@ -72,6 +73,7 @@ impl Parse for Component { } } +#[cfg(feature = "to_tokens")] impl ToTokens for Component { fn to_tokens(&self, tokens: &mut TokenStream2) { let name = &self.name; diff --git a/packages/rsx/src/element.rs b/packages/rsx/src/element.rs index b74403b83..0036e7bff 100644 --- a/packages/rsx/src/element.rs +++ b/packages/rsx/src/element.rs @@ -1,10 +1,11 @@ use super::*; use proc_macro2::TokenStream as TokenStream2; +#[cfg(feature = "to_tokens")] use quote::{quote, ToTokens, TokenStreamExt}; use syn::{ parse::{Parse, ParseBuffer, ParseStream}, - Expr, Ident, LitStr, Result, Token, + Error, Expr, Ident, LitStr, Result, Token, }; // ======================================= @@ -65,7 +66,7 @@ impl Parse for Element { } if content.parse::().is_err() { - missing_trailing_comma!(ident); + missing_trailing_comma!(ident.span()); } continue; } @@ -123,7 +124,7 @@ impl Parse for Element { // todo: add a message saying you need to include commas between fields if content.parse::().is_err() { - missing_trailing_comma!(ident); + missing_trailing_comma!(ident.span()); } continue; } @@ -158,6 +159,7 @@ impl Parse for Element { } } +#[cfg(feature = "to_tokens")] impl ToTokens for Element { fn to_tokens(&self, tokens: &mut TokenStream2) { let name = &self.name; @@ -193,38 +195,21 @@ impl ToTokens for Element { #[derive(PartialEq, Eq)] pub enum ElementAttr { /// attribute: "valuee {}" - AttrText { - name: Ident, - value: LitStr, - }, + AttrText { name: Ident, value: LitStr }, /// attribute: true, - AttrExpression { - name: Ident, - value: Expr, - }, + AttrExpression { name: Ident, value: Expr }, /// "attribute": "value {}" - CustomAttrText { - name: LitStr, - value: LitStr, - }, + CustomAttrText { name: LitStr, value: LitStr }, /// "attribute": true, - CustomAttrExpression { - name: LitStr, - value: Expr, - }, + CustomAttrExpression { name: LitStr, value: Expr }, // /// onclick: move |_| {} // EventClosure { name: Ident, closure: ExprClosure }, /// onclick: {} - EventTokens { - name: Ident, - tokens: Expr, - }, - - Meta(String), + EventTokens { name: Ident, tokens: Expr }, } #[derive(PartialEq, Eq)] @@ -233,6 +218,7 @@ pub struct ElementAttrNamed { pub attr: ElementAttr, } +#[cfg(feature = "to_tokens")] impl ToTokens for ElementAttrNamed { fn to_tokens(&self, tokens: &mut TokenStream2) { let ElementAttrNamed { el_name, attr } = self; @@ -268,10 +254,6 @@ impl ToTokens for ElementAttrNamed { dioxus_elements::on::#name(__cx, #tokens) } } - - ElementAttr::Meta(_) => { - todo!("meta attributes not supported yet"); - } }); } } diff --git a/packages/rsx/src/errors.rs b/packages/rsx/src/errors.rs index 27e2c7f98..7467a657c 100644 --- a/packages/rsx/src/errors.rs +++ b/packages/rsx/src/errors.rs @@ -1,15 +1,11 @@ macro_rules! missing_trailing_comma { ($span:expr) => { - proc_macro_error::emit_error!($span, "missing trailing comma") + return Err(Error::new($span, "missing trailing comma")); }; } macro_rules! attr_after_element { ($span:expr) => { - proc_macro_error::emit_error!( - $span, - "expected element"; - help = "move the attribute above all the children and text elements" - ) + return Err(Error::new($span, "expected element\n = help move the attribute above all the children and text elements")); }; } diff --git a/packages/rsx/src/ifmt.rs b/packages/rsx/src/ifmt.rs index 819b40b95..7b7b12e40 100644 --- a/packages/rsx/src/ifmt.rs +++ b/packages/rsx/src/ifmt.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "to_tokens")] use ::quote::{quote, ToTokens}; use ::std::ops::Not; use ::syn::{ @@ -7,6 +8,7 @@ use ::syn::{ }; use proc_macro2::TokenStream; +#[cfg(feature = "to_tokens")] pub fn format_args_f_impl(input: IfmtInput) -> Result { let IfmtInput { format_literal, diff --git a/packages/rsx/src/node.rs b/packages/rsx/src/node.rs index 6828ab58c..0f9025e5e 100644 --- a/packages/rsx/src/node.rs +++ b/packages/rsx/src/node.rs @@ -1,6 +1,7 @@ use super::*; use proc_macro2::TokenStream as TokenStream2; +#[cfg(feature = "to_tokens")] use quote::{quote, ToTokens, TokenStreamExt}; use syn::{ parse::{Parse, ParseStream}, @@ -21,7 +22,6 @@ pub enum BodyNode { Component(Component), Text(LitStr), RawExpr(Expr), - Meta(String), } impl BodyNode { @@ -79,6 +79,7 @@ impl Parse for BodyNode { } } +#[cfg(feature = "to_tokens")] impl ToTokens for BodyNode { fn to_tokens(&self, tokens: &mut TokenStream2) { match &self { @@ -90,7 +91,6 @@ impl ToTokens for BodyNode { BodyNode::RawExpr(exp) => tokens.append_all(quote! { __cx.fragment_from_iter(#exp) }), - BodyNode::Meta(_) => {} } } }