yew_style/
style.rs

1use std::{
2    fmt::{self, Display},
3    ops::Deref,
4};
5
6use indexmap::IndexMap;
7use yew::{html::IntoPropValue, AttrValue};
8
9fn style_map_to_string(map: &IndexMap<String, Option<String>>) -> String {
10    map.iter()
11        .filter_map(|(key, value)| {
12            value
13                .as_ref()
14                .and_then(|value| (!value.is_empty()).then_some(format!("{key}: {value};")))
15        })
16        .collect::<Vec<_>>()
17        .join(" ")
18}
19
20#[derive(Clone, Debug, PartialEq)]
21pub enum InnerStyle {
22    String(String),
23    Structured(IndexMap<String, Option<String>>),
24}
25
26impl InnerStyle {
27    pub fn with_defaults<I: Into<InnerStyle>>(self, defaults: I) -> Self {
28        let defaults: InnerStyle = defaults.into();
29
30        match (self, defaults) {
31            (Self::String(string), Self::String(default_string)) => {
32                Self::String(format!("{default_string} {string}"))
33            }
34            (Self::String(string), Self::Structured(default_map)) => {
35                Self::String(format!("{} {}", style_map_to_string(&default_map), string))
36            }
37            (Self::Structured(map), Self::String(default_string)) => {
38                Self::String(format!("{} {}", default_string, style_map_to_string(&map)))
39            }
40            (Self::Structured(map), Self::Structured(default_map)) => {
41                InnerStyle::Structured(default_map.into_iter().chain(map).collect())
42            }
43        }
44    }
45}
46
47impl Display for InnerStyle {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            Self::String(string) => write!(f, "{}", string),
51            Self::Structured(map) => write!(f, "{}", style_map_to_string(map),),
52        }
53    }
54}
55
56#[derive(Clone, Debug, Default, PartialEq)]
57pub struct Style(pub Option<InnerStyle>);
58
59impl Style {
60    pub fn new() -> Self {
61        Self::default()
62    }
63
64    pub fn with_defaults<I: Into<Self>>(self, defaults: I) -> Self {
65        let defaults: Self = defaults.into();
66
67        Style(match (self.0, defaults.0) {
68            (Some(style), Some(defaults)) => Some(style.with_defaults(defaults)),
69            (Some(style), None) => Some(style),
70            (None, Some(defaults)) => Some(defaults),
71            (None, None) => None,
72        })
73    }
74}
75
76impl Deref for Style {
77    type Target = Option<InnerStyle>;
78
79    fn deref(&self) -> &Self::Target {
80        &self.0
81    }
82}
83
84impl Display for Style {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        write!(
87            f,
88            "{}",
89            self.0
90                .as_ref()
91                .map(|inner_style| inner_style.to_string())
92                .unwrap_or_default(),
93        )
94    }
95}
96
97impl From<Option<&str>> for Style {
98    fn from(value: Option<&str>) -> Style {
99        Style(value.map(|value| InnerStyle::String(value.to_string())))
100    }
101}
102
103impl From<Option<String>> for Style {
104    fn from(value: Option<String>) -> Style {
105        Style(value.map(InnerStyle::String))
106    }
107}
108
109impl From<&str> for Style {
110    fn from(value: &str) -> Style {
111        Style(Some(InnerStyle::String(value.to_string())))
112    }
113}
114
115impl From<String> for Style {
116    fn from(value: String) -> Style {
117        Style(Some(InnerStyle::String(value)))
118    }
119}
120
121impl From<IndexMap<String, Option<String>>> for Style {
122    fn from(value: IndexMap<String, Option<String>>) -> Style {
123        Style(Some(InnerStyle::Structured(value)))
124    }
125}
126
127impl From<IndexMap<String, String>> for Style {
128    fn from(value: IndexMap<String, String>) -> Style {
129        Style(Some(InnerStyle::Structured(
130            value
131                .into_iter()
132                .map(|(key, value)| (key, Some(value)))
133                .collect(),
134        )))
135    }
136}
137
138impl<const N: usize> From<[(&str, Option<&str>); N]> for Style {
139    fn from(value: [(&str, Option<&str>); N]) -> Style {
140        Style(Some(InnerStyle::Structured(IndexMap::from_iter(
141            value.map(|(key, value)| (key.to_string(), value.map(|value| value.to_string()))),
142        ))))
143    }
144}
145
146impl<const N: usize> From<[(&str, &str); N]> for Style {
147    fn from(value: [(&str, &str); N]) -> Style {
148        Style(Some(InnerStyle::Structured(IndexMap::from_iter(
149            value.map(|(key, value)| (key.to_string(), Some(value.to_string()))),
150        ))))
151    }
152}
153
154impl<const N: usize> From<[(&str, Option<String>); N]> for Style {
155    fn from(value: [(&str, Option<String>); N]) -> Style {
156        Style(Some(InnerStyle::Structured(IndexMap::from_iter(
157            value.map(|(key, value)| (key.to_string(), value)),
158        ))))
159    }
160}
161
162impl<const N: usize> From<[(&str, String); N]> for Style {
163    fn from(value: [(&str, String); N]) -> Style {
164        Style(Some(InnerStyle::Structured(IndexMap::from_iter(
165            value.map(|(key, value)| (key.to_string(), Some(value))),
166        ))))
167    }
168}
169
170impl<const N: usize> From<[(String, Option<String>); N]> for Style {
171    fn from(value: [(String, Option<String>); N]) -> Style {
172        Style(Some(InnerStyle::Structured(IndexMap::from_iter(value))))
173    }
174}
175
176impl<const N: usize> From<[(String, String); N]> for Style {
177    fn from(value: [(String, String); N]) -> Style {
178        Style(Some(InnerStyle::Structured(IndexMap::from_iter(
179            value.map(|(key, value)| (key, Some(value))),
180        ))))
181    }
182}
183
184impl IntoPropValue<Style> for IndexMap<String, Option<String>> {
185    fn into_prop_value(self) -> Style {
186        self.into()
187    }
188}
189
190impl IntoPropValue<Style> for IndexMap<String, String> {
191    fn into_prop_value(self) -> Style {
192        self.into()
193    }
194}
195
196impl<const N: usize> IntoPropValue<Style> for [(&str, Option<&str>); N] {
197    fn into_prop_value(self) -> Style {
198        self.into()
199    }
200}
201
202impl<const N: usize> IntoPropValue<Style> for [(&str, &str); N] {
203    fn into_prop_value(self) -> Style {
204        self.into()
205    }
206}
207
208impl<const N: usize> IntoPropValue<Style> for [(&str, Option<String>); N] {
209    fn into_prop_value(self) -> Style {
210        self.into()
211    }
212}
213
214impl<const N: usize> IntoPropValue<Style> for [(&str, String); N] {
215    fn into_prop_value(self) -> Style {
216        self.into()
217    }
218}
219
220impl<const N: usize> IntoPropValue<Style> for [(String, Option<String>); N] {
221    fn into_prop_value(self) -> Style {
222        self.into()
223    }
224}
225
226impl<const N: usize> IntoPropValue<Style> for [(String, String); N] {
227    fn into_prop_value(self) -> Style {
228        self.into()
229    }
230}
231
232impl From<&InnerStyle> for AttrValue {
233    fn from(value: &InnerStyle) -> Self {
234        AttrValue::from(value.to_string())
235    }
236}
237
238impl From<&Style> for AttrValue {
239    fn from(value: &Style) -> Self {
240        AttrValue::from(value.to_string())
241    }
242}
243
244impl From<InnerStyle> for AttrValue {
245    fn from(value: InnerStyle) -> Self {
246        AttrValue::from(value.to_string())
247    }
248}
249
250impl From<Style> for AttrValue {
251    fn from(value: Style) -> Self {
252        AttrValue::from(value.to_string())
253    }
254}
255
256impl IntoPropValue<Option<AttrValue>> for Style {
257    fn into_prop_value(self) -> Option<AttrValue> {
258        self.as_ref().map(|value| value.into())
259    }
260}
261
262impl IntoPropValue<AttrValue> for Style {
263    fn into_prop_value(self) -> AttrValue {
264        self.into()
265    }
266}
267
268#[cfg(test)]
269mod tests {
270    use super::*;
271
272    #[test]
273    fn test_to_string() {
274        assert_eq!("", Style::default().to_string());
275
276        assert_eq!("", Style::from(None::<String>).to_string());
277
278        assert_eq!(
279            "margin: 1rem; padding: 0.5rem;",
280            Style::from(Some("margin: 1rem; padding: 0.5rem;")).to_string(),
281        );
282
283        assert_eq!(
284            "margin: 1rem; padding: 0.5rem;",
285            Style::from("margin: 1rem; padding: 0.5rem;").to_string(),
286        );
287
288        assert_eq!(
289            "color: white; border: 1px solid black;",
290            Style::from([
291                ("color", Some("white")),
292                ("background-color", None),
293                ("border", Some("1px solid black")),
294            ])
295            .to_string()
296        );
297
298        assert_eq!(
299            "color: white; background-color: gray; border: 1px solid black;",
300            Style::from([
301                ("color", "white"),
302                ("background-color", "gray"),
303                ("border", "1px solid black"),
304            ])
305            .to_string()
306        );
307    }
308
309    #[test]
310    fn test_with_defaults() {
311        // String with string defaults
312        assert_eq!(
313            Style::from("pointer-events: none; color: red;"),
314            Style::from("color: red;").with_defaults("pointer-events: none;"),
315        );
316        assert_eq!(
317            Style::from("color: blue; color: red;"),
318            Style::from("color: red;").with_defaults("color: blue;"),
319        );
320
321        // String with structured defaults
322        assert_eq!(
323            Style::from("pointer-events: none; color: red;"),
324            Style::from("color: red;").with_defaults([("pointer-events", "none")]),
325        );
326        assert_eq!(
327            Style::from("color: blue; color: red;"),
328            Style::from("color: red;").with_defaults([("color", "blue")]),
329        );
330
331        // Structured with string defaults
332        assert_eq!(
333            Style::from("pointer-events: none; color: red;"),
334            Style::from([("color", "red")]).with_defaults("pointer-events: none;"),
335        );
336        assert_eq!(
337            Style::from("color: blue; color: red;"),
338            Style::from([("color", "red")]).with_defaults("color: blue;"),
339        );
340
341        // Structured with structured defaults
342        assert_eq!(
343            Style::from([("pointer-events", "none"), ("color", "red")]),
344            Style::from([("color", "red")]).with_defaults([("pointer-events", "none")]),
345        );
346        assert_eq!(
347            Style::from([("color", "red")]),
348            Style::from([("color", "red")]).with_defaults([("color", "blue")]),
349        );
350    }
351}