tailwind_css/modules/flexbox/gap/
mod.rs

1use super::*;
2use crate::AxisXY;
3
4#[doc=include_str!("readme.md")]
5#[derive(Debug, Copy, Clone)]
6pub struct TailwindGap {
7    size: LengthUnit,
8    axis: AxisXY,
9}
10
11impl Display for TailwindGap {
12    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
13        match self.axis {
14            AxisXY::N => write!(f, "gap-{}", self.size.get_class_arbitrary()),
15            AxisXY::X => write!(f, "gap-x-{}", self.size.get_class_arbitrary()),
16            AxisXY::Y => write!(f, "gap-y-{}", self.size.get_class_arbitrary()),
17        }
18    }
19}
20
21impl TailwindInstance for TailwindGap {
22    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
23        let class = match self.axis {
24            AxisXY::N => "gap",
25            AxisXY::X => "column-gap",
26            AxisXY::Y => "row-gap",
27        };
28        css_attributes! {
29            class => self.size.get_properties()
30        }
31    }
32}
33
34impl TailwindGap {
35    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
36        match pattern {
37            ["x", rest @ ..] => Ok(Self { size: parse_size(rest, arbitrary)?, axis: AxisXY::X }),
38            ["y", rest @ ..] => Ok(Self { size: parse_size(rest, arbitrary)?, axis: AxisXY::Y }),
39            _ => Ok(Self { size: parse_size(pattern, arbitrary)?, axis: AxisXY::N }),
40        }
41    }
42}
43
44fn parse_size(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<LengthUnit> {
45    let size = match pattern {
46        [] => arbitrary.as_length_or_fraction()?,
47        ["px"] => LengthUnit::px(1.0),
48        [n] => {
49            let a = TailwindArbitrary::from(*n);
50            LengthUnit::rem(a.as_float()? / 4.0)
51        },
52        _ => return syntax_error!("Unknown gap instructions"),
53    };
54    Ok(size)
55}