From b316508b7f333d5d18ef39272e8716f17e828c17 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 7 Feb 2020 01:43:24 -0600 Subject: [PATCH] test --- enterprise-compiler/src/lib.rs | 68 ++++++++++++++++++---------------- src/main.rs | 30 +++++++++++++-- 2 files changed, 63 insertions(+), 35 deletions(-) diff --git a/enterprise-compiler/src/lib.rs b/enterprise-compiler/src/lib.rs index e2134bc..42a1aef 100644 --- a/enterprise-compiler/src/lib.rs +++ b/enterprise-compiler/src/lib.rs @@ -1,5 +1,7 @@ -#[macro_use] extern crate quote; -#[macro_use] extern crate maplit; +#[macro_use] +extern crate quote; +#[macro_use] +extern crate maplit; extern crate proc_macro; use std::collections::{HashMap, HashSet}; @@ -7,7 +9,7 @@ use std::collections::{HashMap, HashSet}; use petgraph::{dot::Dot, graph::Graph, graphmap::DiGraphMap}; use proc_macro2::{Span, TokenStream, TokenTree}; use quote::ToTokens; -use syn::{Expr, ExprPath, Ident, Path, PathSegment, PathArguments, punctuated::Punctuated}; +use syn::{punctuated::Punctuated, Expr, ExprPath, Ident, Path, PathArguments, PathSegment}; #[derive(Debug, PartialEq, Eq, Hash)] enum TagLhs { @@ -28,7 +30,6 @@ enum Rsx { Tag(Tag), Code(Expr), Text(String), - // For(String, String, Vec), } @@ -71,11 +72,11 @@ impl Visitor { model: HashMap::new(), } } - + fn load_model(&mut self, model: &HashMap) { self.model.extend(model.clone()); } - + fn unique_name(&mut self, base: impl AsRef) -> String { // TODO: normalize the name somehow so it fits in an ident (ex. strip punct) let base = base.as_ref(); @@ -83,7 +84,7 @@ impl Visitor { self.idx += 1; format!("{}_{}", base, next) } - + fn unique_idx(&mut self, node: DepNode) -> u32 { let next = self.idx; self.idx += 1; @@ -119,13 +120,13 @@ impl Visitor { fn extract_model_dependencies(expr: &Expr) -> HashSet { let tokens = expr.to_token_stream(); let mut result = HashSet::new(); - + for token in tokens.into_iter() { if let TokenTree::Ident(ident) = token { result.insert(format!("{}", ident)); } } - + result } @@ -137,39 +138,27 @@ pub fn example(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream "name".into() => "String".into(), }; + let todomvc_datainit: HashMap = hashmap! { + "name".into() => "\"world\".into()".into(), + }; + let todomvc_dom = vec![ Rsx::Tag(Tag { tag: "input".into(), attrs: hashmap! { - TagLhs::Bind("value".into()) => "name".into(), + TagLhs::Bind("value".into()) => "name".into(), }, ..Tag::default() }), Rsx::Text("Hello, ".into()), - Rsx::Code(Expr::Path(ExprPath { - attrs: vec![], - qself: None, - path: Path { - leading_colon: None, - segments: { - let mut segments = Punctuated::new(); - let ident = Ident::new("name", Span::call_site()); - let arguments = PathArguments::None; - segments.push(PathSegment { - ident, - arguments, - }); - segments - }, - }, - })), + Rsx::Code(syn::parse_str::("name").unwrap()), Rsx::Text("!".into()), ]; - + let mut visitor = Visitor::new(); visitor.load_model(&todomvc_datamodel); visitor.visit(&todomvc_dom); - + println!("{:?}", visitor); println!("DOT:"); let graph: Graph<_, _, _> = visitor.deps.0.clone().into_graph(); @@ -177,19 +166,34 @@ pub fn example(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream let name = format_ident!("{}", todomvc_name); let mut model = TokenStream::new(); + let mut init = TokenStream::new(); for (name, ty) in visitor.model { let name = format_ident!("{}", name); // TODO: parse this into an actual expression tree for Vec let ty = format_ident!("{}", ty); - model.extend(quote!{ #name : #ty , }); + model.extend(quote! { #name : #ty , }); + } + for (name, value) in todomvc_datainit { + let name = format_ident!("{}", name); + let value = syn::parse_str::(&value).unwrap(); + init.extend(quote! { #name : #value , }); } let result = quote! { struct #name { #model } - + impl #name { - + fn new() -> Self { + #name { + #init + } + } + } + + impl crate::Component for #name { + fn initialize(&self, el: &crate::Element) { + } } }; diff --git a/src/main.rs b/src/main.rs index 411f3d7..0f142ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,31 @@ -#[macro_use] extern crate enterprise_compiler; +#[macro_use] +extern crate enterprise_compiler; +#[macro_use] +extern crate stdweb; + +use stdweb::web::{document, Element, INonElementParentNode}; + +trait Component { + fn initialize(&self, element: &Element); +} example!(); -fn main() { - +fn render(component: &C, id: impl AsRef) { + let id = id.as_ref(); + if let Some(el) = document().get_element_by_id(id) { + component.initialize(&el); + } +} + +fn main() { + stdweb::initialize(); + + let todomvc = TodoMVC::new(); + render(&todomvc, ""); + + let message = "Hello world!"; + js! { console.log(@{message}); } + + stdweb::event_loop(); }