sauron_core/vdom/
map_msg.rs

1use crate::vdom::Attribute;
2use crate::vdom::AttributeValue;
3use crate::vdom::Element;
4use crate::vdom::Leaf;
5use crate::vdom::Node;
6use crate::vdom::TemplatedView;
7
8impl<MSG> Node<MSG> {
9    /// map the msg of this node such that Node<MSG> becomes Node<MSG2>
10    pub fn map_msg<F, MSG2>(self, cb: F) -> Node<MSG2>
11    where
12        F: Fn(MSG) -> MSG2 + Clone + 'static,
13        MSG2: 'static,
14        MSG: 'static,
15    {
16        match self {
17            Node::Element(element) => Node::Element(element.map_msg(cb)),
18            Node::Leaf(leaf) => Node::Leaf(leaf.map_msg(cb)),
19        }
20    }
21}
22
23impl<MSG> Element<MSG> {
24    /// map the msg of this element such that `Element<MSG>` becomes `Element<MSG2>`
25    pub fn map_msg<F, MSG2>(self, cb: F) -> Element<MSG2>
26    where
27        F: Fn(MSG) -> MSG2 + Clone + 'static,
28        MSG2: 'static,
29        MSG: 'static,
30    {
31        Element {
32            namespace: self.namespace,
33            tag: self.tag,
34            attrs: self
35                .attrs
36                .into_iter()
37                .map(|attr| attr.map_msg(cb.clone()))
38                .collect(),
39            children: self
40                .children
41                .into_iter()
42                .map(|child| child.map_msg(cb.clone()))
43                .collect(),
44            self_closing: self.self_closing,
45        }
46    }
47}
48
49impl<MSG> Attribute<MSG> {
50    /// map the msg of this attribute such that `Attribute<MSG>` becomes `Attribute<MSG2>`
51    pub fn map_msg<F, MSG2>(self, cb: F) -> Attribute<MSG2>
52    where
53        F: Fn(MSG) -> MSG2 + Clone + 'static,
54        MSG2: 'static,
55        MSG: 'static,
56    {
57        Attribute {
58            name: self.name,
59            value: self
60                .value
61                .into_iter()
62                .map(|v| v.map_msg(cb.clone()))
63                .collect(),
64            namespace: self.namespace,
65        }
66    }
67}
68
69impl<MSG> AttributeValue<MSG> {
70    /// map the msg of this AttributeValue such that `AttributeValue<MSG>` becomes
71    /// `AttributeValue<MSG2>`
72    pub fn map_msg<F, MSG2>(self, cb: F) -> AttributeValue<MSG2>
73    where
74        F: Fn(MSG) -> MSG2 + Clone + 'static,
75        MSG2: 'static,
76        MSG: 'static,
77    {
78        match self {
79            AttributeValue::Simple(this) => AttributeValue::Simple(this),
80            AttributeValue::Style(this) => AttributeValue::Style(this),
81            AttributeValue::EventListener(this) => AttributeValue::EventListener(this.map_msg(cb)),
82            AttributeValue::ComponentEventListener(this) => {
83                AttributeValue::ComponentEventListener(this)
84            }
85            AttributeValue::Empty => AttributeValue::Empty,
86        }
87    }
88}
89
90impl<MSG> Leaf<MSG> {
91    /// map the msg of this Leaf such that `Leaf<MSG>` becomes `Leaf<MSG2>`
92    pub fn map_msg<F, MSG2>(self, cb: F) -> Leaf<MSG2>
93    where
94        F: Fn(MSG) -> MSG2 + Clone + 'static,
95        MSG2: 'static,
96        MSG: 'static,
97    {
98        match self {
99            Self::Text(v) => Leaf::Text(v),
100            Self::Symbol(v) => Leaf::Symbol(v),
101            Self::Comment(v) => Leaf::Comment(v),
102            Self::DocType(v) => Leaf::DocType(v),
103            Self::Fragment(nodes) => Leaf::Fragment(
104                nodes
105                    .into_iter()
106                    .map(|node| node.map_msg(cb.clone()))
107                    .collect(),
108            ),
109            Self::NodeList(node_list) => Leaf::NodeList(
110                node_list
111                    .into_iter()
112                    .map(|node| node.map_msg(cb.clone()))
113                    .collect(),
114            ),
115            Self::StatefulComponent(v) => Leaf::StatefulComponent(v.map_msg(cb)),
116            Self::StatelessComponent(v) => Leaf::StatelessComponent(v.map_msg(cb)),
117            Self::TemplatedView(v) => Leaf::TemplatedView(v.map_msg(cb)),
118        }
119    }
120}
121
122impl<MSG> TemplatedView<MSG> {
123    /// mape the msg of this TemplatedView such that `TemplatedView<MSG>` becomes `TemplatedView<MSG2>`
124    pub fn map_msg<F, MSG2>(self, cb: F) -> TemplatedView<MSG2>
125    where
126        F: Fn(MSG) -> MSG2 + Clone + 'static,
127        MSG2: 'static,
128        MSG: 'static,
129    {
130        TemplatedView {
131            view: Box::new(self.view.map_msg(cb.clone())),
132            skip_diff: self.skip_diff,
133        }
134    }
135}