tailwind_css/modules/spacing/space/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Clone, Debug)]
5pub struct TailwindSpace {
6    axis: bool,
7    negative: Negative,
8    size: SpacingSize,
9}
10
11impl Display for TailwindSpace {
12    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
13        self.negative.write(f)?;
14        match self.axis {
15            true => write!(f, "space-x-{}", self.size),
16            false => write!(f, "space-y-{}", self.size),
17        }
18    }
19}
20
21impl TailwindInstance for TailwindSpace {
22    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
23        let class = match self.axis {
24            true => "margin-left",
25            false => "margin-top",
26        };
27        css_attributes! {
28            class => self.size.get_properties()
29        }
30    }
31}
32
33impl TailwindSpace {
34    /// https://tailwindcss.com/docs/space
35    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary, negative: Negative) -> Result<Box<dyn TailwindInstance>> {
36        match pattern {
37            ["x", rest @ ..] => Self::parse_axis(rest, arbitrary, true, negative),
38            ["y", rest @ ..] => Self::parse_axis(rest, arbitrary, false, negative),
39            _ => syntax_error!("Unknown space instructions: {}", pattern.join("-")),
40        }
41    }
42    fn parse_axis(
43        pattern: &[&str],
44        arbitrary: &TailwindArbitrary,
45        axis: bool,
46        negative: Negative,
47    ) -> Result<Box<dyn TailwindInstance>> {
48        match pattern {
49            [] => Ok(Self::parse_arbitrary(arbitrary, negative, axis)?.boxed()),
50            ["reverse"] => Ok(TailwindSpaceReverse::from(axis).boxed()),
51            _ => {
52                let size = SpacingSize::parse(pattern, arbitrary, &Self::check_valid)?;
53                Ok(Self { axis, negative, size }.boxed())
54            },
55        }
56    }
57    /// https://tailwindcss.com/docs/margin#arbitrary-values
58    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary, negative: Negative, axis: bool) -> Result<Self> {
59        let size = SpacingSize::parse_arbitrary(arbitrary)?;
60        Ok(Self { axis, negative, size })
61    }
62    /// https://developer.mozilla.org/en-US/docs/Web/CSS/margin#syntax
63    pub fn check_valid(mode: &str) -> bool {
64        let set = BTreeSet::from_iter(vec!["auto", "inherit", "initial", "revert", "unset"]);
65        set.contains(mode)
66    }
67}