1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! # Domafic - Safe, high-performance, universal web applications
//!
//! Domafic is a library for building templates and interactive web applications. Applications
//! built in Domafic can be rendered server-side and used in front-end web servers, but they can
//! also be deployed directly to the client using asm.js and WebAssembly.
//!
//! A simple template:
//!
//! ```rust
//! use domafic::{DomNode, IntoNode};
//! use domafic::tags::{div, h1};
//! use domafic::empty::empty;
//!
//! type Msg = ();
//!
//! // Create a function `render` from `birthday: &'static str` to `DomNode<Message=Msg>`
//! let render = |birthday: &'static str| div((
//!     h1((
//!         "Hello, world! Your birthday is: ".into_node(),
//!         birthday.into_node(),
//!     )),
//!
//!     // Since we don't publish any messages, we need to create an empty node with our
//!     // message type. This tells the compiler that our message type is `Msg`. This would
//!     // be unnecessary if we published any messages or if we specified the return type of
//!     // the `render` function.
//!     empty::<Msg>(),
//! ));
//!
//! assert_eq!(
//!     "<div><h1>Hello, world! Your birthday is: Christmas</h1></div>".to_string(),
//!     render("Christmas").to_string()
//! );
//! ```
//!
//! If you've used HTML or JSX, the syntax should look familiar. Note that we didn't need
//! to use any macros or interpreters-- the template above is just pure, allocation-free Rust. The
//! template itself is just a function that returns a `DomNode`. The `DomNode` trait lets us use
//! the result of `render` as an HTML node. We can write `DomNode`s to HTML, render them to a live
//! web page using Javascript, or use them as children of other `DomNode`s.
//!
//! Domafic's design is similar to that of popular single-state frontend frameworks such as Elm
//! or Redux. An application consists of state, an updater, and a renderer.
//!
//! The application state holds all of the information needed by the renderer to draw the page.
//! The renderer is a function that takes the current state as input and produces the current UI as
//! output. Finally, the updater is responsible for recieving messages generated by event listeners
//! and updating the application state accordingly.
//!
//! For example, here is a simple example showing a counter and +/- buttons:
//!
//! ```rust
//! use domafic::IntoNode;
//! use domafic::tags::{button, div, h1};
//! use domafic::listener::on;
//!
//! // If rendering client-side with asm.js or WebAssembly:
//! #[cfg(target_os = "emscripten")]
//! use domafic::web_render::run;
//! #[cfg(target_os = "emscripten")]
//! use domafic::KeyIter;
//!
//! type State = isize;
//!
//! enum Msg {
//!     Increment,
//!     Decrement,
//! }
//!
//! #[cfg(target_os = "emscripten")]
//! let update = |state: &mut State, msg: Msg, _keys: KeyIter| {
//!     *state = match msg {
//!         Msg::Increment => *state + 1,
//!         Msg::Decrement => *state - 1,
//!     }
//! };
//!
//! let render = |state: &State| {
//!     div ((
//!         h1("Hello from rust!".into_node()),
//!         button ((
//!             on("click", |_| Msg::Decrement),
//!             "-".into_node(),
//!         )),
//!         state.to_string().into_node(),
//!         button ((
//!             on("click", |_| Msg::Increment),
//!             "+".into_node(),
//!         )),
//!     ))
//! };
//!
//! // If rendering server-side:
//! #[cfg(not(target_os = "emscripten"))]
//! println!("HTML: {}", render(&0));
//!
//! // If rendering client-side with asm.js or WebAssembly:
//! #[cfg(target_os = "emscripten")]
//! run("body", update, render, 0);
//! ```
//!
//! Check out more examples like this one
//! [in the Github repository.](https://github.com/cramertj/domafic-rs/tree/master/examples)
//!
//! The above example, if compiled for an emscripten target
//! (via `cargo build --target=asmjs-unknown-emscripten` or similar) will produce a Javascript file
//! that, when included on a webpage, will replace the contents of "body" with the message
//! "Hello from rust!", +/- buttons, and a number.
//!
//! So how does this all work? When the call to `run` occurs, Domafic gives the initial state (0)
//! to the renderer (our "render" function) which returns the initial page to display to the user.
//!
//! This page includes buttons with listeners for `on("click", ...)`, so when a button is clicked,
//! the appropriate message is generated (either `Msg::Increment` or `Msg::Decrement`). This
//! message is then passed into the updater (our `update` function) and used to update the state.
//!
//! Once the state is successfully updated, `render` is called once more to redraw the page.
//! When run in the browser, Domafic keeps an internal DOM (tree-based representation of the UI)
//! and uses it to minimize the changes that need to be made on-screen. This prevents unnecessary
//! re-drawing of UI components.
//!
//! One last thing you may have noticed:
//! we've been writing our `render` functions as closures, rather than named functions.
//! The reason for this is that the return type of the `render` method is long and hard
//! to write out. If you must use named functions, consider using the nightly
//! `conservative_impl_trait` feature, which will allow you to write the function signature of
//! `render` like `fn render(state: &State) -> impl DomNode<Message=Msg>`.

#![cfg_attr(test, feature(conservative_impl_trait))]
#![cfg_attr(not(any(feature = "use_std", test)), no_std)]
#![allow(unused_unsafe)]
#![deny(missing_docs)]

/// Trait for elements that can be drawn as to HTML DOM nodes
pub mod dom_node;
pub use dom_node::{DomNode, DomValue, IntoNode};

#[cfg(any(feature = "use_std", test))]
/// Types, traits and functions for writing a `DomNode` to HTML
pub mod html_writer;

mod keys;
pub use keys::KeyIter;
/// Types, traits, and functions for creating event handlers
pub mod listener;
pub use listener::{Listener, Event, on};
/// Traits for processing collections of `DomNode`s or `Listener`s
pub mod processors;
pub use processors::{DomNodes, Listeners};
/// Types and functions for creating tag elements such as `div`s or `span`s
pub mod tags;

#[cfg(all(feature = "web_render", target_os = "emscripten"))]
/// Functions for interacting with a webpage when rendering client-side using asmjs or emscripten
pub mod web_render;

/// A mapping between an attribute key and value.
/// Example: `("key", AttributeValue::Str("value"))`
pub type KeyValue = (&'static str, AttributeValue);

/// A value of a `DomNode` attribute.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum AttributeValue {
    /// A value represented by a static string reference
    Str(&'static str),
    /// A value represented by an owned `String`
    OwnedStr(String),
    /// A boolean value
    Bool(bool),

    // TODO: add numeric variants?
}

impl AttributeValue {
    /// Extracts a string slice representing the contents.
    /// If the value is a `Bool`, this method returns "true" or "false".
    fn as_str(&self) -> &str {
        match *self {
            AttributeValue::Str(value) => value,
            AttributeValue::OwnedStr(ref value) => value,
            AttributeValue::Bool(true) => "true",
            AttributeValue::Bool(false) => "false",
        }
    }
}

#[cfg(any(feature = "use_std", test))]
impl std::fmt::Display for AttributeValue {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Types and functions for creating `DomNodes` or `Listeners` with no runtime representation.
pub mod empty {
    #[cfg(not(any(feature = "use_std", test)))]
    extern crate core as std;
    use std::marker::PhantomData;

    use super::processors::{DomNodes, DomNodeProcessor, Listeners, ListenerProcessor};

    /// An empty set of nodes with no children or attributes.
    /// Instances of this type have no DOM representation.
    #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
    pub struct EmptyNodes<Message>(pub PhantomData<Message>);

    /// Creates a new `EmptyNodes`.
    pub fn empty<Message>() -> EmptyNodes<Message> { EmptyNodes(PhantomData) }

    impl<M> DomNodes for EmptyNodes<M> {
        type Message = M;
        fn process_all<'a, P: DomNodeProcessor<'a, M>>(&'a self, _acc: &mut P::Acc) -> Result<(), P::Error> {
            Ok(())
        }
    }

    /// An empty set of listeners.
    /// Instances of this type have no DOM representation.
    #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
    pub struct EmptyListeners<Message>(pub PhantomData<Message>);

    /// Creates a new `EmptyListeners`.
    pub fn empty_listeners<Message>() -> EmptyListeners<Message> { EmptyListeners(PhantomData) }
    impl<M> Listeners for EmptyListeners<M> {
        type Message = M;
        fn process_all<'a, P: ListenerProcessor<'a, Self::Message>>(&self, _acc: &mut P::Acc) -> Result<(), P::Error> {
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{DomNode, DomValue, KeyValue, IntoNode};
    use super::AttributeValue::Str;
    use super::tags::*;
    use super::processors::{DomNodes, DomNodeProcessor};
    use super::empty::{empty, empty_listeners, EmptyNodes, EmptyListeners};
    use super::html_writer::HtmlWriter;

    #[cfg(feature = "use_either_n")]
    extern crate either_n;
    #[cfg(feature = "use_either_n")]
    use self::either_n::*;

    use std::marker::PhantomData;

    struct BogusOne(EmptyNodes<Never>, EmptyListeners<Never>);
    const BOGUS_1: BogusOne = BogusOne(EmptyNodes(PhantomData), EmptyListeners(PhantomData));
    impl DomNode for BogusOne {
        type Message = Never;
        type Children = EmptyNodes<Self::Message>;
        type Listeners = EmptyListeners<Self::Message>;
        type WithoutListeners = BogusOne;

        fn children(&self) -> &Self::Children { &self.0 }
        fn listeners(&self) -> &Self::Listeners { &self.1 }
        fn children_and_listeners(&self) -> (&Self::Children, &Self::Listeners) {
            (&self.0, &self.1)
        }
        fn split_listeners(self) -> (Self::WithoutListeners, Self::Listeners) {
            (BOGUS_1, empty_listeners())
        }

        fn key(&self) -> Option<u32> { None }
        fn get_attribute(&self, _index: usize) -> Option<&KeyValue> { None }
        fn value(&self) -> DomValue {
            DomValue::Element { tag: "bogus_tag_one" }
        }
    }

    struct BogusTwo(EmptyNodes<Never>, EmptyListeners<Never>);
    const BOGUS_2: BogusTwo = BogusTwo(EmptyNodes(PhantomData), EmptyListeners(PhantomData));
    impl DomNode for BogusTwo {
        type Message = Never;
        type Children = EmptyNodes<Self::Message>;
        type Listeners = EmptyListeners<Self::Message>;
        type WithoutListeners = BogusTwo;

        fn key(&self) -> Option<u32> { None }
        fn get_attribute(&self, _index: usize) -> Option<&KeyValue> { None }

        fn children(&self) -> &Self::Children { &self.0 }
        fn listeners(&self) -> &Self::Listeners { &self.1 }
        fn children_and_listeners(&self) -> (&Self::Children, &Self::Listeners) {
            (&self.0, &self.1)
        }
        fn split_listeners(self) -> (Self::WithoutListeners, Self::Listeners) {
            (BOGUS_2, empty_listeners())
        }

        fn value(&self) -> DomValue {
            DomValue::Element { tag: "bogus_tag_two" }
        }
    }

    struct ChildCounter;
    #[derive(Debug, Clone, Copy)]
    enum Never {}
    impl<'a, M> DomNodeProcessor<'a, M> for ChildCounter {
        type Acc = usize;
        type Error = Never;

        fn get_processor<T: DomNode>() -> fn(&mut Self::Acc, &'a T) -> Result<(), Never> {
            fn incr<'a, T: DomNode>(count: &mut usize, _node: &'a T) -> Result<(), Never> {
                *count += 1;
                Ok(())
            }
            incr
        }
    }

    fn html_sample() -> impl DomNode<Message = Never> + 'static {
        div ((
            attributes([("attr", Str("value"))]),
            (
            BOGUS_1,
            BOGUS_1,
            BOGUS_2,
            table ((
                "something".into_node(),
                th (empty()),
                tr (empty()),
                tr (empty()),
            )),
            )
        ))
    }

    #[cfg(feature = "use_either_n")]
    fn html_either(include_rows: bool) -> impl DomNode<Message = Never> + 'static {
        div((
            table((
                if include_rows {
                    Either2::One((
                        tr("a".into_node()),
                        tr("b".into_node()),
                    ))
                } else {
                    Either2::Two("sumthin else".into_node())
                }
            ))
        ))
    }

    #[cfg(feature = "use_either_n")]
    fn builds_an_either_string(arg: bool, expected: &'static str) {
        let mut string_buffer = Vec::new();
        html_either(arg).process_all::<HtmlWriter<Vec<u8>>>(&mut string_buffer).unwrap();
        let string = String::from_utf8(string_buffer).unwrap();
        assert_eq!(
            without_whitespace(expected.to_string()),
            without_whitespace(string)
        );
    }

    #[cfg(feature = "use_either_n")]
    #[test]
    fn builds_either_string() {
        builds_an_either_string(true, r#"
        <div>
            <table>
                <tr>a</tr>
                <tr>b</tr>
            </table>
        </div>
        "#);

        builds_an_either_string(false, r#"
        <div>
            <table>
                sumthin else
            </table>
        </div>
        "#);
    }

    #[test]
    fn counts_children() {
        let mut count = 0;
        (BOGUS_1, BOGUS_1, BOGUS_2).process_all::<ChildCounter>(&mut count).unwrap();
        assert_eq!(3, count);

        count = 0;
        (BOGUS_1, (BOGUS_1,), BOGUS_2).process_all::<ChildCounter>(&mut count).unwrap();
        assert_eq!(3, count);

        count = 0;
        [BOGUS_1, BOGUS_1, BOGUS_1].process_all::<ChildCounter>(&mut count).unwrap();
        assert_eq!(3, count);

        count = 0;
        (BOGUS_1, BOGUS_1,
            [BOGUS_1, BOGUS_1, BOGUS_1],
            [(BOGUS_1)],
            vec![empty(), empty(), empty()],
            [BOGUS_2, BOGUS_2, BOGUS_2],
        ).process_all::<ChildCounter>(&mut count).unwrap();
        assert_eq!(9, count);

        let sample = html_sample();

        count = 0;
        sample.process_all::<ChildCounter>(&mut count).unwrap();
        assert_eq!(1, count);

        count = 0;
        sample.children().process_all::<ChildCounter>(&mut count).unwrap();
        assert_eq!(4, count);
    }

    fn without_whitespace(string: String) -> String {
        string.chars().filter(|c| !c.is_whitespace()).collect()
    }

    #[test]
    fn builds_string() {
        let mut string_buffer = Vec::new();
        html_sample().process_all::<HtmlWriter<Vec<u8>>>(&mut string_buffer).unwrap();
        let string = String::from_utf8(string_buffer).unwrap();
        assert_eq!(
            without_whitespace(r#"
            <div attr="value">
                <bogus_tag_one></bogus_tag_one>
                <bogus_tag_one></bogus_tag_one>
                <bogus_tag_two></bogus_tag_two>
                <table>
                    something
                    <th></th>
                    <tr></tr>
                    <tr></tr>
                </table>
            </div>
            "#.to_string()),
            without_whitespace(string)
        );
    }

    fn check_attribute_list<T: DomNode>(div: T) {
        assert_eq!(div.get_attribute(0), Some(&("attr1", Str("val1"))));
        assert_eq!(div.get_attribute(1), Some(&("attr2", Str("val2"))));
        assert_eq!(div.get_attribute(2), Some(&("attr3", Str("val3"))));
        assert_eq!(div.get_attribute(3), None);

        let mut attr_iter = div.attributes();
        assert_eq!(attr_iter.next(), Some(&("attr1", Str("val1"))));
        assert_eq!(attr_iter.next(), Some(&("attr2", Str("val2"))));
        assert_eq!(attr_iter.next(), Some(&("attr3", Str("val3"))));
        assert_eq!(attr_iter.next(), None);
    }

    #[test]
    fn builds_attribute_list() {
        let div1 = div(empty::<Never>())
            .with_attributes([("attr2", Str("val2")), ("attr3", Str("val3"))])
            .with_attributes([("attr1", Str("val1"))]);
        check_attribute_list(div1);

        let div2 = div((
            attributes([("attr2", Str("val2")), ("attr3", Str("val3"))]),
            div(empty::<Never>())
        )).with_attributes([("attr1", Str("val1"))]);
        check_attribute_list(div2);
    }
}