tailwind_css_fixes/modules/typography/leading/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Debug, Clone)]
5pub struct TailwindLeading {
6    kind: LineHeight,
7}
8
9#[derive(Debug, Clone)]
10enum LineHeight {
11    Length(LengthUnit),
12    Standard(String),
13}
14
15impl Display for LineHeight {
16    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17        match self {
18            Self::Length(n) => write!(f, "{}", n.get_class_arbitrary()),
19            Self::Standard(g) => write!(f, "{}", g),
20        }
21    }
22}
23
24impl Display for TailwindLeading {
25    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26        write!(f, "leading-{}", self.kind)
27    }
28}
29
30impl TailwindInstance for TailwindLeading {
31    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
32        let leading = match &self.kind {
33            LineHeight::Length(n) => n.get_properties(),
34            LineHeight::Standard(g) => g.to_string(),
35        };
36        css_attributes! {
37            "line-height" => leading
38        }
39    }
40}
41
42impl TailwindLeading {
43    /// https://tailwindcss.com/docs/line-height
44    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
45        match pattern {
46            ["none"] => scale(1.0),
47            ["tight"] => scale(1.25),
48            ["snug"] => scale(1.375),
49            // different from tailwind.js
50            ["wide"] => scale(1.5),
51            ["wider" | "relaxed"] => scale(1.625),
52            ["widest" | "loose"] => scale(2.0),
53            // https://developer.mozilla.org/zh-CN/docs/Web/CSS/line-height#normal
54            ["normal"] => Ok(Self { kind: LineHeight::Standard("normal".to_string()) }),
55            [] => Self::parse_arbitrary(arbitrary),
56            [n] => Self::parse_arbitrary(&TailwindArbitrary::from(*n)),
57            _ => syntax_error!("Unknown leading instructions: {}", pattern.join("-")),
58        }
59    }
60    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
61        Self::maybe_no_unit(arbitrary).or_else(|_| Self::maybe_length(arbitrary))
62    }
63    #[inline]
64    fn maybe_no_unit(arbitrary: &TailwindArbitrary) -> Result<Self> {
65        rem(arbitrary.as_float()? / 4.0)
66    }
67    #[inline]
68    fn maybe_length(arbitrary: &TailwindArbitrary) -> Result<Self> {
69        rem(arbitrary.as_float()? / 4.0)
70    }
71}
72
73#[inline(always)]
74fn scale(x: f32) -> Result<TailwindLeading> {
75    Ok(TailwindLeading { kind: LineHeight::Length(LengthUnit::percent(x * 100.0)) })
76}
77#[inline(always)]
78fn rem(x: f32) -> Result<TailwindLeading> {
79    Ok(TailwindLeading { kind: LineHeight::Length(LengthUnit::rem(x)) })
80}