mew_css/
variable.rs

1use std::fmt;
2
3/// A CSS custom property reference.
4///
5/// This struct represents a reference to a CSS variable used in property
6/// values. It simply stores the variable name and formats it as
7/// `var(--name)` when displayed.
8#[derive(Debug, Clone, PartialEq)]
9pub struct CssVar(String);
10
11impl CssVar {
12    /// Create a new CSS variable reference.
13    pub fn new(name: &str) -> Self {
14        let trimmed = name.trim();
15        let name = if trimmed.starts_with("--") {
16            trimmed.to_string()
17        } else {
18            format!("--{}", trimmed)
19        };
20        Self(name)
21    }
22}
23
24impl fmt::Display for CssVar {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "var({})", self.0)
27    }
28}
29
30/// Convenience function to create a [`CssVar`].
31pub fn var(name: &str) -> CssVar {
32    CssVar::new(name)
33}