polished_css/data_type/
alpha.rs

1use crate::data_type::{Number, NumberStorage, Percentage};
2
3/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/alpha_value)
4#[derive(
5    Clone,
6    Debug,
7    PartialEq,
8    strum_macros::EnumIs,
9    polished_css_macros::Display,
10    polished_css_macros::DataTypeFromDataTypes,
11)]
12#[display(on_enum = true)]
13pub enum Alpha {
14    // NOTE: We need to override to add bounds
15    // TODO: #[custom_constraint(fn_name)]
16    Number(Number),
17    // NOTE: We need to override to add bounds
18    Percentage(Percentage),
19}
20
21impl Default for Alpha {
22    fn default() -> Self {
23        Self::visible()
24    }
25}
26
27impl From<f64> for Alpha {
28    fn from(value: f64) -> Self {
29        Self::number(value)
30    }
31}
32
33#[polished_css_macros::create_trait_from_enum_impl()]
34impl Alpha {
35    #[must_use]
36    pub fn invisible() -> Self {
37        Self::number(0.0)
38    }
39
40    #[must_use]
41    pub fn visible() -> Self {
42        Self::number(1.0)
43    }
44
45    #[must_use]
46    pub fn full() -> Self {
47        Self::Percentage(Percentage::full())
48    }
49
50    #[must_use]
51    pub fn reset() -> Self {
52        Self::Percentage(Percentage::reset())
53    }
54
55    // TODO: Add conversion methods?
56}
57
58#[cfg(test)]
59mod test {
60    #[test]
61    fn display() {
62        use crate::data_type::*;
63        assert_eq!(super::Alpha::number(0.1).to_string(), String::from("0.1"));
64        assert_eq!(super::Alpha::visible().to_string(), String::from("1"));
65        assert_eq!(super::Alpha::invisible().to_string(), String::from("0"));
66        assert_eq!(
67            super::Alpha::percentage(10.0).to_string(),
68            String::from("10%")
69        );
70        assert_eq!(super::Alpha::full().to_string(), String::from("100%"));
71        assert_eq!(super::Alpha::reset().to_string(), String::from("0%"));
72    }
73}