From cdb7a9b8e30cb99b456dfe50e6a272ab1cebeeed Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 10 Feb 2020 18:39:44 -0600 Subject: [PATCH] make a separate crate --- .gitignore | 8 ------ Cargo.lock | 8 ++++++ Cargo.toml | 1 + enterprise-compiler/src/lib.rs | 13 +++++---- enterprise-compiler/src/visitor.rs | 24 ++++++++++------ examples/helloworld/.gitignore | 2 ++ examples/helloworld/Cargo.toml | 9 ++++++ examples/helloworld/src/main.rs | 29 +++++++++++++++++++ src/lib.rs | 24 ++++++++++++++++ src/main.rs | 46 ------------------------------ 10 files changed, 96 insertions(+), 68 deletions(-) create mode 100644 examples/helloworld/.gitignore create mode 100644 examples/helloworld/Cargo.toml create mode 100644 examples/helloworld/src/main.rs create mode 100644 src/lib.rs delete mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 532b1c7..53eaa21 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,2 @@ /target **/*.rs.bk - - -#Added by cargo -# -#already existing elements are commented out - -#/target -#**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock index 2ceb4c9..f1efc8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,14 @@ name = "fixedbitset" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "helloworld" +version = "0.1.0" +dependencies = [ + "enterprise 0.1.0", + "stdweb 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "indexmap" version = "1.3.1" diff --git a/Cargo.toml b/Cargo.toml index 7dedd23..fdf4781 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [workspace] members = [ "enterprise-compiler", + "examples/helloworld", ] [dependencies] diff --git a/enterprise-compiler/src/lib.rs b/enterprise-compiler/src/lib.rs index 0e53e07..07689ed 100644 --- a/enterprise-compiler/src/lib.rs +++ b/enterprise-compiler/src/lib.rs @@ -38,12 +38,12 @@ fn process( let name = format_ident!("{}", name); // TODO: parse this into an actual expression tree for Vec let ty = format_ident!("{}", ty); - model.extend(quote! { #name : Arc> , }); + model.extend(quote! { #name : Arc> , }); } for (name, value) in datainit { let name = format_ident!("{}", name); let value = syn::parse_str::(&value).unwrap(); - init.extend(quote! { #name : Arc::new(Mutex::new(#value)) , }); + init.extend(quote! { #name : Arc::new(enterprise::parking_lot::Mutex::new(#value)) , }); } let impl_code = &visitor.impl_code; @@ -51,8 +51,11 @@ fn process( for fn_name in toplevel_names.iter() { let fn_name = format_ident!("{}", fn_name); init_el_code.extend(quote! { - let sub = self.#fn_name(); - el.append_child(&sub); + { + use enterprise::stdweb::web::INode; + let sub = self.#fn_name(); + el.append_child(&sub); + } }); } @@ -74,7 +77,7 @@ fn process( } impl crate::Component for #name { - fn initialize(&self, el: &crate::Element) { + fn initialize(&self, el: &enterprise::stdweb::web::Element) { #init_el_code } } diff --git a/enterprise-compiler/src/visitor.rs b/enterprise-compiler/src/visitor.rs index efd43ee..baae472 100644 --- a/enterprise-compiler/src/visitor.rs +++ b/enterprise-compiler/src/visitor.rs @@ -188,7 +188,8 @@ impl Visitor { let id_str = id.as_str(); update_func.extend(quote! { { - if let Some(target) = document().get_element_by_id(#id_str) { + use enterprise::stdweb::web::{INonElementParentNode, INode}; + if let Some(target) = enterprise::stdweb::web::document().get_element_by_id(#id_str) { target.set_text_content(&new_value.clone()); } } @@ -199,17 +200,21 @@ impl Visitor { } } updates.extend(quote! { - let inner_el = el.clone(); - el.add_event_listener(move |evt: stdweb::web::event::InputEvent| { - let new_value = InputElement::try_from(inner_el.clone()).unwrap().raw_value(); - #update_func - }); + { + use enterprise::stdweb::{web::IEventTarget, unstable::TryFrom}; + let inner_el = el.clone(); + el.add_event_listener(move |evt: stdweb::web::event::InputEvent| { + let new_value = enterprise::stdweb::web::html_element::InputElement::try_from(inner_el.clone()).unwrap().raw_value(); + #update_func + }); + } }); } } self.impl_code.extend(quote! { fn #make_node_id(&self) -> impl stdweb::web::INode { - let el = document().create_element(#tag).unwrap(); + use enterprise::stdweb::web::IElement; + let el = enterprise::stdweb::web::document().create_element(#tag).unwrap(); el.set_attribute("id", #node_str).unwrap(); #updates el @@ -221,7 +226,8 @@ impl Visitor { self.impl_code.extend(quote! { #[inline] fn #make_node_id(&self) -> impl stdweb::web::INode { - let el = document().create_element("span").expect("shouldn't fail"); + use enterprise::stdweb::web::IElement; + let el = enterprise::stdweb::web::document().create_element("span").expect("shouldn't fail"); el.set_attribute("id", #node_str).unwrap(); el } @@ -231,7 +237,7 @@ impl Visitor { self.impl_code.extend(quote! { #[inline] fn #make_node_id(&self) -> impl stdweb::web::INode { - document().create_text_node(#literal) + enterprise::stdweb::web::document().create_text_node(#literal) } }); } diff --git a/examples/helloworld/.gitignore b/examples/helloworld/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/examples/helloworld/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/examples/helloworld/Cargo.toml b/examples/helloworld/Cargo.toml new file mode 100644 index 0000000..a1d52d2 --- /dev/null +++ b/examples/helloworld/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "helloworld" +version = "0.1.0" +authors = ["Michael Zhang "] +edition = "2018" + +[dependencies] +stdweb = "0.4.20" +enterprise = { path = "../.." } \ No newline at end of file diff --git a/examples/helloworld/src/main.rs b/examples/helloworld/src/main.rs new file mode 100644 index 0000000..892f5a8 --- /dev/null +++ b/examples/helloworld/src/main.rs @@ -0,0 +1,29 @@ +#[macro_use] +extern crate enterprise; + +// #[macro_use] +extern crate stdweb; + +use std::sync::Arc; + +use enterprise::{Backend, Component, Web}; + +// use stdweb::{ +// unstable::TryFrom, +// web::{ +// document, html_element::InputElement, Element, IElement, IEventTarget, INode, +// INonElementParentNode, +// }, +// }; + +example!(); + +fn main() { + stdweb::initialize(); + + let web = Web; + let app = HelloWorld::new(&web); + enterprise::render(&app, "app"); + + stdweb::event_loop(); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1136875 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,24 @@ +extern crate enterprise_compiler; + +// re-exports +pub extern crate parking_lot; +pub extern crate stdweb; + +mod backend; + +pub use enterprise_compiler::example; +use stdweb::web::{document, Element, INonElementParentNode}; + +pub use crate::backend::{Backend, Web}; + +pub trait Component { + fn initialize(&self, element: &Element); +} + +pub fn render>(component: &C, id: impl AsRef) { + let id = id.as_ref(); + if let Some(el) = document().get_element_by_id(id) { + println!("Rendering..."); + component.initialize(&el); + } +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index c7d5c3c..0000000 --- a/src/main.rs +++ /dev/null @@ -1,46 +0,0 @@ -#[macro_use] -extern crate enterprise_compiler; -#[macro_use] -extern crate stdweb; - -mod backend; - -use std::sync::Arc; - -use parking_lot::Mutex; -use stdweb::{ - unstable::TryFrom, - web::{ - document, html_element::InputElement, Element, IElement, IEventTarget, INode, - INonElementParentNode, - }, -}; - -use crate::backend::{Backend, Web}; - -trait Component { - fn initialize(&self, element: &Element); -} - -example!(); - -fn render>(component: &C, id: impl AsRef) { - let id = id.as_ref(); - if let Some(el) = document().get_element_by_id(id) { - println!("Rendering..."); - component.initialize(&el); - } -} - -fn main() { - stdweb::initialize(); - - let web = Web; - let app = HelloWorld::new(&web); - render(&app, "app"); - - let message = "Hello world!"; - js! { console.log(@{message}); } - - stdweb::event_loop(); -}