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 CSS class reference into an attribute value.
55impl IntoReactiveValue for CssClass {
56 /// Converts this CSS class into an `AttributeValue::Css`.
57 ///
58 /// # Returns
59 ///
60 /// - `AttributeValue` - A CSS class attribute value.
61 fn into_reactive_value(self) -> AttributeValue {
62 AttributeValue::Css(self)
63 }
64}
65
66/// Converts a reference to a CSS class into an attribute value by cloning.
67impl IntoReactiveValue for &'static CssClass {
68 /// Converts this CSS class reference into an `AttributeValue::Css` by cloning.
69 ///
70 /// # Returns
71 ///
72 /// - `AttributeValue` - A CSS class attribute value.
73 fn into_reactive_value(self) -> AttributeValue {
74 AttributeValue::Css(self.clone())
75 }
76}
77
78/// Converts a `String` into its own value for reactive string storage.
79impl IntoReactiveString for String {
80 /// Returns this string as-is.
81 ///
82 /// # Returns
83 ///
84 /// - `String` - The same string.
85 fn into_reactive_string(self) -> String {
86 self
87 }
88}
89
90/// Converts a string slice into an owned string for reactive string storage.
91impl IntoReactiveString for &str {
92 /// Converts this string slice into an owned `String`.
93 ///
94 /// # Returns
95 ///
96 /// - `String` - The owned string.
97 fn into_reactive_string(self) -> String {
98 self.to_string()
99 }
100}
101
102/// Converts a `CssClass` into its class name for reactive string storage.
103impl IntoReactiveString for CssClass {
104 /// Returns the class name as an owned string.
105 ///
106 /// # Returns
107 ///
108 /// - `String` - The class name.
109 fn into_reactive_string(self) -> String {
110 self.get_name().to_string()
111 }
112}
113
114/// Converts a reference to a `CssClass` into its class name for reactive string storage.
115impl IntoReactiveString for &'static CssClass {
116 /// Returns the class name as an owned string.
117 ///
118 /// # Returns
119 ///
120 /// - `String` - The class name.
121 fn into_reactive_string(self) -> String {
122 self.get_name().to_string()
123 }
124}
125
126/// Converts a `bool` into `"true"` or `"false"` for reactive string storage.
127impl IntoReactiveString for bool {
128 /// Returns `"true"` or `"false"` as a string.
129 ///
130 /// # Returns
131 ///
132 /// - `String` - The boolean as a string.
133 fn into_reactive_string(self) -> String {
134 self.to_string()
135 }
136}
137
138/// Converts an `i32` into a string for reactive string storage.
139impl IntoReactiveString for i32 {
140 /// Converts this integer into its string representation.
141 ///
142 /// # Returns
143 ///
144 /// - `String` - The integer as a string.
145 fn into_reactive_string(self) -> String {
146 self.to_string()
147 }
148}
149
150/// Converts a `u32` into a string for reactive string storage.
151impl IntoReactiveString for u32 {
152 /// Converts this unsigned integer into its string representation.
153 ///
154 /// # Returns
155 ///
156 /// - `String` - The unsigned integer as a string.
157 fn into_reactive_string(self) -> String {
158 self.to_string()
159 }
160}
161
162/// Converts a `f64` into a string for reactive string storage.
163impl IntoReactiveString for f64 {
164 /// Converts this float into its string representation.
165 ///
166 /// # Returns
167 ///
168 /// - `String` - The float as a string.
169 fn into_reactive_string(self) -> String {
170 self.to_string()
171 }
172}
173
174/// Converts a string signal into a reactive string by resolving its current value.
175impl IntoReactiveString for Signal<String> {
176 /// Resolves the current value of this signal.
177 ///
178 /// # Returns
179 ///
180 /// - `String` - The current value of the signal.
181 fn into_reactive_string(self) -> String {
182 self.get()
183 }
184}
185
186/// Converts a bool signal into a reactive string by resolving its current value.
187impl IntoReactiveString for Signal<bool> {
188 /// Resolves the current value of this bool signal as a string.
189 ///
190 /// # Returns
191 ///
192 /// - `String` - The current boolean value as `"true"` or `"false"`.
193 fn into_reactive_string(self) -> String {
194 self.get().to_string()
195 }
196}
197
198/// Converts a closure into a callback attribute value.
199impl<F> IntoCallbackAttribute for F
200where
201 F: FnMut(NativeEvent) + 'static,
202{
203 /// Wraps this closure into an `AttributeValue::Event` with a generic "callback" event name.
204 ///
205 /// # Returns
206 ///
207 /// - `AttributeValue` - An event attribute value wrapping this closure.
208 fn into_callback_attribute(self) -> AttributeValue {
209 AttributeValue::Event(NativeEventHandler::new(
210 NativeEventName::Other("callback".to_string()),
211 self,
212 ))
213 }
214}
215
216/// Converts an owned event handler into a callback attribute value.
217impl IntoCallbackAttribute for NativeEventHandler {
218 /// Converts this event handler into an `AttributeValue::Event`.
219 ///
220 /// # Returns
221 ///
222 /// - `AttributeValue` - An event attribute value.
223 fn into_callback_attribute(self) -> AttributeValue {
224 AttributeValue::Event(self)
225 }
226}
227
228/// Converts an optional event handler into a callback attribute value.
229impl IntoCallbackAttribute for Option<NativeEventHandler> {
230 /// Converts this optional handler into an `AttributeValue::Event` or `AttributeValue::Text` if `None`.
231 ///
232 /// # Returns
233 ///
234 /// - `AttributeValue` - An event attribute value if `Some`, otherwise an empty text attribute.
235 fn into_callback_attribute(self) -> AttributeValue {
236 match self {
237 Some(handler) => AttributeValue::Event(handler),
238 None => AttributeValue::Text(String::new()),
239 }
240 }
241}