euv_core/vdom/cast/impl.rs
1use crate::*;
2
3/// Converts a `VirtualNode` into itself via `IntoNode`.
4impl IntoNode for VirtualNode {
5 /// Returns this virtual node as-is.
6 ///
7 /// # Returns
8 ///
9 /// - `VirtualNode` - This same virtual node.
10 fn into_node(self) -> VirtualNode {
11 self
12 }
13}
14
15/// Converts a `Vec<VirtualNode>` into a `VirtualNode::Fragment` via `IntoNode`.
16///
17/// This enables using a `Vec<VirtualNode>` directly in the `html!` macro
18/// without manually wrapping it in `VirtualNode::Fragment(...)`.
19///
20/// # Returns
21///
22/// - `VirtualNode` - A `VirtualNode::Fragment` containing the nodes, or
23/// `VirtualNode::Empty` if the vector is empty.
24impl IntoNode for Vec<VirtualNode> {
25 fn into_node(self) -> VirtualNode {
26 if self.is_empty() {
27 VirtualNode::Empty
28 } else {
29 VirtualNode::Fragment(self)
30 }
31 }
32}
33
34/// Converts an `Option<VirtualNode>` into a `VirtualNode` via `IntoNode`.
35///
36/// `Some(node)` returns the inner node, `None` returns `VirtualNode::Empty`.
37///
38/// # Returns
39///
40/// - `VirtualNode` - The inner node if `Some`, otherwise `VirtualNode::Empty`.
41impl IntoNode for Option<VirtualNode> {
42 fn into_node(self) -> VirtualNode {
43 match self {
44 Some(node) => node,
45 None => VirtualNode::Empty,
46 }
47 }
48}
49
50/// Converts an `Option<Vec<VirtualNode>>` into a `VirtualNode` via `IntoNode`.
51///
52/// `Some(vec)` converts the vector into a `VirtualNode::Fragment` (or `Empty`
53/// if the vector is empty), `None` returns `VirtualNode::Empty`.
54///
55/// # Returns
56///
57/// - `VirtualNode` - A `VirtualNode::Fragment` if `Some` with nodes,
58/// `VirtualNode::Empty` if `None` or the vector is empty.
59impl IntoNode for Option<Vec<VirtualNode>> {
60 fn into_node(self) -> VirtualNode {
61 match self {
62 Some(nodes) => nodes.into_node(),
63 None => VirtualNode::Empty,
64 }
65 }
66}
67
68/// Wraps a `FnMut() -> VirtualNode` closure into a `DynamicNode` via `IntoNode`.
69///
70/// This enables writing `{move || html! { ... }}` directly in HTML markup
71/// without explicit `DynamicNode` construction.
72impl<F> IntoNode for F
73where
74 F: FnMut() -> VirtualNode + 'static,
75{
76 /// Wraps this closure into a `VirtualNode::Dynamic` with a fresh hook context.
77 ///
78 /// # Returns
79 ///
80 /// - `VirtualNode` - A dynamic virtual node wrapping this closure.
81 fn into_node(self) -> VirtualNode {
82 let render_inner: Rc<RefCell<RenderFnInner>> =
83 Rc::new(RefCell::new(RenderFnInner::new(Box::new(self))));
84 VirtualNode::Dynamic(DynamicNode::new(
85 render_inner,
86 crate::reactive::create_hook_context(),
87 ))
88 }
89}
90
91/// Converts a `String` into a text virtual node via `IntoNode`.
92impl IntoNode for String {
93 /// Converts this string into a text virtual node.
94 ///
95 /// # Returns
96 ///
97 /// - `VirtualNode` - A text virtual node.
98 fn into_node(self) -> VirtualNode {
99 VirtualNode::Text(TextNode::new(self, None))
100 }
101}
102
103/// Converts a `&str` into a text virtual node via `IntoNode`.
104impl IntoNode for &str {
105 /// Converts this string slice into a text virtual node.
106 ///
107 /// # Returns
108 ///
109 /// - `VirtualNode` - A text virtual node.
110 fn into_node(self) -> VirtualNode {
111 VirtualNode::Text(TextNode::new(self.to_string(), None))
112 }
113}
114
115/// Converts an `i32` into a text virtual node via `IntoNode`.
116impl IntoNode for i32 {
117 /// Converts this integer into a text virtual node.
118 ///
119 /// # Returns
120 ///
121 /// - `VirtualNode` - A text virtual node.
122 fn into_node(self) -> VirtualNode {
123 VirtualNode::Text(TextNode::new(self.to_string(), None))
124 }
125}
126
127/// Converts a `usize` into a text virtual node via `IntoNode`.
128impl IntoNode for usize {
129 /// Converts this unsigned integer into a text virtual node.
130 ///
131 /// # Returns
132 ///
133 /// - `VirtualNode` - A text virtual node.
134 fn into_node(self) -> VirtualNode {
135 VirtualNode::Text(TextNode::new(self.to_string(), None))
136 }
137}
138
139/// Converts a `bool` into a text virtual node via `IntoNode`.
140impl IntoNode for bool {
141 /// Converts this boolean into a text virtual node.
142 ///
143 /// # Returns
144 ///
145 /// - `VirtualNode` - A text virtual node.
146 fn into_node(self) -> VirtualNode {
147 VirtualNode::Text(TextNode::new(self.to_string(), None))
148 }
149}
150
151/// Converts a signal into a reactive text virtual node via `IntoNode`.
152impl<T> IntoNode for Signal<T>
153where
154 T: Clone + PartialEq + Display + 'static,
155{
156 /// Converts this signal into a reactive text virtual node.
157 ///
158 /// # Returns
159 ///
160 /// - `VirtualNode` - A reactive text virtual node.
161 fn into_node(self) -> VirtualNode {
162 self.as_reactive_text()
163 }
164}
165
166/// Converts a signal into a reactive text node with listener wiring.
167impl<T> AsReactiveText for Signal<T>
168where
169 T: Clone + PartialEq + Display + 'static,
170{
171 /// Creates a reactive text node that auto-updates when the signal changes.
172 ///
173 /// Internally creates a bridge `Signal<String>` that subscribes to the
174 /// source signal and updates the text content on every change.
175 ///
176 /// # Returns
177 ///
178 /// - `VirtualNode` - A text virtual node with reactive signal binding.
179 fn as_reactive_text(&self) -> VirtualNode {
180 let initial: String = self.get().to_string();
181 let string_signal: Signal<String> = Signal::create(initial.clone());
182 let string_signal_clone: Signal<String> = string_signal;
183 self.replace_subscribe({
184 let source: Signal<T> = *self;
185 move || {
186 let new_value: String = source.get().to_string();
187 string_signal_clone.set_silent(new_value);
188 }
189 });
190 VirtualNode::Text(TextNode::new(initial, Some(string_signal)))
191 }
192}
193
194/// Constructs an `EventAdapter` that wraps any event-compatible value.
195impl<T> EventAdapter<T> {
196 /// Returns the inner wrapped value, consuming the adapter.
197 ///
198 /// # Returns
199 ///
200 /// - `T` - The inner value.
201 pub(crate) fn into_inner(self) -> T {
202 self.inner
203 }
204}
205
206/// Adapts a `FnMut(Event)` closure into an `AttributeValue::Event`.
207///
208/// Wraps the closure into a `NativeEventHandler` and returns it as an
209/// event attribute value. This replaces the `__EventWrapper<F>` type
210/// that was previously generated inline by the `html!` macro.
211impl<F> EventAdapter<F>
212where
213 F: FnMut(Event) + 'static,
214{
215 /// Converts the wrapped closure into an event `AttributeValue`.
216 ///
217 /// # Arguments
218 ///
219 /// - `&'static str` - The event name string to associate with the handler.
220 ///
221 /// # Returns
222 ///
223 /// - `AttributeValue` - An `AttributeValue::Event` wrapping the handler.
224 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
225 AttributeValue::Event(NativeEventHandler::create(event_name, self.into_inner()))
226 }
227}
228
229/// Adapts an owned `NativeEventHandler` into an `AttributeValue::Event` directly.
230///
231/// When the user already provides a `NativeEventHandler`, the handler is
232/// re-wrapped with the given `event_name` to ensure the DOM event listener
233/// is bound to the correct event type (e.g., "click" rather than "onclick").
234impl EventAdapter<NativeEventHandler> {
235 /// Converts the wrapped handler into an event `AttributeValue`.
236 ///
237 /// Re-wraps the handler with the provided `event_name` so that the
238 /// DOM event listener uses the correct event type string.
239 ///
240 /// # Arguments
241 ///
242 /// - `&'static str` - The event name to bind the handler to.
243 ///
244 /// # Returns
245 ///
246 /// - `AttributeValue` - An `AttributeValue::Event` containing the re-wrapped handler.
247 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
248 let mut handler: NativeEventHandler = self.into_inner();
249 handler.set_event_name(event_name);
250 AttributeValue::Event(handler)
251 }
252}
253
254/// Adapts an `Option<NativeEventHandler>` into an `AttributeValue`.
255///
256/// `Some(handler)` becomes `AttributeValue::Event(handler)` re-wrapped with the
257/// given event name, and `None` becomes `AttributeValue::Text(String::new())`.
258impl EventAdapter<Option<NativeEventHandler>> {
259 /// Converts the wrapped optional handler into an attribute value.
260 ///
261 /// Re-wraps a `Some` handler with the provided `event_name` so that the
262 /// DOM event listener uses the correct event type string.
263 ///
264 /// # Arguments
265 ///
266 /// - `&'static str` - The event name to bind the handler to.
267 ///
268 /// # Returns
269 ///
270 /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
271 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
272 match self.into_inner() {
273 Some(handler) => EventAdapter::new(handler).into_attribute(event_name),
274 None => AttributeValue::Text(String::new()),
275 }
276 }
277}
278
279/// Adapts an `Option<Rc<dyn Fn(Event)>>` into an `AttributeValue`.
280///
281/// `Some(callback)` becomes `AttributeValue::Event` by wrapping the shared closure
282/// into a `NativeEventHandler`, and `None` becomes `AttributeValue::Text(String::new())`.
283/// This supports component Props that use `Option<Rc<dyn Fn(Event)>>` for event callbacks.
284impl EventAdapter<Option<Rc<dyn Fn(Event)>>> {
285 /// Converts the wrapped optional shared closure into an attribute value.
286 ///
287 /// # Arguments
288 ///
289 /// - `&'static str` - The event name to bind the handler to.
290 ///
291 /// # Returns
292 ///
293 /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
294 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
295 match self.into_inner() {
296 Some(callback) => {
297 let wrapper = move |event: Event| {
298 callback(event);
299 };
300 AttributeValue::Event(NativeEventHandler::create(event_name, wrapper))
301 }
302 None => AttributeValue::Text(String::new()),
303 }
304 }
305}
306
307/// Constructs an `AttrValueAdapter` that wraps any attribute-compatible value.
308impl<T> AttrValueAdapter<T> {
309 /// Returns the inner wrapped value, consuming the adapter.
310 ///
311 /// # Returns
312 ///
313 /// - `T` - The inner value.
314 pub(crate) fn into_inner(self) -> T {
315 self.inner
316 }
317}
318
319/// Adapts a `FnMut(Event)` closure into a callback `AttributeValue`.
320///
321/// This handles the case where a closure is used as a component callback prop.
322/// The closure is converted via `IntoCallbackAttribute::into_callback_attribute()`.
323impl<F> AttrValueAdapter<F>
324where
325 F: FnMut(Event) + 'static,
326{
327 /// Converts the wrapped closure into a callback `AttributeValue`.
328 ///
329 /// # Returns
330 ///
331 /// - `AttributeValue` - An event attribute value wrapping the adapted closure.
332 pub fn into_callback_attribute_value(self) -> AttributeValue {
333 self.into_inner().into_callback_attribute()
334 }
335
336 /// Converts the wrapped closure into a callback `AttributeValue` with a
337 /// custom event name for component props.
338 ///
339 /// # Arguments
340 ///
341 /// - `&'static str` - The custom attribute name (e.g., "on-increment", "on-change").
342 ///
343 /// # Returns
344 ///
345 /// - `AttributeValue` - An event attribute value with the custom name.
346 pub fn into_callback_attribute_value_with_name(self, name: &'static str) -> AttributeValue {
347 AttributeValue::Event(NativeEventHandler::create(name, self.into_inner()))
348 }
349}
350
351/// Adapts an owned `NativeEventHandler` into an `AttributeValue::Event` directly.
352impl AttrValueAdapter<NativeEventHandler> {
353 /// Converts the wrapped handler into an event `AttributeValue`.
354 ///
355 /// # Returns
356 ///
357 /// - `AttributeValue` - An `AttributeValue::Event` containing the re-wrapped handler.
358 pub fn into_callback_attribute_value(self) -> AttributeValue {
359 AttributeValue::Event(self.into_inner())
360 }
361
362 /// Converts the wrapped handler into a callback `AttributeValue` with a
363 /// custom event name for component props.
364 ///
365 /// # Arguments
366 ///
367 /// - `&'static str` - The custom attribute name.
368 ///
369 /// # Returns
370 ///
371 /// - `AttributeValue` - An event attribute value with the custom name.
372 pub fn into_callback_attribute_value_with_name(self, name: &'static str) -> AttributeValue {
373 let mut handler: NativeEventHandler = self.into_inner();
374 handler.set_event_name(name);
375 AttributeValue::Event(handler)
376 }
377}
378
379/// Adapts an `Option<NativeEventHandler>` into an `AttributeValue`.
380impl AttrValueAdapter<Option<NativeEventHandler>> {
381 /// Converts the wrapped optional handler into an attribute value.
382 ///
383 /// # Returns
384 ///
385 /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
386 pub fn into_callback_attribute_value(self) -> AttributeValue {
387 match self.into_inner() {
388 Some(handler) => AttrValueAdapter::new(handler).into_callback_attribute_value(),
389 None => AttributeValue::Text(String::new()),
390 }
391 }
392
393 /// Converts this optional handler into a callback `AttributeValue` with a
394 /// custom event name for component props.
395 ///
396 /// # Arguments
397 ///
398 /// - `&'static str` - The custom attribute name.
399 ///
400 /// # Returns
401 ///
402 /// - `AttributeValue` - An event attribute with the custom name if `Some`,
403 /// otherwise an empty text attribute.
404 pub fn into_callback_attribute_value_with_name(self, name: &'static str) -> AttributeValue {
405 match self.into_inner() {
406 Some(handler) => {
407 AttrValueAdapter::new(handler).into_callback_attribute_value_with_name(name)
408 }
409 None => AttributeValue::Text(String::new()),
410 }
411 }
412}
413
414/// Adapts any `IntoReactiveValue` type into an `AttributeValue`.
415///
416/// This is the fallback path for non-closure attribute values (strings, signals,
417/// CSS classes, etc.). The value is converted via `IntoReactiveValue::into_reactive_value()`.
418impl<T> AttrValueAdapter<T>
419where
420 T: IntoReactiveValue,
421{
422 /// Converts the wrapped value into an `AttributeValue` via reactive value adaptation.
423 ///
424 /// # Returns
425 ///
426 /// - `AttributeValue` - The reactive attribute value.
427 pub fn into_reactive_attribute_value(self) -> AttributeValue {
428 self.into_inner().into_reactive_value()
429 }
430}