Skip to main content

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_fn_inner: Rc<RefCell<RenderFnInner>> =
83            Rc::new(RefCell::new(RenderFnInner::new(Box::new(self))));
84        VirtualNode::Dynamic(DynamicNode::new(
85            render_fn_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                string_signal_clone.set_silent(source.get().to_string());
187            }
188        });
189        VirtualNode::Text(TextNode::new(initial, Some(string_signal)))
190    }
191}
192
193/// Constructs an `EventAdapter` that wraps any event-compatible value.
194impl<T> EventAdapter<T> {
195    /// Returns the inner wrapped value, consuming the adapter.
196    ///
197    /// # Returns
198    ///
199    /// - `T` - The inner value.
200    pub(crate) fn into_inner(self) -> T {
201        self.inner
202    }
203}
204
205/// Adapts a `FnMut(Event)` closure into an `AttributeValue::Event`.
206///
207/// Wraps the closure into a `NativeEventHandler` and returns it as an
208/// event attribute value. This replaces the `__EventWrapper<F>` type
209/// that was previously generated inline by the `html!` macro.
210impl<F> EventAdapter<F>
211where
212    F: FnMut(Event) + 'static,
213{
214    /// Converts the wrapped closure into an event `AttributeValue`.
215    ///
216    /// # Arguments
217    ///
218    /// - `&'static str` - The event name string to associate with the handler.
219    ///
220    /// # Returns
221    ///
222    /// - `AttributeValue` - An `AttributeValue::Event` wrapping the handler.
223    pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
224        AttributeValue::Event(NativeEventHandler::create(event_name, self.into_inner()))
225    }
226}
227
228/// Adapts an owned `NativeEventHandler` into an `AttributeValue::Event` directly.
229///
230/// When the user already provides a `NativeEventHandler`, the handler is
231/// re-wrapped with the given `event_name` to ensure the DOM event listener
232/// is bound to the correct event type (e.g., "click" rather than "onclick").
233impl EventAdapter<NativeEventHandler> {
234    /// Converts the wrapped handler into an event `AttributeValue`.
235    ///
236    /// Re-wraps the handler with the provided `event_name` so that the
237    /// DOM event listener uses the correct event type string.
238    ///
239    /// # Arguments
240    ///
241    /// - `&'static str` - The event name to bind the handler to.
242    ///
243    /// # Returns
244    ///
245    /// - `AttributeValue` - An `AttributeValue::Event` containing the re-wrapped handler.
246    pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
247        let mut handler: NativeEventHandler = self.into_inner();
248        handler.set_event_name(event_name);
249        AttributeValue::Event(handler)
250    }
251}
252
253/// Adapts an `Option<NativeEventHandler>` into an `AttributeValue`.
254///
255/// `Some(handler)` becomes `AttributeValue::Event(handler)` re-wrapped with the
256/// given event name, and `None` becomes `AttributeValue::Text(String::new())`.
257impl EventAdapter<Option<NativeEventHandler>> {
258    /// Converts the wrapped optional handler into an attribute value.
259    ///
260    /// Re-wraps a `Some` handler with the provided `event_name` so that the
261    /// DOM event listener uses the correct event type string.
262    ///
263    /// # Arguments
264    ///
265    /// - `&'static str` - The event name to bind the handler to.
266    ///
267    /// # Returns
268    ///
269    /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
270    pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
271        match self.into_inner() {
272            Some(handler) => EventAdapter::new(handler).into_attribute(event_name),
273            None => AttributeValue::Text(String::new()),
274        }
275    }
276}
277
278/// Adapts an `Option<Rc<dyn Fn(Event)>>` into an `AttributeValue`.
279///
280/// `Some(callback)` becomes `AttributeValue::Event` by wrapping the shared closure
281/// into a `NativeEventHandler`, and `None` becomes `AttributeValue::Text(String::new())`.
282/// This supports component Props that use `Option<Rc<dyn Fn(Event)>>` for event callbacks.
283impl EventAdapter<Option<Rc<dyn Fn(Event)>>> {
284    /// Converts the wrapped optional shared closure into an attribute value.
285    ///
286    /// # Arguments
287    ///
288    /// - `&'static str` - The event name to bind the handler to.
289    ///
290    /// # Returns
291    ///
292    /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
293    pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
294        match self.into_inner() {
295            Some(callback) => {
296                let wrapper = move |event: Event| {
297                    callback(event);
298                };
299                AttributeValue::Event(NativeEventHandler::create(event_name, wrapper))
300            }
301            None => AttributeValue::Text(String::new()),
302        }
303    }
304}
305
306/// Constructs an `AttrValueAdapter` that wraps any attribute-compatible value.
307impl<T> AttrValueAdapter<T> {
308    /// Returns the inner wrapped value, consuming the adapter.
309    ///
310    /// # Returns
311    ///
312    /// - `T` - The inner value.
313    pub(crate) fn into_inner(self) -> T {
314        self.inner
315    }
316}
317
318/// Adapts a `FnMut(Event)` closure into a callback `AttributeValue`.
319///
320/// This handles the case where a closure is used as a component callback prop.
321/// The closure is converted via `IntoCallbackAttribute::into_callback_attribute()`.
322impl<F> AttrValueAdapter<F>
323where
324    F: FnMut(Event) + 'static,
325{
326    /// Converts the wrapped closure into a callback `AttributeValue`.
327    ///
328    /// # Returns
329    ///
330    /// - `AttributeValue` - An event attribute value wrapping the adapted closure.
331    pub fn into_callback_attribute_value(self) -> AttributeValue {
332        self.into_inner().into_callback_attribute()
333    }
334
335    /// Converts the wrapped closure into a callback `AttributeValue` with a
336    /// custom event name for component props.
337    ///
338    /// # Arguments
339    ///
340    /// - `&'static str` - The custom attribute name (e.g., "on-increment", "on-change").
341    ///
342    /// # Returns
343    ///
344    /// - `AttributeValue` - An event attribute value with the custom name.
345    pub fn into_callback_attribute_value_with_name(self, name: &'static str) -> AttributeValue {
346        AttributeValue::Event(NativeEventHandler::create(name, self.into_inner()))
347    }
348}
349
350/// Adapts an owned `NativeEventHandler` into an `AttributeValue::Event` directly.
351impl AttrValueAdapter<NativeEventHandler> {
352    /// Converts the wrapped handler into an event `AttributeValue`.
353    ///
354    /// # Returns
355    ///
356    /// - `AttributeValue` - An `AttributeValue::Event` containing the re-wrapped handler.
357    pub fn into_callback_attribute_value(self) -> AttributeValue {
358        AttributeValue::Event(self.into_inner())
359    }
360
361    /// Converts the wrapped handler into a callback `AttributeValue` with a
362    /// custom event name for component props.
363    ///
364    /// # Arguments
365    ///
366    /// - `&'static str` - The custom attribute name.
367    ///
368    /// # Returns
369    ///
370    /// - `AttributeValue` - An event attribute value with the custom name.
371    pub fn into_callback_attribute_value_with_name(self, name: &'static str) -> AttributeValue {
372        let mut handler: NativeEventHandler = self.into_inner();
373        handler.set_event_name(name);
374        AttributeValue::Event(handler)
375    }
376}
377
378/// Adapts an `Option<NativeEventHandler>` into an `AttributeValue`.
379impl AttrValueAdapter<Option<NativeEventHandler>> {
380    /// Converts the wrapped optional handler into an attribute value.
381    ///
382    /// # Returns
383    ///
384    /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
385    pub fn into_callback_attribute_value(self) -> AttributeValue {
386        match self.into_inner() {
387            Some(handler) => AttrValueAdapter::new(handler).into_callback_attribute_value(),
388            None => AttributeValue::Text(String::new()),
389        }
390    }
391
392    /// Converts this optional handler into a callback `AttributeValue` with a
393    /// custom event name for component props.
394    ///
395    /// # Arguments
396    ///
397    /// - `&'static str` - The custom attribute name.
398    ///
399    /// # Returns
400    ///
401    /// - `AttributeValue` - An event attribute with the custom name if `Some`,
402    ///   otherwise an empty text attribute.
403    pub fn into_callback_attribute_value_with_name(self, name: &'static str) -> AttributeValue {
404        match self.into_inner() {
405            Some(handler) => {
406                AttrValueAdapter::new(handler).into_callback_attribute_value_with_name(name)
407            }
408            None => AttributeValue::Text(String::new()),
409        }
410    }
411}
412
413/// Adapts any `IntoReactiveValue` type into an `AttributeValue`.
414///
415/// This is the fallback path for non-closure attribute values (strings, signals,
416/// CSS classes, etc.). The value is converted via `IntoReactiveValue::into_reactive_value()`.
417impl<T> AttrValueAdapter<T>
418where
419    T: IntoReactiveValue,
420{
421    /// Converts the wrapped value into an `AttributeValue` via reactive value adaptation.
422    ///
423    /// # Returns
424    ///
425    /// - `AttributeValue` - The reactive attribute value.
426    pub fn into_reactive_attribute_value(self) -> AttributeValue {
427        self.into_inner().into_reactive_value()
428    }
429}