diff --git a/Cargo.toml b/Cargo.toml index fdf4781..8bd9eac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,11 @@ members = [ "examples/helloworld", ] +[features] +default = ["web"] +web = ["stdweb"] + [dependencies] enterprise-compiler = { path = "enterprise-compiler" } -stdweb = "0.4.20" +stdweb = { version = "0.4.20", optional = true } parking_lot = "0.10.0" diff --git a/enterprise-compiler/src/lib.rs b/enterprise-compiler/src/lib.rs index 07689ed..fe8468a 100644 --- a/enterprise-compiler/src/lib.rs +++ b/enterprise-compiler/src/lib.rs @@ -77,7 +77,7 @@ fn process( } impl crate::Component for #name { - fn initialize(&self, el: &enterprise::stdweb::web::Element) { + fn create(&self, el: &enterprise::stdweb::web::Element) { #init_el_code } } diff --git a/examples/helloworld/src/main.rs b/examples/helloworld/src/main.rs index 5f2c265..c42e258 100644 --- a/examples/helloworld/src/main.rs +++ b/examples/helloworld/src/main.rs @@ -14,7 +14,7 @@ fn main() { let web = Web; let app = HelloWorld::new(&web); - enterprise::render(&app, "app"); + web.initialize(app, "app".into()); stdweb::event_loop(); } diff --git a/src/backend.rs b/src/backend.rs deleted file mode 100644 index e250422..0000000 --- a/src/backend.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub trait Backend {} - -pub struct Web; - -impl Backend for Web {} diff --git a/src/backend/mod.rs b/src/backend/mod.rs new file mode 100644 index 0000000..4cd0393 --- /dev/null +++ b/src/backend/mod.rs @@ -0,0 +1,16 @@ +//! Everything related to backends and backend-specific actions. + +mod web; + +pub use self::web::Web; + +use crate::Component; + +/// Describes a possible compile backend. +pub trait Backend: Sized { + /// The parameters that are passed into the init function. + type InitParams; + + /// Initializes the backend with the given component. + fn initialize>(&self, _: C, _: Self::InitParams); +} diff --git a/src/backend/web.rs b/src/backend/web.rs new file mode 100644 index 0000000..968a7c5 --- /dev/null +++ b/src/backend/web.rs @@ -0,0 +1,18 @@ +use stdweb::web::{document, INonElementParentNode}; + +use crate::backend::Backend; +use crate::Component; + +/// Compiling to a web application. +pub struct Web; + +impl Backend for Web { + type InitParams = String; + + fn initialize>(&self, component: C, params: Self::InitParams) { + let id = params.as_ref(); + if let Some(el) = document().get_element_by_id(id) { + component.create(&el); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 1136875..b647568 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ +//! Enterprise is a backend-agnostic framework for developing server-client GUI applications. + +#![deny(missing_docs)] + extern crate enterprise_compiler; // re-exports @@ -7,18 +11,11 @@ pub extern crate stdweb; mod backend; pub use enterprise_compiler::example; -use stdweb::web::{document, Element, INonElementParentNode}; pub use crate::backend::{Backend, Web}; +/// Components are the building-blocks of enterprise applications. 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); - } + /// TODO: replace this with a real init function. + fn create(&self, el: &crate::stdweb::web::Element); }