logo
 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
use std::fmt::Write;

use super::*;

#[doc = include_str!("readme.md")]
#[derive(Copy, Clone, Debug)]
pub struct TailwindScale {
    negative: bool,
    scale: usize,
    axis: Option<bool>,
}
impl Display for TailwindScale {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.negative {
            f.write_char('-')?
        }
        match self.axis {
            None => write!(f, "scale-{}", self.scale),
            Some(true) => write!(f, "scale-x-{}", self.scale),
            Some(false) => write!(f, "scale-y-{}", self.scale),
        }
    }
}

impl TailwindInstance for TailwindScale {
    fn attributes(&self, _: &TailwindBuilder) -> BTreeSet<CssAttribute> {
        let scale = self.scale as f32 / 100.0;
        let scale = match self.axis {
            None => format!("scale({})", scale),
            Some(true) => format!("scaleX({})", scale),
            Some(false) => format!("scaleY({})", scale),
        };
        css_attributes! {
            "transform" => scale
        }
    }
}

impl TailwindScale {
    // https://tailwindcss.com/docs/scale
    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary, negative: bool) -> Result<Self> {
        debug_assert!(arbitrary.is_none(), "forbidden arbitrary");
        match pattern {
            ["x", rest @ ..] => parse_axis(rest, Some(true), negative),
            ["y", rest @ ..] => parse_axis(rest, Some(false), negative),
            _ => parse_axis(pattern, Some(true), negative),
        }
    }
}

fn parse_axis(input: &[&str], axis: Option<bool>, negative: bool) -> Result<TailwindScale> {
    match input {
        [n] => {
            let scale = TailwindArbitrary::from(*n).as_integer()?;
            Ok(TailwindScale { negative, scale, axis })
        },
        _ => syntax_error!("Unknown scale instructions: {}", input.join("-")),
    }
}