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