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
use std::collections::HashMap;
use web_sys::{Element, Node};
use yew::html::NodeRef;
use yew::virtual_dom::{Classes, VComp, VList, VNode, VTag, VText};

type Attributes = HashMap<String, String>;

#[derive(Debug, PartialEq)]
struct VTagStruct {
    reference: Option<Element>,
    attributes: Attributes,
    classes: Classes,
    value: Option<String>,
    kind: Option<String>,
    checked: bool,
    node_ref: NodeRef,
}

impl From<VTag> for VTagStruct {
    fn from(vtag: VTag) -> Self {
        VTagStruct {
            reference: vtag.reference,
            attributes: vtag.attributes,
            classes: vtag.classes,
            value: vtag.value,
            kind: vtag.kind,
            checked: vtag.checked,
            node_ref: vtag.node_ref,
        }
    }
}

/// All the properties of VNodeStruct
#[derive(Debug, PartialEq)]
pub struct VNodeStruct {
    vtag: Option<VTagStruct>,
    vlist: Option<VList>,
    vtext: Option<VText>,
    vcomp: Option<VComp>,
    vref: Option<Node>,
    children: Option<Vec<VNodeStruct>>,
}

impl VNodeStruct {
    /// Get all the complete structure from VNode
    pub fn new(vnode: VNode) -> Self {
        match vnode {
            VNode::VTag(ref vtag) => VNodeStruct {
                vtag: Some(VTagStruct::from(*vtag.clone())),
                vlist: None,
                vtext: None,
                vcomp: None,
                vref: None,
                children: if !vtag.children.is_empty() {
                    Some(vec![VNodeStruct::new(VNode::VList(vtag.children.clone()))])
                } else {
                    None
                },
            },
            VNode::VText(ref vtext) => VNodeStruct {
                vtag: None,
                vlist: None,
                vtext: Some(VText {
                    text: vtext.text.clone(),
                    reference: vtext.reference.clone(),
                }),
                vcomp: None,
                vref: None,
                children: None,
            },
            VNode::VList(ref vlist) => VNodeStruct {
                vtag: None,
                vlist: Some(vlist.clone()),
                vtext: None,
                vcomp: None,
                vref: None,
                children: if !vlist.children.is_empty() {
                    Some(
                        vlist
                            .children
                            .clone()
                            .into_iter()
                            .map(VNodeStruct::new)
                            .collect(),
                    )
                } else {
                    None
                },
            },
            VNode::VComp(ref vcomp) => VNodeStruct {
                vtag: None,
                vlist: None,
                vtext: None,
                vcomp: Some(vcomp.clone()),
                vref: None,
                children: None,
            },
            VNode::VRef(ref vref) => VNodeStruct {
                vtag: None,
                vlist: None,
                vtext: None,
                vcomp: None,
                vref: Some(vref.clone()),
                children: None,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::VNodeStruct;
    use yew::html;

    #[test]
    fn should_match_the_vnode_structures() {
        let vnode = html! {
            <div id="example">{"example"}</div>
        };

        let vnode_structure = VNodeStruct::new(vnode);

        let vnode_expected = html! {
            <div id="example">{"example"}</div>
        };

        let vnode_structure_expected = VNodeStruct::new(vnode_expected);

        assert_eq!(vnode_structure, vnode_structure_expected);
    }

    #[test]
    fn should_no_match_the_vnode_structures() {
        let vnode = html! {
            <div id="example">{"example"}</div>
        };

        let vnode_structure = VNodeStruct::new(vnode);

        let vnode_expected = html! {
            <div id="second-example">{"second example"}</div>
        };

        let vnode_structure_expected = VNodeStruct::new(vnode_expected);

        assert_ne!(vnode_structure, vnode_structure_expected);
    }
}