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
use web_sys::Node;

use super::{VComponent, VElement, VList, VText};

/// VNode is enum representing node in virtual DOM tree.
/// Provides a wrapper over different types of nodes along with concise and convinient API for VDOM manipulation.
#[derive(PartialEq, Debug)]
pub enum VNode {
    /// Represents [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) in DOM and contains [VElement],
    Element(VElement),
    /// Represents [Text](https://developer.mozilla.org/en-US/docs/Web/API/Text) in DOM and contains [VText],
    Text(VText),
    /// Represents a series of adjacent [virtual nodes](VNode) located at the same depth, contains [VList],
    List(VList),
    /// Represents user-defined custom component, contains [VComponent].
    Component(VComponent),
}

impl VNode {
    pub(crate) fn patch(&mut self, last: Option<VNode>, ancestor: &Node) {
        match self {
            VNode::Element(velement) => velement.patch(last, ancestor),
            VNode::Text(vtext) => vtext.patch(last, ancestor),
            VNode::Component(vcomp) => vcomp.patch(last, ancestor),
            VNode::List(vlist) => vlist.patch(last, ancestor),
        };
    }

    pub(crate) fn erase(&self) {
        match self {
            VNode::Element(v) => v.erase(),
            VNode::Text(v) => v.erase(),
            VNode::List(v) => v.erase(),
            VNode::Component(v) => v.erase(),
        }
    }

    pub(crate) fn set_depth(&mut self, depth: u32) {
        match self {
            VNode::Component(vcomp) => vcomp.set_depth(depth),
            VNode::List(vlist) => vlist.set_depth(depth),
            VNode::Element(velem) => velem.set_depth(depth),
            VNode::Text(_) => {}
        }
    }
}

impl From<VElement> for VNode {
    fn from(velement: VElement) -> Self {
        Self::Element(velement)
    }
}

impl From<VComponent> for VNode {
    fn from(vcomp: VComponent) -> Self {
        Self::Component(vcomp)
    }
}

impl From<VText> for VNode {
    fn from(vtext: VText) -> Self {
        Self::Text(vtext)
    }
}

impl From<VList> for VNode {
    fn from(vlist: VList) -> Self {
        Self::List(vlist)
    }
}

impl<T: ToString> From<T> for VNode {
    fn from(t: T) -> Self {
        Self::Text(VText::new(t))
    }
}

impl<T: Into<VNode>> FromIterator<T> for VNode {
    fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self {
        Self::List(VList::new(iter.into_iter().map(Into::into).collect(), None))
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        component::{behavior::Behavior, Component},
        virtual_dom::{VComponent, VElement, VList, VText},
    };
    use wasm_bindgen_test::wasm_bindgen_test;

    use super::VNode;
    wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

    #[wasm_bindgen_test]
    fn from_to_string() {
        let target = String::from("tmp");
        assert_eq!(
            VNode::Text(VText {
                text: "tmp".to_string(),
                dom: None
            }),
            target.into()
        );
    }

    #[wasm_bindgen_test]
    fn from_vec_string() {
        let target = vec![String::from("tmp")];
        assert_eq!(
            VNode::List(VList::new(vec![VText::new("tmp").into()], None)),
            VNode::from_iter(target)
        );
    }

    #[wasm_bindgen_test]
    fn from_vec_elements() {
        let target = vec![VElement::new(
            "div".to_string(),
            [].into(),
            [].into(),
            None,
            [].into(),
        )];
        assert_eq!(
            VNode::List(VList::new(
                vec![VNode::Element(VElement::new(
                    "div".to_string(),
                    [].into(),
                    [].into(),
                    None,
                    [].into()
                ))],
                None
            )),
            VNode::from_iter(target)
        );
    }

    #[wasm_bindgen_test]
    fn from_vec_lists() {
        let target = vec![VList::new(vec![], None)];
        assert_eq!(
            VNode::List(VList::new(
                vec![VNode::List(VList::new(vec![], None,))],
                None
            )),
            VNode::from_iter(target)
        );
    }

    struct Comp;
    impl Component for Comp {
        type Message = ();
        type Properties = ();

        fn new(_props: Self::Properties) -> Self {
            Comp
        }
        fn view(&self, _behavior: &mut impl Behavior<Self>) -> VNode {
            VText::new("Test").into()
        }
        fn update(&mut self, _message: Self::Message) -> bool {
            false
        }
    }

    #[wasm_bindgen_test]
    fn from_vec_comp() {
        let target = vec![VComponent::new::<Comp>((), None)];
        assert_eq!(
            VNode::List(VList::new(
                vec![VNode::Component(VComponent::new::<Comp>((), None))],
                None
            )),
            VNode::from_iter(target)
        );
    }
}