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_name(&self, f: &mut Formatter, prefix: &str) -> std::fmt::Result {
32 match self {
33 Self::Number { n, .. } => {
34 self.write_negative(f)?;
35 write!(f, "{}{}", prefix, n.abs())
36 }
37 Self::Keyword(s) => write!(f, "{}{}", prefix, s),
38 Self::Arbitrary(s) => write!(f, "{}[{}]", prefix, s.get_properties()),
39 }
40 }
41}
42
43impl NumericValue {
44 pub fn negative_parser(
46 id: &'static str,
47 checker: impl Fn(&str) -> bool,
48 ) -> impl Fn(&[&str], &TailwindArbitrary, Negative) -> Result<Self> {
49 move |pattern: &[&str], arbitrary: &TailwindArbitrary, negative: Negative| {
50 let joined = pattern.join("-");
51 match pattern {
52 _ if checker(&joined) => Ok(Self::Keyword(joined)),
53 [] => Self::parse_arbitrary(arbitrary),
54 [n] => Self::parse_number(n, negative),
55 _ => Err(TailwindError::syntax_error(format!("Unknown {} pattern", id))),
56 }
57 }
58 }
59 pub fn positive_parser(
61 id: &'static str,
62 checker: impl Fn(&str) -> bool,
63 ) -> impl Fn(&[&str], &TailwindArbitrary) -> Result<Self> {
64 move |pattern: &[&str], arbitrary: &TailwindArbitrary| {
65 let joined = pattern.join("-");
66 match pattern {
67 _ if checker(&joined) => Ok(Self::Keyword(joined)),
68 [] => Self::parse_arbitrary(arbitrary),
69 [n] => {
70 let i = TailwindArbitrary::from(*n).as_integer()?;
71 Ok(Self::Number { n: i as f32, negative: false, can_be_negative: false })
72 },
73 _ => Err(TailwindError::syntax_error(format!("Unknown {} pattern", id))),
74 }
75 }
76 }
77 pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
78 Ok(Self::Arbitrary(TailwindArbitrary::new(arbitrary)?))
79 }
80 pub fn parse_number(n: &str, negative: Negative) -> Result<Self> {
82 let mut n = TailwindArbitrary::from(n).as_float()?;
83 if negative.0 {
84 n = -n
85 }
86 Ok(Self::Number { n, negative: negative.0, can_be_negative: true })
87 }
88}