polished_css/function/
var.rs

1//! Cascading variables as a new primitive value type that is accepted by all
2//! CSS properties, and custom properties for defining them.
3//!
4//! ### Resources
5//!
6//! - [CSSWG specification](https://drafts.csswg.org/css-variables-1/)
7//! - [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties)
8
9use crate::{data_type::DashedIdent, utils::Nothing};
10
11/// Cascading variables as a new primitive value type that is accepted by all
12/// CSS properties, and custom properties for defining them.
13///
14/// ### Resources
15///
16/// - [CSSWG specification](https://drafts.csswg.org/css-variables-1/)
17/// - [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties)
18#[derive(Clone, Debug, PartialEq)]
19pub struct Var<T>
20where
21    T: Clone + std::fmt::Debug + std::fmt::Display + PartialEq,
22{
23    pub dashed_ident: DashedIdent,
24    pub fallback: Option<T>,
25}
26
27impl<T> std::fmt::Display for Var<T>
28where
29    T: Clone + std::fmt::Debug + std::fmt::Display + PartialEq,
30{
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        if let Some(fallback) = &self.fallback {
33            write!(f, "var({},{})", self.dashed_ident, fallback)
34        } else {
35            write!(f, "var({})", self.dashed_ident)
36        }
37    }
38}
39
40impl<T> crate::utils::UnitDataTypeContainer<T> for Var<T> where
41    T: Clone + std::fmt::Debug + std::fmt::Display + PartialEq + crate::utils::UnitDataType<Self>
42{
43}
44
45impl From<DashedIdent> for Var<Nothing> {
46    fn from(value: DashedIdent) -> Self {
47        Self {
48            dashed_ident: value,
49            fallback: None,
50        }
51    }
52}
53
54impl From<&str> for Var<Nothing> {
55    fn from(value: &str) -> Self {
56        Self {
57            dashed_ident: value.into(),
58            fallback: None,
59        }
60    }
61}
62
63impl<T> From<(DashedIdent, Option<T>)> for Var<T>
64where
65    T: Clone + std::fmt::Debug + std::fmt::Display + PartialEq + crate::utils::UnitDataType<Self>,
66{
67    fn from(value: (DashedIdent, Option<T>)) -> Self {
68        let (dashed_ident, fallback) = value;
69        Self {
70            dashed_ident,
71            fallback,
72        }
73    }
74}
75
76impl<T> From<(DashedIdent, T)> for Var<T>
77where
78    T: Clone + std::fmt::Debug + std::fmt::Display + PartialEq + crate::utils::UnitDataType<Self>,
79{
80    fn from(value: (DashedIdent, T)) -> Self {
81        let (dashed_ident, fallback) = value;
82        Self {
83            dashed_ident,
84            fallback: Some(fallback),
85        }
86    }
87}
88
89#[cfg(test)]
90mod test {
91    #[test]
92    fn display() {
93        // With fallback
94        assert_eq!(
95            super::Var::<crate::property::AllValue> {
96                dashed_ident: "example-with-fallback".into(),
97                fallback: Some(crate::property::AllValue::Initial)
98            }
99            .to_string(),
100            String::from("var(--example-with-fallback,initial)"),
101        );
102        // Without fallback
103        assert_eq!(
104            super::Var::from("example-without-fallback").to_string(),
105            String::from("var(--example-without-fallback)"),
106        );
107    }
108}