Skip to main content

euv_core/reactive/cast/
impl.rs

1use crate::*;
2
3/// Converts a static `String` into a text attribute value.
4impl IntoReactiveValue for String {
5    /// Converts this string into an `AttributeValue::Text`.
6    ///
7    /// # Returns
8    ///
9    /// - `AttributeValue` - A text attribute value containing this string.
10    fn into_reactive_value(self) -> AttributeValue {
11        AttributeValue::Text(self)
12    }
13}
14
15/// Converts a string slice into a text attribute value.
16impl IntoReactiveValue for &str {
17    /// Converts this string slice into an `AttributeValue::Text`.
18    ///
19    /// # Returns
20    ///
21    /// - `AttributeValue` - A text attribute value containing the owned string.
22    fn into_reactive_value(self) -> AttributeValue {
23        AttributeValue::Text(self.to_string())
24    }
25}
26
27/// Converts a string signal into a reactive attribute value.
28impl IntoReactiveValue for Signal<String> {
29    /// Converts this string signal into an `AttributeValue::Signal`.
30    ///
31    /// # Returns
32    ///
33    /// - `AttributeValue` - A signal-backed attribute value.
34    fn into_reactive_value(self) -> AttributeValue {
35        AttributeValue::Signal(self)
36    }
37}
38
39/// Converts a mutable bool signal into a reactive attribute value.
40///
41/// The signal is mapped to a `Signal<String>` that yields `"true"` or `"false"`,
42/// enabling boolean attributes like `checked` to reactively update the DOM.
43impl IntoReactiveValue for Signal<bool> {
44    /// Converts this bool signal into an `AttributeValue` via string mapping.
45    ///
46    /// # Returns
47    ///
48    /// - `AttributeValue` - A signal-backed attribute value yielding `"true"` or `"false"`.
49    fn into_reactive_value(self) -> AttributeValue {
50        bool_signal_to_string_attribute_value(self)
51    }
52}
53
54/// Converts a `bool` into a dynamic attribute value.
55///
56/// Stored as `AttributeValue::Dynamic("true"/"false")` so components can
57/// extract the original boolean via `try_get_typed_prop`.
58impl IntoReactiveValue for bool {
59    /// Converts this boolean into an `AttributeValue::Dynamic`.
60    ///
61    /// # Returns
62    ///
63    /// - `AttributeValue` - A dynamic attribute value containing the boolean string.
64    fn into_reactive_value(self) -> AttributeValue {
65        AttributeValue::Dynamic(self.to_string())
66    }
67}
68
69/// Converts an `i32` into a dynamic attribute value.
70///
71/// Stored as `AttributeValue::Dynamic` so components can extract the
72/// original integer via `try_get_typed_prop`.
73impl IntoReactiveValue for i32 {
74    /// Converts this integer into an `AttributeValue::Dynamic`.
75    ///
76    /// # Returns
77    ///
78    /// - `AttributeValue` - A dynamic attribute value containing the integer string.
79    fn into_reactive_value(self) -> AttributeValue {
80        AttributeValue::Dynamic(self.to_string())
81    }
82}
83
84/// Converts an `f64` into a dynamic attribute value.
85///
86/// Stored as `AttributeValue::Dynamic` so components can extract the
87/// original float via `try_get_typed_prop`.
88impl IntoReactiveValue for f64 {
89    /// Converts this float into an `AttributeValue::Dynamic`.
90    ///
91    /// # Returns
92    ///
93    /// - `AttributeValue` - A dynamic attribute value containing the float string.
94    fn into_reactive_value(self) -> AttributeValue {
95        AttributeValue::Dynamic(self.to_string())
96    }
97}
98
99/// Converts a CSS class reference into an attribute value.
100impl IntoReactiveValue for CssClass {
101    /// Converts this CSS class into an `AttributeValue::Css`.
102    ///
103    /// # Returns
104    ///
105    /// - `AttributeValue` - A CSS class attribute value.
106    fn into_reactive_value(self) -> AttributeValue {
107        AttributeValue::Css(self)
108    }
109}
110
111/// Converts a reference to a CSS class into an attribute value by cloning.
112impl IntoReactiveValue for &'static CssClass {
113    /// Converts this CSS class reference into an `AttributeValue::Css` by cloning.
114    ///
115    /// # Returns
116    ///
117    /// - `AttributeValue` - A CSS class attribute value.
118    fn into_reactive_value(self) -> AttributeValue {
119        AttributeValue::Css(self.clone())
120    }
121}
122
123/// Converts a `String` into its own value for reactive string storage.
124impl IntoReactiveString for String {
125    /// Returns this string as-is.
126    ///
127    /// # Returns
128    ///
129    /// - `String` - The same string.
130    fn into_reactive_string(self) -> String {
131        self
132    }
133}
134
135/// Converts a string slice into an owned string for reactive string storage.
136impl IntoReactiveString for &str {
137    /// Converts this string slice into an owned `String`.
138    ///
139    /// # Returns
140    ///
141    /// - `String` - The owned string.
142    fn into_reactive_string(self) -> String {
143        self.to_string()
144    }
145}
146
147/// Converts a `CssClass` into its class name for reactive string storage.
148impl IntoReactiveString for CssClass {
149    /// Returns the class name as an owned string.
150    ///
151    /// # Returns
152    ///
153    /// - `String` - The class name.
154    fn into_reactive_string(self) -> String {
155        self.get_name().to_string()
156    }
157}
158
159/// Converts a reference to a `CssClass` into its class name for reactive string storage.
160impl IntoReactiveString for &'static CssClass {
161    /// Returns the class name as an owned string.
162    ///
163    /// # Returns
164    ///
165    /// - `String` - The class name.
166    fn into_reactive_string(self) -> String {
167        self.get_name().to_string()
168    }
169}
170
171/// Converts a `bool` into `"true"` or `"false"` for reactive string storage.
172impl IntoReactiveString for bool {
173    /// Returns `"true"` or `"false"` as a string.
174    ///
175    /// # Returns
176    ///
177    /// - `String` - The boolean as a string.
178    fn into_reactive_string(self) -> String {
179        self.to_string()
180    }
181}
182
183/// Converts an `i32` into a string for reactive string storage.
184impl IntoReactiveString for i32 {
185    /// Converts this integer into its string representation.
186    ///
187    /// # Returns
188    ///
189    /// - `String` - The integer as a string.
190    fn into_reactive_string(self) -> String {
191        self.to_string()
192    }
193}
194
195/// Converts a `u32` into a string for reactive string storage.
196impl IntoReactiveString for u32 {
197    /// Converts this unsigned integer into its string representation.
198    ///
199    /// # Returns
200    ///
201    /// - `String` - The unsigned integer as a string.
202    fn into_reactive_string(self) -> String {
203        self.to_string()
204    }
205}
206
207/// Converts a `f64` into a string for reactive string storage.
208impl IntoReactiveString for f64 {
209    /// Converts this float into its string representation.
210    ///
211    /// # Returns
212    ///
213    /// - `String` - The float as a string.
214    fn into_reactive_string(self) -> String {
215        self.to_string()
216    }
217}
218
219/// Converts a string signal into a reactive string by resolving its current value.
220impl IntoReactiveString for Signal<String> {
221    /// Resolves the current value of this signal.
222    ///
223    /// # Returns
224    ///
225    /// - `String` - The current value of the signal.
226    fn into_reactive_string(self) -> String {
227        self.get()
228    }
229}
230
231/// Converts a bool signal into a reactive string by resolving its current value.
232impl IntoReactiveString for Signal<bool> {
233    /// Resolves the current value of this bool signal as a string.
234    ///
235    /// # Returns
236    ///
237    /// - `String` - The current boolean value as `"true"` or `"false"`.
238    fn into_reactive_string(self) -> String {
239        self.get().to_string()
240    }
241}
242
243/// Converts a closure into a callback attribute value.
244impl<F> IntoCallbackAttribute for F
245where
246    F: FnMut(NativeEvent) + 'static,
247{
248    /// Wraps this closure into an `AttributeValue::Event` with a generic "callback" event name.
249    ///
250    /// # Returns
251    ///
252    /// - `AttributeValue` - An event attribute value wrapping this closure.
253    fn into_callback_attribute(self) -> AttributeValue {
254        AttributeValue::Event(NativeEventHandler::new(
255            NativeEventName::Other("callback".to_string()),
256            self,
257        ))
258    }
259}
260
261/// Converts an owned event handler into a callback attribute value.
262///
263/// Re-wraps the handler with a generic "callback" event name so that
264/// subsequent `EventAdapter::into_attribute` calls can override it with
265/// the correct DOM event type.
266impl IntoCallbackAttribute for NativeEventHandler {
267    /// Converts this event handler into an `AttributeValue::Event`.
268    ///
269    /// # Returns
270    ///
271    /// - `AttributeValue` - An event attribute value with a generic callback event name.
272    fn into_callback_attribute(self) -> AttributeValue {
273        AttrValueAdapter::new(self).into_callback_attribute_value()
274    }
275}
276
277/// Converts an optional event handler into a callback attribute value.
278///
279/// Re-wraps a `Some` handler with a generic "callback" event name so that
280/// subsequent `EventAdapter::into_attribute` calls can override it with
281/// the correct DOM event type.
282impl IntoCallbackAttribute for Option<NativeEventHandler> {
283    /// Converts this optional handler into an `AttributeValue::Event` or `AttributeValue::Text` if `None`.
284    ///
285    /// # Returns
286    ///
287    /// - `AttributeValue` - An event attribute value if `Some`, otherwise an empty text attribute.
288    fn into_callback_attribute(self) -> AttributeValue {
289        AttrValueAdapter::new(self).into_callback_attribute_value()
290    }
291}