tailwind_css/systems/units/length/
mod.rs

1use std::ops::Rem;
2
3use tailwind_ast::parse_fraction;
4
5use super::*;
6
7#[derive(Debug, Copy, Clone)]
8pub enum LengthUnit {
9    Fraction(u32, u32),
10    Unit(f32, &'static str),
11}
12
13impl Display for LengthUnit {
14    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Self::Fraction(a, b) => write!(f, "{}/{}", a, b),
17            Self::Unit(a, b) => write!(f, "{}{}", *a as usize, b),
18        }
19    }
20}
21
22impl LengthUnit {
23    /// <https://developer.mozilla.org/en-US/docs/Web/CSS/length#syntax>
24    pub fn parse_faction(input: &str) -> Result<Self> {
25        let (a, b) = parse_fraction(input)?.1;
26        Ok(Self::radio(a as u32, b as u32))
27    }
28    pub fn parse_length(input: &str) -> Result<Self> {
29        let valid = (unit("px"), unit("em"), unit("rem"), unit("%"));
30        let (f, unit) = tuple((parse_f32, alt(valid)))(input)?.1;
31        Ok(Self::Unit(f, unit))
32    }
33    pub fn parse_angle(input: &str) -> Result<Self> {
34        let valid = (unit("deg"), unit("rad"), unit("grad"), unit("turn"));
35        let (f, unit) = tuple((parse_f32, alt(valid)))(input)?.1;
36        Ok(Self::Unit(f, unit))
37    }
38    pub fn px(x: f32) -> Self {
39        Self::Unit(x, "px")
40    }
41    pub fn em(x: f32) -> Self {
42        Self::Unit(x, "em")
43    }
44    pub fn rem(x: f32) -> Self {
45        Self::Unit(x, "rem")
46    }
47    pub fn percent(x: f32) -> Self {
48        Self::Unit(x, "%")
49    }
50    pub fn radio(a: u32, b: u32) -> Self {
51        if b.eq(&0) {
52            return Self::Fraction(0, 1);
53        }
54        let n = gcd(a, b);
55        Self::Fraction(a / n, b / n)
56    }
57}
58
59pub fn gcd<T>(a: T, b: T) -> T
60where
61    T: PartialEq + Rem<Output = T> + Default + Copy,
62{
63    if b == T::default() { a } else { gcd(b, a % b) }
64}
65
66fn unit(unit: &'static str) -> impl Fn(&str) -> IResult<&str, &'static str> {
67    move |input: &str| tag(unit)(input).map(|(s, _)| (s, unit))
68}
69
70impl LengthUnit {
71    #[inline]
72    pub fn get_class(&self) -> String {
73        self.to_string()
74    }
75    #[inline]
76    pub fn get_class_arbitrary(&self) -> String {
77        format!("[{}]", self)
78    }
79    #[inline]
80    pub fn get_properties(&self) -> String {
81        match self {
82            Self::Fraction(a, b) => {
83                let p = *a as f32 / *b as f32;
84                format!("{}%", 100.0 * p)
85            },
86            Self::Unit(a, b) => format!("{}{}", a, b),
87        }
88    }
89
90    pub fn is_fraction(&self) -> bool {
91        matches!(self, Self::Fraction { .. })
92    }
93    pub fn is_fraction_eq(&self) -> bool {
94        match self {
95            Self::Fraction(a, b) => a.eq(b),
96            _ => false,
97        }
98    }
99    pub fn is_fraction_zero(&self) -> bool {
100        match self {
101            Self::Fraction(a, _) => a.eq(&0),
102            _ => false,
103        }
104    }
105}