make a separate crate

This commit is contained in:
Michael Zhang 2020-02-10 18:39:44 -06:00
parent 46be78e007
commit cdb7a9b8e3
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
10 changed files with 96 additions and 68 deletions

8
.gitignore vendored
View file

@ -1,10 +1,2 @@
/target
**/*.rs.bk
#Added by cargo
#
#already existing elements are commented out
#/target
#**/*.rs.bk

8
Cargo.lock generated
View file

@ -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"

View file

@ -7,6 +7,7 @@ edition = "2018"
[workspace]
members = [
"enterprise-compiler",
"examples/helloworld",
]
[dependencies]

View file

@ -38,12 +38,12 @@ fn process(
let name = format_ident!("{}", name);
// TODO: parse this into an actual expression tree for Vec<T>
let ty = format_ident!("{}", ty);
model.extend(quote! { #name : Arc<Mutex<#ty>> , });
model.extend(quote! { #name : Arc<enterprise::parking_lot::Mutex<#ty>> , });
}
for (name, value) in datainit {
let name = format_ident!("{}", name);
let value = syn::parse_str::<Expr>(&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<B: Backend> crate::Component<B> for #name<B> {
fn initialize(&self, el: &crate::Element) {
fn initialize(&self, el: &enterprise::stdweb::web::Element) {
#init_el_code
}
}

View file

@ -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)
}
});
}

2
examples/helloworld/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
**/*.rs.bk

View file

@ -0,0 +1,9 @@
[package]
name = "helloworld"
version = "0.1.0"
authors = ["Michael Zhang <iptq@protonmail.com>"]
edition = "2018"
[dependencies]
stdweb = "0.4.20"
enterprise = { path = "../.." }

View file

@ -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();
}

24
src/lib.rs Normal file
View file

@ -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<B: Backend> {
fn initialize(&self, element: &Element);
}
pub fn render<B: Backend, C: Component<B>>(component: &C, id: impl AsRef<str>) {
let id = id.as_ref();
if let Some(el) = document().get_element_by_id(id) {
println!("Rendering...");
component.initialize(&el);
}
}

View file

@ -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<B: Backend> {
fn initialize(&self, element: &Element);
}
example!();
fn render<B: Backend, C: Component<B>>(component: &C, id: impl AsRef<str>) {
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();
}