1
0
Эх сурвалжийг харах

allow rsx to be used outside of a proc_macro context

Evan Almloff 3 жил өмнө
parent
commit
06390b9be5

+ 3 - 3
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"]

+ 0 - 1
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")]

+ 7 - 4
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"]

+ 3 - 1
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;

+ 11 - 29
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::<Token![,]>().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::<Token![,]>().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");
-            }
         });
     }
 }

+ 2 - 6
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"));
     };
 }

+ 2 - 0
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<TokenStream> {
     let IfmtInput {
         format_literal,

+ 2 - 2
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(_) => {}
         }
     }
 }