sauron_core/vdom/attribute/
style.rs

1use crate::vdom::Value;
2use std::borrow::Cow;
3use std::fmt;
4
5/// css styles
6/// style can be converted into an attribute
7#[derive(Debug, Clone, PartialEq)]
8pub struct Style {
9    /// style name such as border, width, etc
10    pub name: Cow<'static, str>,
11    /// value of the style
12    pub value: Value,
13}
14
15impl Style {
16    /// create a style with name and value
17    pub fn new(name: impl Into<Cow<'static, str>>, value: impl Into<Value>) -> Self {
18        Style {
19            name: name.into(),
20            value: value.into(),
21        }
22    }
23
24    pub(crate) fn merge_to_string<'a>(
25        styles: impl IntoIterator<Item = &'a Self>,
26    ) -> Option<String> {
27        let stringed = styles
28            .into_iter()
29            .map(|s| format!("{s};"))
30            .collect::<Vec<_>>();
31        if !stringed.is_empty() {
32            let joined = stringed.join("");
33            Some(joined)
34        } else {
35            None
36        }
37    }
38}
39
40impl fmt::Display for Style {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(f, "{}:{}", self.name, self.value)
43    }
44}