tailwind_css_fixes/modules/layouts/z_index/
mod.rs

1use super::*;
2
3#[derive(Clone, Debug)]
4enum ZIndex {
5    Unit(i32),
6    Standard(String),
7    Arbitrary(TailwindArbitrary),
8}
9
10#[doc=include_str!("readme.md")]
11#[derive(Clone, Debug)]
12pub struct TailwindZIndex {
13    kind: ZIndex,
14}
15
16impl Display for TailwindZIndex {
17    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
18        match &self.kind {
19            ZIndex::Unit(s) => {
20                let n = s.abs();
21                match *s > 0i32 {
22                    true => write!(f, "z-{}", n),
23                    false => write!(f, "-z-{}", n),
24                }
25            },
26            ZIndex::Standard(s) => write!(f, "z-{}", s),
27            ZIndex::Arbitrary(s) => s.write_class(f, "z-"),
28        }
29    }
30}
31
32impl TailwindInstance for TailwindZIndex {
33    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
34        let index = match &self.kind {
35            ZIndex::Unit(n) => n.to_string(),
36            ZIndex::Standard(s) => s.to_string(),
37            ZIndex::Arbitrary(s) => s.get_properties(),
38        };
39        css_attributes! {
40            "z-index" => index
41        }
42    }
43}
44impl TailwindZIndex {
45    /// <https://tailwindcss.com/docs/z-index>
46    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary, negative: Negative) -> Result<Self> {
47        match pattern {
48            [] => Self::parse_arbitrary(arbitrary),
49            [s] if Self::check_valid(s) => Ok(Self { kind: ZIndex::Standard(s.to_string()) }),
50            [n] => {
51                let mut i: i32 = TailwindArbitrary::from(*n).as_integer()?;
52                if negative.0 {
53                    i = -i;
54                }
55                Ok(Self { kind: ZIndex::Unit(i) })
56            },
57            _ => syntax_error!("Unknown z-index instructions"),
58        }
59    }
60    /// dispatch to [z-index](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index)
61    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
62        Ok(Self { kind: ZIndex::Arbitrary(arbitrary.to_owned()) })
63    }
64    /// <https://developer.mozilla.org/en-US/docs/Web/CSS/z-index#syntax>
65    pub fn check_valid(mode: &str) -> bool {
66        let set = BTreeSet::from_iter(vec!["auto", "inherit", "initial", "revert", "unset"]);
67        set.contains(mode)
68    }
69}