tailwind_css_fixes/systems/units/integer_only/
mod.rs1use super::*;
2
3mod traits;
4
5#[derive(Debug, Clone)]
7pub enum NumericValue {
8 Number { n: f32, negative: bool, can_be_negative: bool },
9 Keyword(String),
10 Arbitrary(TailwindArbitrary),
11}
12
13impl NumericValue {
14 pub fn get_properties(&self, number: impl FnOnce(&f32) -> String) -> String {
15 match self {
16 Self::Number { n, .. } => number(n),
17 Self::Keyword(s) => s.to_string(),
18 Self::Arbitrary(s) => s.get_properties(),
19 }
20 }
21 pub fn write_negative(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22 match *self {
23 Self::Number { n, can_be_negative, .. } if can_be_negative && n < 0.0 => write!(f, "-"),
24 _ => write!(f, ""),
25 }
26 }
27 pub fn write_class(&self, f: &mut Formatter, before: &str) -> std::fmt::Result {
28 write!(f, "{}{}", before, self)
29 }
30}
31
32impl NumericValue {
33 pub fn negative_parser(
34 id: &'static str,
35 checker: impl Fn(&str) -> bool,
36 ) -> impl Fn(&[&str], &TailwindArbitrary, Negative) -> Result<Self> {
37 move |pattern: &[&str], arbitrary: &TailwindArbitrary, negative: Negative| {
38 let joined = pattern.join("-");
39 match pattern {
40 _ if checker(&joined) => Ok(Self::Keyword(joined)),
41 [] => Self::parse_arbitrary(arbitrary),
42 [n] => Self::parse_number(n, negative),
43 _ => Err(TailwindError::syntax_error(format!("Unknown {} pattern", id))),
44 }
45 }
46 }
47 pub fn positive_parser(
48 id: &'static str,
49 checker: impl Fn(&str) -> bool,
50 ) -> impl Fn(&[&str], &TailwindArbitrary) -> Result<Self> {
51 move |pattern: &[&str], arbitrary: &TailwindArbitrary| {
52 let joined = pattern.join("-");
53 match pattern {
54 _ if checker(&joined) => Ok(Self::Keyword(joined)),
55 [] => Self::parse_arbitrary(arbitrary),
56 [n] => {
57 let i = TailwindArbitrary::from(*n).as_integer()?;
58 Ok(Self::Number { n: i as f32, negative: false, can_be_negative: true })
59 },
60 _ => Err(TailwindError::syntax_error(format!("Unknown {} pattern", id))),
61 }
62 }
63 }
64 pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
65 Ok(Self::Arbitrary(TailwindArbitrary::new(arbitrary)?))
66 }
67 pub fn parse_number(n: &str, negative: Negative) -> Result<Self> {
68 let mut n = TailwindArbitrary::from(n).as_float()?;
69 if negative.0 {
70 n = -n
71 }
72 Ok(Self::Number { n, negative: negative.0, can_be_negative: false })
73 }
74}