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
/// This module provides traits that help users define how their types should be rendered.
/// Implementation for primitive types is also provided.
use crate::dom::{Nodes, StaticNodes};

pub trait Render<C> {
    fn render(self, nodes: Nodes<C>) -> Nodes<C>;
}

pub trait StaticRender<C> {
    fn render(self, nodes: StaticNodes<C>) -> StaticNodes<C>;
}

macro_rules! impl_render_with_to_string {
    ($($type:ident)+) => {
        $(
            impl<C> Render<C> for $type {
                fn render(self, nodes: Nodes<C>) -> Nodes<C> {
                    nodes.update_text(&self.to_string())
                }
            }
            impl<C> StaticRender<C> for $type {
                fn render(self, nodes: StaticNodes<C>) -> StaticNodes<C> {
                    nodes.static_text(&self.to_string())
                }
            }
        )+
    }
}

impl_render_with_to_string! {
    i8 i16 i32 i64 u8 u16 u32 u64 isize usize f32 f64 bool
}

// Special case for 'static str => always render as static text
impl<C> Render<C> for &'static str {
    fn render(self, nodes: Nodes<C>) -> Nodes<C> {
        nodes.static_text(self)
    }
}

impl<C> StaticRender<C> for &str {
    fn render(self, nodes: StaticNodes<C>) -> StaticNodes<C> {
        nodes.static_text(self)
    }
}

impl<C> Render<C> for &String {
    fn render(self, nodes: Nodes<C>) -> Nodes<C> {
        nodes.update_text(self)
    }
}

impl<C> StaticRender<C> for &String {
    fn render(self, nodes: StaticNodes<C>) -> StaticNodes<C> {
        nodes.static_text(self)
    }
}

pub trait ListItem<C> {
    const ROOT_ELEMENT_TAG: &'static str;
    fn render(&self, state: &C, item: crate::dom::ElementUpdater<C>);
}