duid_core/core/duid_events/
map_msg.rs1use crate::core::{
2 html::{
3 attributes::{AttributeValue, Attribute},
4 nodes::{Node, Element, Leaf}
5 },
6 events::listener::Listener
7};
8
9
10pub trait NodeMapMsg<MSG>
11where
12 MSG: 'static,
13{
14 fn map_msg<F, MSG2>(self, func: F) -> Node<MSG2>
15 where
16 F: Fn(MSG) -> MSG2 + 'static,
17 MSG2: 'static;
18
19 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Node<MSG2>
20 where
21 MSG2: 'static;
22}
23
24
25pub trait ElementMapMsg<MSG>
26where
27 MSG: 'static,
28{
29 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Element<MSG2>
30 where
31 MSG2: 'static;
32}
33
34pub trait LeafMapMsg<MSG>
35where
36 MSG: 'static,
37{
38 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Leaf<MSG2>
39 where
40 MSG2: 'static;
41}
42
43
44pub trait AttributeMapMsg<MSG>
45where
46 MSG: 'static,
47{
48 fn map_msg<F, MSG2>(self, func: F) -> Attribute<MSG2>
49 where
50 F: Fn(MSG) -> MSG2 + 'static,
51 MSG2: 'static;
52
53 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Attribute<MSG2>
54 where
55 MSG2: 'static;
56}
57
58pub trait AttributeValueMapMsg<MSG>
59where
60 MSG: 'static,
61{
62 fn map_callback<MSG2>(
63 self,
64 cb: Listener<MSG, MSG2>,
65 ) -> AttributeValue<MSG2>
66 where
67 MSG2: 'static;
68}
69
70
71impl<MSG> NodeMapMsg<MSG> for Node<MSG>
72where
73 MSG: 'static,
74{
75 fn map_msg<F, MSG2>(self, func: F) -> Node<MSG2>
76 where
77 F: Fn(MSG) -> MSG2 + 'static,
78 MSG2: 'static,
79 {
80 let cb = Listener::from(func);
81 self.map_callback(cb)
82 }
83
84 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Node<MSG2>
85 where
86 MSG2: 'static,
87 {
88 match self {
89 Node::Element(element) => Node::Element(element.map_callback(cb)),
90 Node::Fragment(frg) => Node::Fragment(frg.into_iter().map(|child| child.map_callback(cb.clone())).collect()),
91 Node::Text(text) => Node::Text(text.map_callback(cb)),
92 Node::Comment(comment) => Node::Comment(comment.map_callback(cb)),
93 Node::DocType(doctype) => Node::DocType(doctype.map_callback(cb)),
94 }
95 }
96}
97
98impl<MSG> ElementMapMsg<MSG> for Element<MSG>
99where
100 MSG: 'static,
101{
102 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Element<MSG2>
103 where
104 MSG2: 'static,
105 {
106 Element {
107 namespace: self.namespace,
108 tag: self.tag,
109 props: self
110 .props
111 .into_iter()
112 .map(|attr| attr.map_callback(cb.clone()))
113 .collect(),
114 children: self
115 .children
116 .into_iter()
117 .map(|child| child.map_callback(cb.clone()))
118 .collect()
119 }
120 }
121}
122
123impl<MSG> AttributeMapMsg<MSG> for Attribute<MSG>
124where
125 MSG: 'static,
126{
127 fn map_msg<F, MSG2>(self, func: F) -> Attribute<MSG2>
128 where
129 F: Fn(MSG) -> MSG2 + 'static,
130 MSG2: 'static,
131 {
132 let cb = Listener::from(func);
133 self.map_callback(cb)
134 }
135
136 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Attribute<MSG2>
137 where
138 MSG2: 'static,
139 {
140 Attribute {
141 name: self.name,
142 value: self
143 .value
144 .into_iter()
145 .map(|v| v.map_callback(cb.clone()))
146 .collect(),
147 namespace: self.namespace,
148 }
149 }
150}
151
152impl<MSG> AttributeValueMapMsg<MSG> for AttributeValue<MSG>
153where
154 MSG: 'static,
155{
156 fn map_callback<MSG2>(
157 self,
158 cb: Listener<MSG, MSG2>,
159 ) -> AttributeValue<MSG2>
160 where
161 MSG2: 'static,
162 {
163 match self {
164 AttributeValue::FunctionCall(this) => {
165 AttributeValue::FunctionCall(this)
166 }
167 AttributeValue::Simple(this) => AttributeValue::Simple(this),
168 AttributeValue::Style(this) => AttributeValue::Style(this),
169 AttributeValue::EventListener(this) => {
170 AttributeValue::EventListener(this.map_callback(cb))
171 }
172 AttributeValue::Empty => AttributeValue::Empty,
173 }
174 }
175}
176
177impl<MSG> LeafMapMsg<MSG> for Leaf<MSG>
178where
179 MSG: 'static,
180{
181 fn map_callback<MSG2>(self, cb: Listener<MSG, MSG2>) -> Leaf<MSG2>
182 where
183 MSG2: 'static,
184 {
185 Leaf {
186 namespace: self.namespace,
187 tag: self.tag,
188 props: self
189 .props
190 .into_iter()
191 .map(|attr| attr.map_callback(cb.clone()))
192 .collect(),
193 value: self.value
194 }
195 }
196}