tailwind_css/modules/flexbox/span/
resolve.rs

1use super::*;
2
3#[derive(Debug, Copy, Clone)]
4pub(super) enum GridKind {
5    Start(GridSize),
6    End(GridSize),
7    Span(GridSize),
8}
9
10#[derive(Debug, Copy, Clone)]
11pub(super) enum GridSize {
12    Auto,
13    Full,
14    Unit(i32),
15}
16
17impl Display for GridKind {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Self::Start(n) => write!(f, "start-{}", n),
21            Self::End(n) => write!(f, "end-{}", n),
22            Self::Span(n) => write!(f, "span-{}", n),
23        }
24    }
25}
26
27impl Display for GridSize {
28    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::Auto => write!(f, "auto"),
31            Self::Full => write!(f, "full"),
32            Self::Unit(n) => write!(f, "{}", n),
33        }
34    }
35}
36
37impl GridSize {
38    pub fn parse(pattern: &str, allow_full: bool) -> Result<Self> {
39        debug_assert!(allow_full, "can't set to full");
40        let size = match pattern {
41            "auto" => Self::Auto,
42            "full" => Self::Full,
43            n => Self::Unit(TailwindArbitrary::from(n).as_integer()?),
44        };
45        Ok(size)
46    }
47    pub fn get_properties_span(&self) -> String {
48        match self {
49            Self::Auto => "auto".to_string(),
50            Self::Full => "1 / -1".to_string(),
51            Self::Unit(n) => format!("span {n} / span {n}", n = n),
52        }
53    }
54    pub fn get_properties_start_end(&self) -> String {
55        match self {
56            Self::Auto => "auto".to_string(),
57            Self::Full => "full".to_string(),
58            Self::Unit(n) => n.to_string(),
59        }
60    }
61}
62
63impl GridKind {
64    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
65        let kind = match pattern {
66            ["auto"] => Self::Span(GridSize::Auto),
67            ["start", n] => Self::Start(GridSize::parse(n, false)?),
68            ["end", n] => Self::End(GridSize::parse(n, false)?),
69            ["span", n] => Self::Span(GridSize::parse(n, true)?),
70            [] => Self::parse_arbitrary(arbitrary)?,
71            _ => return syntax_error!("Unknown shrink instructions: {}", pattern.join("-")),
72        };
73        Ok(kind)
74    }
75    pub fn parse_arbitrary(_arbitrary: &TailwindArbitrary) -> Result<Self> {
76        Ok(Self::Span(GridSize::Full))
77    }
78    pub fn get_properties(&self) -> String {
79        match self {
80            Self::Start(n) => n.get_properties_start_end(),
81            Self::End(n) => n.get_properties_start_end(),
82            Self::Span(n) => n.get_properties_span(),
83        }
84    }
85}