1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use super::*;

#[derive(Debug, Clone)]
enum Basis {
    Number(f32),
    Length(LengthUnit),
    Standard(String),
    Arbitrary(TailwindArbitrary),
}

#[doc=include_str!("readme.md")]
#[derive(Debug, Clone)]
pub struct TailwindBasis {
    kind: Basis,
}

impl Display for TailwindBasis {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "basis-")?;
        match &self.kind {
            Basis::Number(n) => write!(f, "{}", n),
            Basis::Length(n) if n.is_fraction() => write!(f, "{}", n.get_class()),
            Basis::Length(n) => write!(f, "{}", n.get_class_arbitrary()),
            Basis::Standard(s) => match s.as_str() {
                "fit-content" => write!(f, "fit"),
                "min-content" => write!(f, "min"),
                "max-content" => write!(f, "max"),
                _ => write!(f, "{}", s),
            },
            Basis::Arbitrary(s) => s.write(f),
        }
    }
}

impl TailwindInstance for TailwindBasis {
    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
        let basis = match &self.kind {
            Basis::Number(n) => format!("{}rem", *n as f32 / 4.0),
            Basis::Length(n) => n.get_properties(),
            Basis::Standard(s) => s.to_string(),
            Basis::Arbitrary(s) => s.get_properties(),
        };
        css_attributes! {
            "flex-basis" => basis
        }
    }
}

impl TailwindBasis {
    /// <https://tailwindcss.com/docs/flex-basis>
    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self { kind: Basis::parse(pattern, arbitrary)? })
    }

    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self { kind: Basis::parse_arbitrary(arbitrary)? })
    }
    /// https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis#syntax
    pub fn check_valid(mode: &str) -> bool {
        Basis::check_valid(mode)
    }
}

impl Basis {
    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
        let out = match pattern {
            ["px"] => Self::Length(LengthUnit::px(1.0)),
            ["full"] => Self::Length(LengthUnit::Fraction(1, 1)),
            ["fit" | "min" | "max", "content"] => Self::Standard(pattern.join("-")),
            [s @ ("fit" | "min" | "max")] => Self::Standard(format!("{}-content", s)),
            [s] if Self::check_valid(s) => Self::Standard(s.to_string()),
            [n] => {
                let a = TailwindArbitrary::from(*n);
                Self::maybe_length(&a).or_else(|_| Self::maybe_float(&a))?
            },
            [] => Self::parse_arbitrary(arbitrary)?,
            _ => return syntax_error!("Unknown basis instructions"),
        };
        Ok(out)
    }
    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self::Arbitrary(TailwindArbitrary::new(arbitrary)?))
    }
    fn maybe_float(arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self::Number(arbitrary.as_float()?))
    }
    fn maybe_length(arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self::Length(arbitrary.as_length_or_fraction()?))
    }
    pub fn check_valid(mode: &str) -> bool {
        let set = BTreeSet::from_iter(vec![
            "auto",
            "content",
            "fill",
            "fit-content",
            "inherit",
            "initial",
            "max-content",
            "min-content",
            "revert",
            "unset",
        ]);
        set.contains(mode)
    }
}